【发布时间】: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 = ''
【问题讨论】:
-
相关:使用信号中断长时间运行的命令stackoverflow.com/a/13587384/143880
标签: python ssh timeout nested paramiko