Last active
December 10, 2017 03:55
-
-
Save qiuchengxuan/f03d55fddcc9aef040ae798a62db52b1 to your computer and use it in GitHub Desktop.
Access H3C switch via NETCONF with python script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import code | |
from lxml import etree | |
from lxml.builder import E | |
from ncclient import manager | |
_, ip, user, passwd = sys.argv | |
netconf = manager.connect(host=ip, port=830, username=user, password=passwd, | |
hostkey_verify=False, allow_agent=False, | |
look_for_keys=False) | |
IETF_XMLNS_BASE = 'urn:ietf:params:xml:ns:netconf:base:1.0' | |
H3C_NSMAP = {'xmlns': 'http://www.h3c.com/netconf/data:1.0'} | |
def vlan_filter(vid): | |
return E.VLAN(E.VLANs()) | |
def ifmgr_filter(): | |
return E.Ifmgr(E.Interfaces(E.Interface(E.IfIndex(), E.Name()))) | |
def device_filter(): | |
return E.Device(E.Base(E.HostName(), E.LocalTime(), E.Uptime())) | |
def netconf_get(tree): | |
xml = etree.tostring(E.top(tree, H3C_NSMAP)) | |
print xml | |
return netconf.get(('subtree', xml)).xml | |
def netconf_edit(*elements): | |
xml_config = etree.tostring(E.config(*elements, xmlns=IETF_XMLNS_BASE)) | |
print xml_config | |
return netconf.edit_config(target='running', config=xml_config) | |
code.interact(local=locals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment