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 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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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}');

 

?>

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

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. 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.