首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python网络编程

Python网络编程
EN

Stack Overflow用户
提问于 2021-08-25 20:04:07
回答 2查看 159关注 0票数 0

我读了"python网络编程“,了解了netmiko。我试着连接到书中给出的思科网络,但它没有连接。我上网阅读了关于netmiko的其他文章,并使用书中给出的路由器登录详细信息尝试了他们的示例,但没有一篇有效。我的网络连接很好(我查过了)。拜托,出什么事了?这是我的位置(我住在尼日利亚)吗?

给我的错误如下:

代码语言:javascript
复制
possible reasons why a connection cannot be established are as follows:
*Wrong hostname
*Wrong TCP/IP port
*Wrong password
*Blocked access to router

拜托,怎么了,我需要帮助。或者,如果您有任何免费路由器,我可以连接到,只是为了学习,我想知道。

EN

回答 2

Stack Overflow用户

发布于 2022-04-03 14:49:53

如果您没有直接ssh权限,并且希望通过私有服务器进行连接,您也可以尝试paramiko而不是netmiko。下面,我编写了一个通过服务器连接并通过键入命令进入设备的示例。

代码语言:javascript
复制
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")
票数 1
EN

Stack Overflow用户

发布于 2021-12-14 08:55:45

我不知道你在尝试哪个样例脚本。但是,根据您的错误,设备连接似乎存在问题,您可以尝试ping设备并检查ssh是否已启用。

尝试使用您的凭据运行下面的脚本,它应该可以工作。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68929211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档