3

I have two machines, 192.168.10.6 (local machine) which has my php script and 192.168.12.163 (remote machine) which has my python script. How can I run this remote python script from a local PHP script?

I have a working code for running local python script from a local PHP script but I'm not able run remote Python script from a local PHP script.

4
  • 1
    Can you ssh to the remote? You could use ssh to run a process on the remote: cyberciti.biz/tips/linux-running-commands-on-a-remote-host.html Commented Dec 23, 2015 at 14:36
  • Yes, I can ssh to remote server but I want this to happen through PHP script calling python script located on a remote machine. Commented Dec 23, 2015 at 14:42
  • Do you mean you want the python script to be run at your local machine or at the remote machine? Commented Dec 23, 2015 at 14:48
  • @PavanR: Pemap has a point... if you need to run the (python) script on the local machine, you have to scp it Commented Dec 23, 2015 at 14:56

2 Answers 2

3

I was about to propose using shell_exec/exec to spawn ssh and run a command on the remote host, for example:

$out = shell_exec('ssh [email protected] "ls -la"');

However, I see that PHP supports that with ssh2_exec, example:

$connection = ssh2_connect('192.168.12.163', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'python /path/to/script');

If ssh2 is not available on your server and you cannot install it, you can try phpseclib (see here for example)

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at Paramiko

import paramiko, base64
key = paramiko.RSAKey(data=base64.decodestring('AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('myRemoteMachine', username='strongbad', password='thecheat')
stdin, stdout, stderr = client.exec_command('python myScript.py')
for line in stdout:
    print '... ' + line.strip('\n')
client.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.