Ignoring PHP for a moment; to execute a string of commands on a remote
machine using ssh you would execute the following;

Prompt>ssh -l user "ps -ef|grep edit;cd /usr/local/bin;ls -la"

Which would result in showing any edit processes, followed by a listing of
the contents of the /usr/local/bin directory.

Notice that I did not include a password (this is not needed if the two
servers have already exchanged security keys), and as near as I know, cannot
be supplied unless you are there to type it in on a tty type device, so be
sure to get the keys setup properly.  You will also need the client program
"ssh" installed on the originating machine, and the daemon "sshd" installed
on the remote machine, but then you probably knew that ;-)

In PHP simply;

$cmd = "ssh -l user \"ps -ef|grep edit;cd /usr/local/bin;ls -la";
$rtn = exec($cmd, $responselines);

http://us4.php.net/manual/en/function.exec.php

$responselines is now an array containing lines of output from the commands.

Hope this helps,

Warren Vail


-----Original Message-----
From: Sean Smith [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, September 07, 2004 11:43 AM
To: Vail, Warren
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] remote script execution


Warren,

    I use ssh on all of my servers for logging in remotely. My next 
question would be, how do I connect to the remote server to run commands 
through PHP? Do I need to setup a socket or something? Could you provide 
me a pseudo sample or maybe point me to some docs. I've never had to do 
this type of thing in PHP before and I want to make sure I do it right. 
Thanks.

Sean

Vail, Warren wrote:

>I've done a number of these and the choice depends on many things but 
>perhaps the biggest factor is;
>
>Is server 1 and server 2 behind the same firewall or are they exposed 
>to the internet?
>
>Behind a firewall you can probably get away with running any one of the 
>following commands to remotely execute your script on server 2, from 
>server 1;
>
>Rexec, rsh, rpc, just to name 3.  The client part of the protocol is 
>required on server 1 and the daemon part needs to be running on server 
>2.
>
>Across the open internet, be very careful here, I would think the 
>choice would be ssh (secure shell).  This has several nice features 
>that could be useful internally as well.  You start out by exchanging 
>encryption keys between the two servers and because of this handshake, 
>each server trusts the other in a way that the correct execution 
>permissions can be established on server 2, and of course the data sent 
>between the servers is encrypted rendering it virtually useless to 
>third parties.  Google for ssh for more info.
>
>Warren Vail
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to