Jump to content

PHP script to open a linux command line remotely


MetalSmith

Recommended Posts

You can use ssh to connect to the remote machine and execute a command.  For example:

 

ssh [email protected] 'ls -lath'

 

You can call this command using the PHP backtick techniqe to exec it in a shell and capture the output into a variable, assuming that is necessary, or you can simply exec() it. 

 

Some details you will probably need to figure out -- what windows compatible ssh will you utilize?  Putty is frequently used.  It will need to be in the path possibly.

 

You wlll have to setup key files on the hosts so that you can authenticate without having to utilize a password.

 

 

 

Ok I am very new to PHP. Is there a way I can manually test this ssh command on the windows box? Also is ssh something I need to install?

 

the command I need to remotely run is asterisk -rx "reload"

 

So would my command be like ssh 192.168.0.1 'asterisk -rx "reload"'

 

I will do some more resarch on ssh since I am unfamiliar with it.

I did some looking and found a site that had a command that I am trying. I modified it to see if it would work. when I run php script.php it opens up putty real fast and closes it. I am not sure if its working. If by chance I am on the right track here where in this command would I enter the linux command asterisk -r?

 

Thanks!

 

<?php

 

echo exec('c:\putty.exe -ssh 192.168.0.xxx: -l {username} -pw {password}');

 

?>

  • 1 month later...

LOL that program looks just like winSCP. I am already using that. Does that program let you run commands to? I just use it to copy and delete file off my linux box.

 

Winscp just implements scp, but they are indeed related (ssh and scp).  Scp is for copying files between hosts, whilst ssh is for getting a remote shell and running commands.

I did some looking and found a site that had a command that I am trying. I modified it to see if it would work. when I run php script.php it opens up putty real fast and closes it. I am not sure if its working. If by chance I am on the right track here where in this command would I enter the linux command asterisk -r?

 

Thanks!

 

basic ssh allows you to specify commands following your parameters, easiest way being ssh user@remoteserver "ls -la".  This works, if you already have keys setup on each server.

 

However, the Putty distribution also provides a command line tool called plink that is designed for doing automated stuff.  What you want to do is setup a profile in putty and save it with the server credentials, name and password saved etc.  You can then do something like this:

 

$output = `plink sessionname asterisk -r`;
echo $output;

 

Note that I used backticks as I mentioned previously and not regular single quotes. 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.