【问题标题】:Running remote python script from local PHP script从本地 PHP 脚本运行远程 python 脚本
【发布时间】:2015-12-23 14:34:50
【问题描述】:

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

我有一个可以从本地 PHP 脚本运行本地 Python 脚本的工作代码,但我无法从本地 PHP 脚本运行远程 Python 脚本。

【问题讨论】:

  • 你能 ssh 到远程吗?您可以使用 ssh 在远程运行进程:cyberciti.biz/tips/linux-running-commands-on-a-remote-host.html
  • 是的,我可以通过 ssh 连接到远程服务器,但我希望通过 PHP 脚本调用位于远程机器上的 python 脚本来实现。
  • 你的意思是你想让python脚本在你的本地机器上运行还是在远程机器上运行?
  • @PavanR: Pemap 有一点...如果你需要在本地机器上运行 (python) 脚本,你必须 scp 它

标签: php python remote-server


【解决方案1】:

我正要提议使用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(例如参见here

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-12
      • 2010-12-26
      • 2023-01-28
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2012-09-29
      • 2013-11-13
      相关资源
      最近更新 更多