我读了"python网络编程“,了解了netmiko。我试着连接到书中给出的思科网络,但它没有连接。我上网阅读了关于netmiko的其他文章,并使用书中给出的路由器登录详细信息尝试了他们的示例,但没有一篇有效。我的网络连接很好(我查过了)。拜托,出什么事了?这是我的位置(我住在尼日利亚)吗?
给我的错误如下:
possible reasons why a connection cannot be established are as follows:
*Wrong hostname
*Wrong TCP/IP port
*Wrong password
*Blocked access to router拜托,怎么了,我需要帮助。或者,如果您有任何免费路由器,我可以连接到,只是为了学习,我想知道。
发布于 2022-04-03 14:49:53
如果您没有直接ssh权限,并且希望通过私有服务器进行连接,您也可以尝试paramiko而不是netmiko。下面,我编写了一个通过服务器连接并通过键入命令进入设备的示例。
import paramiko
import time
from termcolor import colored
from timeit import default_timer as timer
import sys
global password
username = "ldap"
password = "pwd"
def vrf_implement_control(device_ip):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
remote_connection = ssh.connect(device_ip, port="2222", username=username, password=password, timeout=15)
remote_connection = ssh.invoke_shell()
# device_connection
remote_connection.send(" 192.168.0.10\n")
time.sleep(2)
print (colored ("connected_ip_address_"+ device_ip,"blue"))
except Exception as e:
print("erisim yok_\n")发布于 2021-12-14 08:55:45
我不知道你在尝试哪个样例脚本。但是,根据您的错误,设备连接似乎存在问题,您可以尝试ping设备并检查ssh是否已启用。
尝试使用您的凭据运行下面的脚本,它应该可以工作。
from netmiko import ConnectHandler
cisco = {
'device_type': 'cisco_ios', #refer netmiko device type
'host': '10.10.1.1', #Replace with your device ip
'username': 'admin', #Router username
'password': 'cisco123', #password
}
net_connect = ConnectHandler(**cisco)
output = net_connect.send_command("show version")
print(output)https://stackoverflow.com/questions/68929211
复制相似问题