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)
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.
精彩评论