Logic with Timeout嵌套SSH与Paramiko

Logic with Timeout嵌套SSH与Paramiko

问题描述:

所以我能够ssh到一个文本文件中存储的思科设备列表。它在设备启动并正常工作时效果很好,但如果由于无限循环超时而破坏,密码字段永远不会弹出。Logic with Timeout嵌套SSH与Paramiko

这与while循环的逻辑是什么在杀死我的思维过程。我知道我的解决方法是模糊的,但这很难测试,因为如果我弄错了,它会将我锁定在我的帐户之外,以便在我们的网络上执行此操作一段时间。任何帮助将非常感激。

目前代码:

import paramiko 
import time 
#***************************************** 
#SSH Credentials for logging into routers 
bass_host = 'ssh.server.com' 
ssh_username = 'username' 
ssh_password = 'password' 
port = 22 
#***************************************** 

ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

ssh.connect(bass_host, port, ssh_username, ssh_password) 
#For Intro Show Commands 
router_channel = ssh.invoke_shell() 
# Ssh and wait for the password prompt. 

hostnames = open('/hostlist.txt', 'r').readlines() 
hostnames = map(lambda s: s.strip(), hostnames) 


for device in hostnames: 
    command = 'ssh ' + device + '\n' 
    router_channel.send(command) 
    buff = '' 


    while not buff.endswith('Password: '): 
     resp = router_channel.recv(9999) 
     buff += resp 

    # Send the password and wait for a prompt. 
    router_channel.send('passwordhere\n') 
    buff = '' 

    while not buff.endswith('#'): 
     resp = router_channel.recv(99999) 
     buff += resp 

    router_channel.send('terminal length 0\n') 
    buff = '' 

    while not buff.endswith('#'): 
     resp = router_channel.recv(99999) 
     buff += resp 

关注的领域:

for device in hostnames: 
    command = 'ssh ' + device + '\n' 
    router_channel.send(command) 
    buff = '' 


    while not buff.endswith('Password: '): 
     resp = router_channel.recv(9999) 
     buff += resp 

    # Send the password and wait for a prompt. 
    router_channel.send('passwordhere\n') 
    buff = '' 

    while not buff.endswith('#'): 
     resp = router_channel.recv(99999) 
     buff += resp 

    router_channel.send('terminal length 0\n') 
    buff = '' 

尝试:

for device in hostnames: 
    command = 'ssh ' + device + '\n' 
    router_channel.send(command) 
    buff = '' 
    timeout = time.time() + 8*3 


    while not buff.endswith('Password: '): 
     resp = router_channel.recv(9999) 
     buff += resp 
     print 'broke' 
     if time.time() > timeout: 
      print 'Broke' 
      break 
      continue 
     else: 
      continue 

    # Send the password and wait for a prompt. 
    router_channel.send('passwordhere\n') 
    buff = '' 

    while not buff.endswith('#'): 
     resp = router_channel.recv(99999) 
     buff += resp 

    router_channel.send('terminal length 0\n') 
    buff = '' 
+0

相关信息:使用信号中断长时间运行的命令http://*.com/a/13587384/143880 – johntellsall 2014-08-31 20:41:01

SETT的命令的paramiko信道上的超时正是它修正的问题。

router_channel.settimeout(23) 


for device in hostnames: 
    try: 
     command = 'ssh ' + device + '\n' 
     router_channel.send(command) 
     buff = '' 
     timeout = time.time() + 8*3 


     while not buff.endswith('Password: '): 
      resp = router_channel.recv(9999) 
      buff += resp 
      print 'broke' 
      if time.time() > timeout: 
      print 'Broke' 
      break 
      continue 
      else: 
      continue 

     # Send the password and wait for a prompt. 
     router_channel.send('passwordhere\n') 
     buff = '' 

     while not buff.endswith('#'): 
      resp = router_channel.recv(99999) 
      buff += resp 

     router_channel.send('terminal length 0\n') 
     buff = '' 

except socket.timeout: 
    print 'connection failed to ' + jConnectID 
    continue