我正在尝试连接到Cisco路由器以获取一些信息,并且在运行代码时收到以下错误:
回溯(最近通话最近): 在第72行中输入文件“ ./cisco_auto_back_up_v4.py” 重新分发(net_connect,device_type = target_device ['device_type']) 重新分发中的文件“ /home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py”,第208行 obj.session_preparation() 在session_preparation中的第16行,文件“ /home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/cisco/cisco_ios.py” self._test_channel_read(pattern = r'[>#]') _test_channel_read中的第791行“ /home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py” new_data + = self._read_channel_timing() _read_channel_timing中的第494行的文件“ /home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py” new_data = self.read_channel() 文件“ /home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py”,第395行,位于read_channel中 输出= self._read_channel() _read_channel中的文件“ /home/ipautowppprod/.pyenv/versions/3.6.5/lib/python3.6/site-packages/netmiko/base_connection.py”,第373行 如果self.remote_conn.recv_ready(): AttributeError:“ NoneType”对象没有属性“ recv_ready”
这是我的代码:
#!/home/ipautowppprod/.pyenv/shims/python
# cisco_auto_back_up_v4
from netmiko import ConnectHandler, redispatch
from netmiko import NetMikoAuthenticationException, NetMikoTimeoutException
import datetime
import sys
import time
now = datetime.datetime.now()
time_now = now.strftime("%Y-%m-%d_%H:%M:%S")
target_info = sys.argv[1].split(',')
ipmon_info = sys.argv[2].split(',')
target_info = [x.strip() for x in target_info]
ipmon_info = [x.strip() for x in ipmon_info]
target_device = {
'device_type': 'cisco_ios',
'ip': target_info[0],
"host": target_info[1],
'username': target_info[2],
'password': target_info[3],
'secret': target_info[4]
}
ipmon = {
'device_type': 'linux',
'ip': ipmon_info[0],
'username': ipmon_info[1],
'password': ipmon_info[2]
}
try:
print("Attempting to Connect...")
# Connect to ipmon
net_connect = ConnectHandler(**ipmon)
print(net_connect.find_prompt())
net_connect.write_channel("ssh {}@{}\n".format(target_device["username"],
target_device["ip"]))
time.sleep(1)
output = net_connect.read_channel()
print(output)
if "RSA" in output:
net_connect.write_channel("yes\n")
time.sleep(1)
output = net_connect.read_channel()
print(output)
if "user" in output:
net_connect.write_channel(target_device["username"] + "\n")
time.sleep(1)
output = net_connect.read_channel()
if "password" in output:
net_connect.write_channel(target_device["password"] + "\n")
time.sleep(5)
output = net_connect.read_channel()
if "password" in output:
print("Wrong credentials.")
sys.exit(1)
elif "refused" in output:
net_connect.disconnect()
print("Connection Refused")
else:
net_connect.disconnect()
# Connect to device
redispatch(net_connect, device_type=target_device['device_type'])
CMD = net_connect.send_command_timing('show running-config')
# Converts CMD output to a string
cmd_output = str(CMD.stdout)
except NetMikoAuthenticationException as autom_err:
print(autom_err)
sys.exit(1)
except NetMikoTimeoutException as timeout_err:
print(timeout_err)
sys.exit(1)
请告知。
预先感谢
答案 0 :(得分:0)
您可能想使用调试器逐步执行代码,但是当您进入redispatch调用时,您的net_connect对象似乎不再连接(即net_connect.remote_conn已设置为None)。
这使我认为有可能调用了elif或else子句中的disconnect()。在这种情况下,您可能应该引发一个异常(或sys.exit()),因为如果发生任何一种情况,程序将无法继续执行。