Python paramiko 脚本,在 exec_command() 期间读取输出的问题 [英] Python paramiko script, problems reading output during exec_command()

查看:390
本文介绍了Python paramiko 脚本,在 exec_command() 期间读取输出的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在使用 python 和 paramiko 来自动化我每次必须为课程提交程序时所经历的过程.我们使用名为handin"的命令提交源代码,但这必须在学校计算机上完成.所以当我在家提交代码时,我必须:sftp到学校服务器,把文件放在一个目录,ssh到学校电脑,使用'handin'命令

Background: I am using python and paramiko to automate the process I go through everytime I have to hand in a program for a class. We use a command called "handin" to submit source code, but this must be done from a school computer. So when I submit code from home, I have to: sftp into school servers, put the files in a dir, ssh into school computer, use 'handin' command

我可以成功地将文件放到学校机器上.当我尝试使用 exec_command('handin my files') 然后读取输出以确定下一步操作时会出现问题.

I can successfully put files onto the school machines. The problem occurs when I try to use exec_command('handin my files') and then read the output to determine the next action.

所以我有

try:
   (stdin, stdout, stderr) = client.exec_command(s)
except:
   print 'whoops'
   sys.exit()
print stdout.readlines()

但是由于某种原因这会导致死锁,脚本似乎什么都不做,我最终必须终止整个进程(ctrl+c 不起作用).我不确定 exec_command 是否没有正确完成(即使它脱离了 try/catch 块),或者我是否有网络问题或什么.

But this causes a deadlock for some reason, the script appears to be doing nothing and I have to eventually kill the entire process (ctrl+c doesnt work). Im not sure if exec_command is not completing correctly (even though it is getting out of the try/catch block) or if im having network problems or what.

有什么想法吗?

更新:

问题在于在执行过程中与handin 命令交互.执行该命令后,handin 可能仍在运行,也可能不在运行.如果它是第一次提交它说成功,等等等等,并完成执行.一切都很好.但如果我重新提交,我必须为每个文件授权覆盖 (stdin.write('y')).

The problem is with interacting with the handin command during execution. After executing the command, handin may or may not still be running. If its the first time submitting it says success, blah blah, and finishes executing. All is well. But if I am re-submitting I have to authorize an overwrite (stdin.write('y')) for each file.

TL/DR:

如何检查 exec_command() 是否仍在运行、等待输入,并相应地从 stdout 中检查 readline()?

How do I check if a exec_command() is still running, waiting for input, and readline() from stdout accordingly?

推荐答案

我看不出上面的代码片段有什么问题.下面的程序是一个完整的脚本,它登录到主机并运行 ls 命令来查看文件.我刚刚尝试过,它对我有用.也许试试这个,看看它是否适合你.如果它不起作用,我怀疑某些特定于您的 ssh 服务器、您正在运行的命令或 paramiko 安装的问题.如果它确实对您有用,那么只需对此进行更改以转向您现有的功能并查看它在哪里中断.

I don't see anything wrong with the code snippit above. The program below is a complete script which logs into a host and runs the ls command to view files. I just tried it and it works for me. Perhaps try this and see if it works for you. If it does not work, I suspect some issue specific to either your ssh server, command you are running, or paramiko installation. If it does work for you, its just a matter of making changes to this to move towards your existing functionality and see where it breaks.

import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<ip address here>',username='<username here>',password='<password here>')
stdin,stdout,stderr = ssh.exec_command("ls /")
print stdout.readlines()

如果这对您有用,我的下一个建议是尝试用您尝试运行的实际handin 命令替换ls/".可能是命令挂起等待用户输入等

If that works for you my next suggestion would be to try replacing the 'ls /' with the actual handin command you are trying to run. Its possible that command is hanging waiting for user input, etc.

这篇关于Python paramiko 脚本,在 exec_command() 期间读取输出的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆