【问题标题】:PHP or Python on windows send command to remote Linux stackWindows 上的 PHP 或 Python 向远程 Linux 堆栈发送命令
【发布时间】:2019-06-06 01:02:24
【问题描述】:

我正在开发一个 Web 应用程序来将 G 代码发送到 3dprinter。这个 3dprinter 有一个 Linux 堆栈。我可以使用以下命令在 Win 上的 Ubuntu 应用程序中实现它

ssh root@ip password python3 /usr/share/griffin/command_util.py sendgcode G28

输入python3 /usr/share/griffin/command_util.py后, 它将返回(Cmd) type command here

我尝试在 PHP 中使用 ssh2,连接和身份验证部分工作。此外,list,cd \usr\share\griffin && list 也返回与我在 windows cmd 中得到的结果相同的结果。但是在下面的代码中,在linux stack中运行py文件后,只返回(Cmd)sendgcode G28 没有任何反应。

<?php
set_include_path('C:/xampp/htdocs/phpseclib1.0.15');
include('Net/SSH2.php');

$ssh = new Net_SSH2('ip');
if (!$ssh->login('root', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('python3 /usr/share/griffin/command_util.py && sendgcode G28');
?>

以下 Python 代码不会收到任何返回值

# 
import paramiko
# realize transport
trans = paramiko.Transport(('ip', 22))
# connection
trans.connect(username='root', password='ultimaker')

# Specify the transport of the sshclient object as the above trans
ssh = paramiko.SSHClient()
ssh._transport = trans
# execute command
stdin, stdout, stderr = ssh.exec_command('python3 /usr/share/griffin/command_util.py \n sendgcode G28')
print(stdout.read().decode())

# close connection
trans.close()

Linux 堆栈显示 sendgcode 命令未记录在案。我不明白为什么我可以在 Windows cmd 中使用它,但不能在 PHP 或 Python 中使用它。任何帮助将不胜感激!

【问题讨论】:

    标签: php python ssh ssh2-exec


    【解决方案1】:

    只需删除 PHP 中的 '&&' 或 Python 中的 '\n' 即可

    【讨论】:

      猜你喜欢
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 2023-03-20
      • 1970-01-01
      • 2013-08-16
      • 2022-06-30
      相关资源
      最近更新 更多