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.

Pavan R
  • 119
  • 2
  • 9
  • 1
    Can you ssh to the remote? You could use ssh to run a process on the remote: http://www.cyberciti.biz/tips/linux-running-commands-on-a-remote-host.html – urban Dec 23 '15 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. – Pavan R Dec 23 '15 at 14:42
  • Do you mean you want the python script to be run at your local machine or at the remote machine? – SOFe Dec 23 '15 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 – urban Dec 23 '15 at 14:56

2 Answers2

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 user@192.168.12.163 "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)

Community
  • 1
  • 1
urban
  • 5,392
  • 3
  • 19
  • 45
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()
Xavier Ashe
  • 123
  • 1
  • 8