Reputation: 101
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!!!
Upvotes: 1
Views: 3118
Reputation: 19
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');
?>
Upvotes: 1
Reputation: 90776
I did this before and was simply using the SSH command directly. For example:
$sshCmd = "ssh [email protected] \"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 [email protected] 'bash -s' < \"/path/to/script.sh\"";
exec($sshCmd, $output, $errorCode);
echo "Error code: $errorCode\n";
echo "Output: " . implode("\n", $output);
Upvotes: 0