从本地PHP脚本运行远程python脚本

问题描述 投票:2回答:2

我有两台机器,192.168.10.6(本地机器)有我的php脚本,192.168.12.163(远程机器)有我的python脚本。我如何从本地PHP脚本中运行这个远程python脚本?

我有一个从本地PHP脚本运行本地python脚本的工作代码,但我不能从本地PHP脚本运行远程python脚本。

php python remote-server
2个回答
2
投票

我正准备建议使用 shell_execexec 来生成ssh并在远程主机上运行命令,例如。

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

然而,我看到PHP支持用 ssh2_exec比如说,如果你的服务器上没有ssh2,而且你也无法安装,你可以尝试一下。

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

如果ssh2在你的服务器上不可用 而且你也无法安装它,你可以尝试一下 phpseclib (见 此处 (例如)


0
投票

看一看 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()
© www.soinside.com 2019 - 2024. All rights reserved.