没有从 parmiko/ssh 命令获取输出
我正在使用 paramiko/ssh/python 尝试在远程服务器上运行命令。当我手动 ssh 并运行相关命令时,我得到了我想要的结果。但是如果我使用下面的python(从本网站的另一个线程中选择),则没有返回的数据。如果我将命令修改为更基本的命令,例如“pwd”或“ls”,我就可以获得输出。任何帮助表示赞赏。
谢谢, 马特
import paramiko
import time
import sys, os, select
import select
hostname='10.15.27.166'
hostport=22
cmd='tail -f /x/web/mlog.txt' #works
#cmd='customexe -args1 -args2' #doesn't work
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(hostname=hostname, username=username, password=password)
transport = client.get_transport()
channel = transport.open_session()
channel.exec_command(cmd)
while True:
rl, wl, xl = select.select([channel],[],[],0.0)
if len(rl) > 0:
# Must be stdout
print channel.recv(1024)
time.sleep(1)
I am using paramiko/ssh/python to attempt to run a command on a remote server. When I ssh manually and run the command in question, I get the results I want. But if I use the python (co-opted from another thread on this site) below, there is no returned data. If I modify the command to be something more basic like 'pwd' or 'ls' I can then get the output. Any help is appreciated.
Thanks,
Matt
import paramiko
import time
import sys, os, select
import select
hostname='10.15.27.166'
hostport=22
cmd='tail -f /x/web/mlog.txt' #works
#cmd='customexe -args1 -args2' #doesn't work
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(hostname=hostname, username=username, password=password)
transport = client.get_transport()
channel = transport.open_session()
channel.exec_command(cmd)
while True:
rl, wl, xl = select.select([channel],[],[],0.0)
if len(rl) > 0:
# Must be stdout
print channel.recv(1024)
time.sleep(1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个修复,但不一定是根本原因:当 paramiko 创建 ssh 连接时,它没有在远程服务器上的主目录中运行我的 bash_profile。因此,我将命令从 bash_profile 复制到 cmd 变量中,从而加载了我认为会自动加载的各种环境变量。然后命令“customexe ...”按预期返回输出。
I found a fix, though not necessarily the root cause: When paramiko created the ssh connection, it did not run my bash_profile in my home directory on the remote server. So, I copied the commands from the bash_profile into the cmd variable and thus loaded various environment variables that I thought would have loaded automatically. Then the command "customexe ..." returned output as expected.