簡體   English   中英

使用Paramiko的嵌套SSH中的超時邏輯

[英]Logic with Timeout in Nested SSH with Paramiko

這樣我就可以將存儲在文本文件中的Cisco設備列表放入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 = ''

設置通道超時的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM