Pavan R
Pavan R

Reputation: 119

Running remote python script from local PHP script

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.

Upvotes: 3

Views: 3041

Answers (2)

urban
urban

Reputation: 5702

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)

Upvotes: 3

Xavier Ashe
Xavier Ashe

Reputation: 123

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()

Upvotes: 0

Related Questions