【发布时间】:2012-09-28 17:08:51
【问题描述】:
我正在尝试编写一个脚本来登录到远程机器,运行命令并返回输出。我正在使用 paramiko 库在 python 中执行此操作。然而,由于某种原因,没有产生完整的输出,只有一行。
为了隔离问题,我创建了一个名为 simple 的本地脚本,它运行命令并将输出发送到文件 remote_test_output.txt。然后我只是简单地 sftp 文件。该文件仅包含相同的一行。唯一的一行输出每次都是一样的:命令的响应码。
当我手动完成所有这些操作(ssh over、登录并运行 ./simple)时,一切都按预期工作,并且输出文件是正确的。但是,通过我机器上的脚本执行此操作,它只返回单行。
我的代码:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(host, username, password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('LD_LIBRARY_PATH=/opt/bin ./simple\n')
print "output:", ssh_stdout.read()+"end" #Reading output of the executed command
print "err:", ssh_stderr.read()#Reading the error stream of the executed command
sftp = ssh.open_sftp()
sftp.get('remote_test_output.txt', 'local_test_output.txt')
sftp.close()
返回什么:
response code: 128
应该返回什么:
field1:value1
field2:value2
response code: 128
field3:value3
field4:value4
etc
有没有人知道为什么我试图调用的命令没有正常输出?
我必须包含 LD_LIBRARY_PATH 变量分配,否则我会收到库不存在错误。
【问题讨论】:
-
也许某处的重定向不完整?你试过2>&1吗? ./simple 在做什么,./simple 运行的基本命令是什么?
-
@Dubslow 这是一个获取状态命令。它获取另一台机器的状态、温度等。我知道重定向不是问题,因为如果我 ssh 并通过终端运行它,它可以正常工作