运行 来自本地 PHP 脚本的远程 python 脚本

Running remote python script from local PHP script

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

我有来自本地 PHP 脚本的 运行ning 本地 python 脚本的工作代码,但我无法 运行 远程 Python来自本地 PHP 脚本的脚本。

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

我正要提议使用 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');

如果 ssh2 在您的服务器上不可用并且您无法安装它,您可以尝试 phpseclib (see here 例如)