免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1621 | 回复: 0
打印 上一主题 下一主题

我用select 处理paramiko的stdout为什么会出错 [复制链接]

论坛徽章:
13
CU大牛徽章
日期:2013-03-14 14:14:082016科比退役纪念章
日期:2016-07-22 11:15:35数据库技术版块每日发帖之星
日期:2016-05-27 06:20:002015亚冠之吉达阿赫利
日期:2015-08-05 10:06:542015年亚洲杯之韩国
日期:2015-04-01 16:05:42双鱼座
日期:2014-11-13 11:04:24丑牛
日期:2014-07-25 17:29:54子鼠
日期:2014-04-25 12:25:45丑牛
日期:2014-04-17 08:35:48巨蟹座
日期:2014-04-16 16:50:05CU大牛徽章
日期:2013-03-14 14:14:29CU大牛徽章
日期:2013-03-14 14:14:26
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-11-18 21:06 |只看该作者 |倒序浏览
本帖最后由 hmchzb19 于 2016-11-18 21:09 编辑

代码在这里:
  1. ssh = paramiko.SSHClient()
  2. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  3. ssh.connect('192.168.0.199',username='root',password='passw0rd')

  4. output=""
  5. err_str=""
  6. stdin, stdout, stderr=ssh.exec_command("/usr/bin/cp /root/names.txt /tmp/names.txt.before; echo good bye; sleep 1; ls -al /dev",)
  7. try:
  8.     while not stdout.channel.exit_status_ready():
  9.         rl, wl, xl = select.select([stdout.channel,], [], [], 0.0)
  10.         if len(rl) != 0:
  11.             output+=stdout.channel.recv(512).decode()
  12.             err_str+=stdout.channel.recv_stderr(512).decode()
  13. except socket.timeout:
  14.     print("Operation time out. Ouput might not be complete.")

  15. rc=stdout.channel.recv_exit_status()
  16. print(rc)
  17. print(output)
  18. print(err_str)
复制代码

这段代码经过我的测试,我发现只会输出一部分/dev/下面的文件,不知道为什么会这样,请高手指点。 主要是while not stdout.channel.exit_status_ready():
        rl, wl, xl = select.select([stdout.channel,], [], [], 0.0)
        if len(rl) != 0:
            output+=stdout.channel.recv(512).decode()
            err_str+=stdout.channel.recv_stderr(512).decode()

这里的代码我感觉不知道怎么写。


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP