Jump to content

using the command line from PHP


severndigital

Recommended Posts

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

 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.