MetalSmith Posted February 19, 2009 Share Posted February 19, 2009 Hey everyone, Ok here is what I need to do. I need a php script on a windows machine to connect to a linux box remotely open the command line and run a command. Has anyone does this type of thing? Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 19, 2009 Share Posted February 19, 2009 You can use ssh to connect to the remote machine and execute a command. For example: ssh david@myhost.com '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. Quote Link to comment Share on other sites More sharing options...
MetalSmith Posted February 19, 2009 Author Share Posted February 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 19, 2009 Share Posted February 19, 2009 Yes -- as I stated in my reply, Putty. Putty is a free open source ssh implementation for windows. Quote Link to comment Share on other sites More sharing options...
MetalSmith Posted February 19, 2009 Author Share Posted February 19, 2009 Let me do some reading about Putty. I will start there. Thanks for the help getting started! Quote Link to comment Share on other sites More sharing options...
MetalSmith Posted February 19, 2009 Author Share Posted February 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
MetalSmith Posted February 19, 2009 Author Share Posted February 19, 2009 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}'); ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.