tags:

views:

414

answers:

3

I am currently trying to figure out how to connect to another server via SSH using PHP's shell functions. I have a site where I have to pass data from PHP to a custom command line program, then return the output. On the old server I was using, this was possible via the exec() function:

$cmd = '/path/to/custom/program "arg1", "arg2", "arg3"';
exec($cms, $output);

$output, of course, would then hold the data that my program returned.

I am now in a position where the host will not allow me to run my custom program. PHP's shell functions, however, are still available. The host suggested I set up PHP to SSH into a different server, run the commands I need to run, and then return the output. This seems like an incredibly backwards way of doing things, but what do I know?

My specific question relates to how I would go about passing the login information to the exec() command. For example, when I connect to any SSH server manually, I put in:

ssh myserver.com -l myusername

And then I see:

[email protected]'s password:

If I were to pass "ssh myserver.com -l myusername" as the command to exec(), how would I then give it the password? After that, how would I then know that I am properly authenticated and connected so I can run the command for my program?

Oh, and also, I do not have access to the ssh2 functions in PHP, nor would I be able to install that extension.

+3  A: 

you could use keys to get around the password problem.

http://pkeck.myweb.uga.edu/ssh/

Oren Mazor
+1. Automating SSH with plain old passwords is no fun.
Matchu
Wouldn't I need to be able to have shell access to my primary machine (the one the PHP script is running on) in order to do this? That's something else I forgot to mention. No shell access what so ever on the primary server.
Charles Chadwick
is your primary server the one executing the php or the one you are connecting to via ssh?
jdizzle
A: 

You need bi-directional communication with the new process from your PHP code - that being the case, proc_open(...) is the only php function you can use to run ssh.

After that - its simply a matter of writing code to wait for prompts and respond to them.

However there is ssh implementation written in PHP posted at phpclasses - although I've no idea how fast/efficient/reliable/compatible it is. I suspect that it won't be any less effort than the proc_open() approach and possible a lot more.

C.

symcbean
A: 

Never tried, but did you consider the PHP core functions passthru() or system().

They are similar to exec().

mr-euro