[英]Running remote python script from local PHP script
我有兩台機器,192.168.10.6(本地機器)有我的 php 腳本,192.168.12.163(遠程機器)有我的 python 腳本。 如何從本地 PHP 腳本運行這個遠程 python 腳本?
我有一個從本地 PHP 腳本運行本地 python 腳本的工作代碼,但我無法從本地 PHP 腳本運行遠程 Python 腳本。
我正要提議使用shell_exec
/ exec
來生成 ssh 並在遠程主機上運行命令,例如:
$out = shell_exec('ssh user@192.168.12.163 "ls -la"');
但是,我看到 PHP 支持ssh2_exec
,例如:
$connection = ssh2_connect('192.168.12.163', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'python /path/to/script');
看看帕拉米科
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.