1

On my VPN I have a remote server and a remote router (which is running RouterOS with an API and supports ssh connections). What I want is to write a php script and deploy it on the server so it will connect to the remote router using ip address and login credentials and than, run some commands.

I read on the internet that there is a solution for that which is libssh2.php library, but I cannot figure out how to install/use or even test if it work on the server. The Server is running CentOS.

Thank you in advance!!!

KostR
  • 101
  • 3
  • 11

2 Answers2

1

I think you'd be best off with phpseclib, a pure PHP SSH implementation. It has a number of advantages over libssh2:

http://phpseclib.sourceforge.net/ssh/compare.html

Here's an example:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
0

I did this before and was simply using the SSH command directly. For example:

$sshCmd = "ssh user@1.2.3.45 \"ls -la ~\"";
exec($sshCmd, $output, $errorCode);
echo "Error code: $errorCode\n";
echo "Output: " . implode("\n", $output);

If you have more complex scripts to run, you can put then in a .sh script and run then via bash:

$sshCmd = "ssh user@1.2.3.45 'bash -s' < \"/path/to/script.sh\"";
exec($sshCmd, $output, $errorCode);
echo "Error code: $errorCode\n";
echo "Output: " . implode("\n", $output);
laurent
  • 88,262
  • 77
  • 290
  • 428
  • Ehm, ok, but how do I see the output??? I wrote did as you suggested and the server didn't return me an error. But how do I know that the commands were executed??? Is there a way to output what ever the commands return??? – KostR Jun 19 '14 at 01:28
  • 1
    @user2462749, `exec` can optionally return the command output and error code. I've updated my example to show this. – laurent Jun 19 '14 at 01:37
  • So I wrote exec($sshCmd, $shhOut, $shhErr) or die("Failed to connect"); ___ And it prints "Failed to connect". So my guess is that exec is not working. Any ideas what might be the problem??? – KostR Jun 19 '14 at 01:44
  • 1
    No that wouldn't work, you need to send all the commands in one go via SSH. In your case, it would be something like this: `ssh admin@192.168.252.34 "/system backup name=phpRemoteBackup"`. First, you could try this in your shell to double-check that it actually works. Also try without the `die()` statement. Instead, inspect the output and error code of ssh as in the example code in my answer. – laurent Jun 19 '14 at 01:48
  • I tried ssh admin@192.168.252.34 /system clock print on my shell and it didn't work. It asked me for the password. How do I put the password with the username??? – KostR Jun 19 '14 at 02:29
  • Also, when I tried to run the command from the php output was an array and error message 255 – KostR Jun 19 '14 at 02:32
  • You need to use public/private key authentication if you want to automate things with ssh. You can set this up for example as described there - http://www.ece.uci.edu/~chou/ssh-key.html – laurent Jun 19 '14 at 03:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55884/discussion-between-user2462749-and-this-lau). – KostR Jun 19 '14 at 05:17