免费注册 查看新帖 |

Chinaunix

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

windows上使用paramiko模拟ssh执行命令问题 [复制链接]

求职 : 其它语言研发
论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-05-18 00:32 |只看该作者 |倒序浏览
  1. #!/usr/bin/env python3
  2. #-*- coding:utf-8 -*-
  3. #stop dpat

  4. import paramiko
  5. import time

  6. username='username'
  7. password='password'
  8. ip='192.168.1.1'

  9. #建立连接
  10. ssh = paramiko.SSHClient()
  11. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  12. ssh.connect(hostname=ip, port=22, username=username, password=password,timeout=10)
  13. #打印分隔符
  14. print('  Server: ' + ip)

  15. stdin, stdout, stderr = ssh.exec_command("jstorm list|grep -A 1 dpat-test|grep status")
  16. test_name = stdout.readline()
  17. print(test_name)


  18. #stop dpat-test,while循环直到状态由ACTIVE变为KILLED
  19. while('ACTIVE' in test_name):
  20.     time.sleep(1)
  21.     #一直想不通执行该命令后为什么没有效果,尝试使用while循环去一遍遍执行,
复制代码
stdin, stdout, stderr = ssh.exec_command('cd /run/ayena/dpat/;sh stop-dpat.sh')
这样代码执行总是没有效果,即时更改为
stdin, stdout, stderr = ssh.exec_command('cd /run/ayena/dpat/;sh stop-dpat.sh',timeout=10)
也没有效果,感觉timeout=10在这里没有效果
大家有什么建议吗,能看出来哪里有问题?


版本信息:
win7
python3.5
D:\python3.5\Lib\site-packages\paramiko 2.1.1

论坛徽章:
0
2 [报告]
发表于 2017-05-18 07:04 |只看该作者
print stderr

求职 : 其它语言研发
论坛徽章:
0
3 [报告]
发表于 2017-05-18 09:34 |只看该作者
799029078 发表于 2017-05-18 07:04
print stderr

print(stderr.read())是空值

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-10-11 06:20:0015-16赛季CBA联赛之山东
日期:2016-05-28 18:18:5615-16赛季CBA联赛之新疆
日期:2017-04-12 22:55:4715-16赛季CBA联赛之青岛
日期:2017-06-26 18:30:0315-16赛季CBA联赛之四川
日期:2017-09-04 12:27:0315-16赛季CBA联赛之福建
日期:2018-02-09 14:28:3315-16赛季CBA联赛之同曦
日期:2018-04-17 12:43:3415-16赛季CBA联赛之浙江
日期:2018-07-14 13:27:4015-16赛季CBA联赛之吉林
日期:2018-09-13 15:48:2915-16赛季CBA联赛之新疆
日期:2016-05-07 05:05:3215-16赛季CBA联赛之八一
日期:2016-03-14 12:32:06程序设计版块每日发帖之星
日期:2015-12-12 06:20:00
4 [报告]
发表于 2017-05-18 19:01 |只看该作者
test_name = stdout.read()

求职 : 其它语言研发
论坛徽章:
0
5 [报告]
发表于 2017-05-18 21:58 |只看该作者
baby_神 发表于 2017-05-18 19:01
test_name = stdout.read()

有什么区别?和readline

求职 : 其它语言研发
论坛徽章:
0
6 [报告]
发表于 2017-05-18 22:48 |只看该作者
本帖最后由 killedman 于 2017-05-18 22:53 编辑
  1. #!/usr/bin/env python3
  2. #-*- coding:utf-8 -*-
  3. #stop dpat

  4. import paramiko
  5. import time

  6. username='username'
  7. password='password'
  8. ip='192.168.1.1'

  9. #建立连接
  10. ssh = paramiko.SSHClient()
  11. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  12. ssh.connect(hostname=ip, port=22, username=username, password=password,timeout=10.0)
  13. #打印分隔符
  14. print('  Server: ' + ip)

  15. stdin, stdout, stderr = ssh.exec_command("jstorm list|grep -A 1 dpat-test|grep status")
  16. test_name = stdout.readline()
  17. print(test_name)


  18. #stop dpat-test,while循环直到状态由ACTIVE变为KILLED
  19. #while('ACTIVE' in test_name):
  20. if('ACTIVE' in test_name):
  21.     print(time.asctime(time.localtime(time.time())))
  22.     stdin, stdout, stderr = ssh.exec_command('cd /run/ayena/dpat/;sh stop-dpat.sh')
  23.     #神奇的5秒钟,上一条命令添加timeout参数不知道为什么不起作用
  24.     time.sleep(5)
  25.     print(time.asctime(time.localtime(time.time())))
  26.     stdin, stdout, stderr = ssh.exec_command('jstorm list|grep -A 1 dpat-test|grep status')
  27.     print(stderr.read())
  28.     test_name = stdout.readline()
  29.     print(test_name)

  30. print("begin  stoping run-dpat: "  + time.asctime(time.localtime(time.time())))
  31. while('KILLED' in test_name):
  32.     time.sleep(10)
  33.     stdin, stdout, stderr = ssh.exec_command('jstorm list|grep -A 1 dpat-test|grep status')
  34.     test_name = stdout.readline()
  35. if test_name is '':
  36.     print(" end  stoping dpat-test : "  + time.asctime(time.localtime(time.time())))
  37.     print('stop dpat-test process success')
  38. #关闭连接
  39. ssh.close()
复制代码

求职 : 其它语言研发
论坛徽章:
0
7 [报告]
发表于 2017-05-18 22:50 |只看该作者
回复 6# killedman
    #神奇的5秒钟,上一条命令添加timeout参数不知道为什么不起作用
    time.sleep(5)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP