Cisco 路由器上的 Python Yang

问题描述

我有一台装有 Cisco XE 的 Cisco CSR 路由器。

我想显示路由器的 YANG 功能

我的代码如下:

main.py

from ncclient import manager
import xml.etree.ElementTree as ET

router = {
    "host": "10.0.0.1","port": "830","username": "admin","password": "******",}

with manager.connect(
    host=router["host"],port=router["port"],username=router["username"],password=router["password"],hostkey_verify=False,allow_agent=False,look_for_keys=False,) as m:
    ip_schema = m.get_schema("ietf-ip")
    root = ET.fromstring(ip_schema.xml)
    yang_tree = list(root)[0].text
    f = open("ietf-ip.yang","w")
    f.write(yang_tree)
    f.close()

另外,请找到我在路由器上运行的节目:

MyRouter#show run
Building configuration...

Current configuration : 1725 bytes
!
! Last configuration change at 10:17:15 UTC march Oct 6 2021
!
version 16.8
service timestamps debug datetime msec
service timestamps log datetime msec
platform qfp utilization monitor load 80
no platform punt-keepalive disable-kernel-core
platform console serial
!
hostname MyRouter
!
boot-start-marker
boot-end-marker
!
!
enable secret 5 $1$tK3F$2WEFfM8JZcyP7YPgzRZbH1
!
aaa new-model
!
!
aaa authentication login default local
aaa authorization exec default local
!
!
!
!
!
aaa session-id common
!
!
!
!
!
!
!
ip domain name myrouter.com
!
!
!
!
!
!
!
!
!
!
subscriber templating
!
!
!
!
!
!
!
multilink bundle-name authenticated
!
!
!
!
!
!
!
!
!
!
!
!
!
!
license udi pid CSR1000V sn 96YNK5Y0HUS
no license smart enable
diagnostic bootup level minimal
!
spanning-tree extend system-id
!
netconf-yang
!
!
username admin privilege 15 secret 5 $1$h/2L$vvzIggHKLFlS1KJlYOqkA1
!
redundancy
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
interface GigabitEthernet1
ip address 10.0.0.1 255.255.255.0
ip nat outside
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet2
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet3
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
!
interface GigabitEthernet4
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
!
ip forward-protocol nd
ip http server
ip http authentication local
ip http secure-server
ip route 0.0.0.0 0.0.0.0 10.0.0.3
!
ip ssh version 2
!
!
!
!
!
!
!
control-plane
!
!
!
!
!
!
line con 0
stopbits 1
line vty 0 4
transport input ssh
!
netconf ssh
wsma agent exec
!
wsma agent config
!
wsma agent filesys
!
wsma agent notify
!
!
end

当我运行代码时,它给出了一个错误`ncclient.transport.errors.SSHError:无法打开套接字到 10.0.0.1:830

知道是什么原因造成的吗?

解决方法

首先您需要确定您的路由器是否启用了 netconf。

您只需从您的机器发出 SSH 命令 telnet 10.0.0.1 830 即可。

如果telnet连接成功,说明你python中缺少SSH模块。

您可以通过使用命令 pip install paramiko

安装 Paramiko 来解决此问题

如果 telnet 失败,则只需使用命令 netconf-yang 在 Cisco 路由器上启用 netconf 并重试。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...