Facebook
From Speedy Water Vole, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 128
  1. from ncclient import manager
  2. with manager.connect(host=host, port=830,
  3. username=user, hostkey_verify=False) as m:
  4. c = m.get_config(source='running').data_xml
  5. with open("%s.xml" % host, 'w') as f:
  6. f.write(c)
  7.  
  8.  
  9. from ncclient import manager
  10. from ncclient.xml_ import *
  11.  
  12. def get_port_list(host, port, user, password):
  13.     conn = manager.connect(host=host,
  14.                             port=port,
  15.                             username=user,
  16.                             password=password,
  17.                             timeout=60,
  18.                             hostkey_verify=False)
  19.     criteria = """
  20.        <components xmlns="http://openconfig.net/yang/platform">
  21.            <component>
  22.                <state>
  23.                    <type>PORT</type>
  24.                </state>
  25.             <optical-port xmlns="http://openconfig.net/yang/transport-line-common"/>
  26.            </component>
  27.        </components>
  28.    """
  29.     filter = ("subtree", criteria)
  30.     result = conn.get(filter)
  31.  
  32.     return result
  33.  
  34. if __name__ == '__main__':
  35.     query = get_port_list('10.11.12.19', 830, 'admin', 'CHGME.1a')
  36.     print(query)