我阅读了“python 网络编程”,并了解了netmiko。我尝试连接到书中给出的 Cisco 网络,但它没有连接。我上网阅读了netmiko上的其他文章,并使用书中给出的路由器登录详细信息尝试了他们的 Examples,但它们都不起作用。
给我的错误如下:
possible reasons why a connection cannot be established are as follows:
*Wrong hostname
*Wrong TCP/IP port
*Wrong password
*Blocked access to router
或者如果你有任何免费的路由器,我可以连接到只是为了学习,我想知道。
如果您没有直接的 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.SSH()
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")
我不知道您正在尝试哪个示例脚本。但是根据您的错误,看起来设备连接存在问题,您可以尝试 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)
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(53条)