当前位置:   article > 正文

python ssh 连接交换机并执行命令并输出结果_paramiko连接交换机正常执行命令停止不动弹

paramiko连接交换机正常执行命令停止不动弹
  1. # encoding=utf-8
  2. import paramiko
  3. import time
  4. client = paramiko.SSHClient()
  5. client.load_system_host_keys()
  6. # connect to client
  7. client.connect('192.168.254.141',22,'test','test',allow_agent=False,look_for_keys=False)
  8. # get shell
  9. ssh_shell = client.invoke_shell()
  10. # ready when line endswith '>' or other character
  11. while True:
  12. line = ssh_shell.recv(1024)
  13. #print line
  14. if line and line.endswith('>'):
  15. break;
  16. # send command
  17. ssh_shell.sendall( 'ping 192.168.254.142' + '\n')
  18. # get result lines
  19. lines = []
  20. while True:
  21. line = ssh_shell.recv(1024)
  22. if line and line.endswith('>'):
  23. break;
  24. lines.append(line)
  25. result = ''.join(lines)
  26. # print result
  27. print result

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/863314
推荐阅读
相关标签
  

闽ICP备14008679号