python - "ping"不允许执行

标签 python paramiko

当我尝试使用 Paramiko 执行我得到的任何命令时

"[COMMAND]" isn't allowed to be executed.

但是如果我通过使用 Putty 来做到这一点,它工作正常,知道是什么原因造成的吗?

Paramiko :

>>>ssh.connect('server',port=22,username='user',password='pass'
>>>stdin,stdout,stderr = ssh.exec_command('ping 8.8.8.8 -c 2')
>>>output = stdout.readlines()
>>>print output
[]
>>>error = stderr.readlines()
>>>print error
>>>u'"ping" isn\'t allowed to be executed.\n'

腻子:

user@server:~$ ping 8.8.8.8 -c 2
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=57 time=15.928 ms
64 bytes from 8.8.8.8: seq=1 ttl=57 time=15.661 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 15.661/15.794/15.928 ms

最佳答案

Paramiko 的 exec_command(cmd) 将调用 /the/login/shell -c cmd 来运行类似于 ssh user@host cmd。如果远程服务器上的登录 shell 不支持 -c,则 exec_command() 将失败。因此,在使用 exec_command() 之前,我通常先从命令行尝试 ssh user@host cmd

invoke_shell() 会起作用,因为它会启动一个交互式 session ,就像您使用 PuTTY 手动连接到服务器一样。

关于python - "ping"不允许执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41570124/

相关文章:

python - 在 bash 脚本中嵌入 python

python - 将 Python 二进制模块安装到 Windows 中的自定义位置

python - Paramiko 在 Windows 上无法从 ssh-agent 找到 key

Python3.6 import paramiko卡住了

python - 使用 paramiko 执行 dd 命令但没有任何返回

python - 如何在 Nuke 脚本中找到所有 ReadGeo 节点?

python - 2组坐标之间的线性平移

python - Tkinter 索引词问题

python - 无法安装python模块

python - Python 中的连接顺序错误并覆盖部分字符串