severndigital Posted June 30, 2009 Share Posted June 30, 2009 SO i need to run an command line from my PHP script ... the only problem is, the command needs to be run on a different server. i need to log into the other server using SSH and then run the command. how I get PHP to do that?... CAN I get php to do that? I looked into the ssh2 commands, but I won't be able to install that on our webserver. I guess my biggest hold up is that the ssh command prompts me for a password. How can I get around that? or get PHP to input the password for me? Any help would be great. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/ Share on other sites More sharing options...
patrickmvi Posted June 30, 2009 Share Posted June 30, 2009 What ssh command are you trying to use? There may a command-line option to it that allows you to pass the password right to it. Besides that, it might be a little tricky to pass information back and forth to the command. There may be some way to implement some sort of rsh-like functionality via SSH which would allow you to run commands remotely provided that permissions are all set correctly. Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866284 Share on other sites More sharing options...
nadeemshafi9 Posted June 30, 2009 Share Posted June 30, 2009 i think you can do it using ftp_site which has some basic commands http://uk2.php.net/manual/en/function.ftp-exec.php - this one i think http://uk2.php.net/ftp_site http://uk2.php.net/manual/en/function.ftp-raw.php Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866287 Share on other sites More sharing options...
nadeemshafi9 Posted June 30, 2009 Share Posted June 30, 2009 What ssh command are you trying to use? There may a command-line option to it that allows you to pass the password right to it. Besides that, it might be a little tricky to pass information back and forth to the command. There may be some way to implement some sort of rsh-like functionality via SSH which would allow you to run commands remotely provided that permissions are all set correctly. what like do the command on the local-remote which exectues it on the remote-remote ? sounds good Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866290 Share on other sites More sharing options...
Adam Posted June 30, 2009 Share Posted June 30, 2009 How about system() or exec()? Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866292 Share on other sites More sharing options...
severndigital Posted June 30, 2009 Author Share Posted June 30, 2009 i'm just trying to get into the other box using in the command line directly I would type ssh servername -l username it will prompt me for a password Once I am logged into the other box, I would just issue the command line I need. the command line is an actual program command not just an ls or something so I'm not sure FTP_SITE would work. it looks something like this /usr/bin/app p32 -P -T7we -p/tmp/output/filename.xml -gd=true it basically tells one of our production applications to export certain data and output it to an xml file that our website will then pick up. Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866296 Share on other sites More sharing options...
nadeemshafi9 Posted June 30, 2009 Share Posted June 30, 2009 How about system() or exec()? forgive me if im wrong off teh top of my head i think they cant exec remotley ? or am i wrng ? i'm just trying to get into the other box using in the command line directly I would type ssh servername -l username you can do that with exec you got confused with you server to server comms, you wont be executing that command on server 2 you will be executing it on server 1 but there is a better way execute it directly on server 2 using server 1. <?php // variable initialization $command = 'ls -al >files.txt'; // set up basic connection $ftp_server = "ftp.example.com"; $conn_id = ftp_connect($ftp_server); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // execute command if (ftp_exec($conn_id, $command)) { echo "$command executed successfully\n"; } else { echo "could not execute $command\n"; } // close the connection ftp_close($conn_id); ?> or you can execute the commands on server 1 just like you would do on a command line from server 1 to server 2 Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866297 Share on other sites More sharing options...
severndigital Posted June 30, 2009 Author Share Posted June 30, 2009 you can do that with exec yeah i know, but then it prompts for a password ... how do i put that in there? Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866299 Share on other sites More sharing options...
severndigital Posted June 30, 2009 Author Share Posted June 30, 2009 forget it .. i am just going to have server 1 create a file with the command line it needs run in it. then i will have server 2 watch the folder, if it sees a file there, it will parse it, execute the command and delete server 1 created file. Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866304 Share on other sites More sharing options...
Adam Posted June 30, 2009 Share Posted June 30, 2009 If you decide to still go ahead with original idea, there is a way you can do it by generating a new key. Found this: https://lists.ubuntu.com/archives/ubuntu-users/2006-June/082869.html Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866306 Share on other sites More sharing options...
Daniel0 Posted June 30, 2009 Share Posted June 30, 2009 You can just pass the command directly to ssh. Example: daniel@daniel0:~$ ssh daniel@phpfreaks.com 'df -h' # on phpfreaks Filesystem Size Used Avail Use% Mounted on /dev/simfs 200G 22G 179G 11% / daniel@daniel0:~$ df -h # here Filesystem Size Used Avail Use% Mounted on /dev/simfs 40G 888M 39G 3% / tmpfs 4,0G 0 4,0G 0% /lib/init/rw tmpfs 4,0G 0 4,0G 0% /dev/shm Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866326 Share on other sites More sharing options...
severndigital Posted June 30, 2009 Author Share Posted June 30, 2009 You can just pass the command directly to ssh. Example: daniel@daniel0:~$ ssh daniel@phpfreaks.com 'df -h' # on phpfreaks Filesystem Size Used Avail Use% Mounted on /dev/simfs 200G 22G 179G 11% / daniel@daniel0:~$ df -h # here Filesystem Size Used Avail Use% Mounted on /dev/simfs 40G 888M 39G 3% / tmpfs 4,0G 0 4,0G 0% /lib/init/rw tmpfs 4,0G 0 4,0G 0% /dev/shm the command still asks for the password. I need PHP to be able to input the password for the other machine. Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866383 Share on other sites More sharing options...
Daniel0 Posted June 30, 2009 Share Posted June 30, 2009 Use public key authentication instead of password authentication. Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866502 Share on other sites More sharing options...
Maq Posted June 30, 2009 Share Posted June 30, 2009 Use public key authentication instead of password authentication. Here's the tutorial I use: http://wp.uberdose.com/2006/10/16/ssh-automatic-login/ Quote Link to comment https://forums.phpfreaks.com/topic/164231-using-the-command-line-from-php/#findComment-866510 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.