使用Paramiko在嵌套SSH中实现带超时的逻辑

0 投票
1 回答
1030 浏览
提问于 2025-04-19 02:57

我可以通过SSH连接到存储在文本文件中的一系列思科设备。当设备正常工作时,这个过程非常顺利,但如果设备因为密码输入框一直不弹出而导致超时,就会出现问题。

我在使用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 = ''

1 个回答

1

设置通道超时的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

撰写回答