【问题标题】:Logic with Timeout in Nested SSH with Paramiko使用 Paramiko 的嵌套 SSH 中的超时逻辑
【发布时间】:2014-10-25 02:20:30
【问题描述】:

所以我可以通过 ssh 进入存储在文本文件中的 cisco 设备列表。当设备启动并正常运行时,它工作得很好,但如果它由于无限循环而超时,当密码字段从不弹出时发生。

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 = ''

【问题讨论】:

标签: python ssh timeout nested paramiko


【解决方案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

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2010-12-27
    • 2020-12-11
    • 2017-08-27
    • 2017-07-29
    • 2011-07-21
    相关资源
    最近更新 更多