Newer
Older
from ncclient import manager
from ncclient.xml_ import *
import lxml.etree as ET
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import re
from typing import Optional, Union
from uuid import UUID, uuid4, uuid5
import logging
NAMESPACE_TFS = UUID('200e3a1f-2223-534f-a100-758e29c37f40')
print (uuid5(NAMESPACE_TFS, '1'))
def extract_roadm_ports (xml_data:str):
pattern = r'\bMG_ON_OPTICAL_PORT_WAVEBAND\b'
xml_bytes = xml_data.encode("utf-8")
root = ET.fromstring(xml_bytes)
with open('xml.log', 'w') as f:
print(xml_bytes, file=f)
namespace = {'oc': 'http://openconfig.net/yang/platform'}
ports = []
components = root.findall('.//oc:component',namespace)
print(f"component {components}")
for component in components:
properties = component.find(".//oc:properties",namespace)
if (properties is not None):
for property in properties :
value = property.find(".//oc:value",namespace)
if (re.search(pattern,value.text)):
name_element= component.find(".//oc:name",namespace)
print (f"name {name_element.text}")
def extract_ports_based_on_type (xml_data:str):
pattern = r':\s*PORT\b'
xml_bytes = xml_data.encode("utf-8")
root = ET.fromstring(xml_bytes)
namespace = {'oc': 'http://openconfig.net/yang/platform', 'typex': 'http://openconfig.net/yang/platform-types'}
ports = []
components = root.findall(".//oc:state[oc:type]",namespace)
for component in components:
type_ele = component.find(".//oc:type",namespace)
match = re.search(pattern, type_ele.text)
if match is not None :
name= component.find(".//oc:name",namespace)
print(name.text)
print("/////////////")
'host': '10.0.2.4', # IP address or hostname of the remote machine
'port': 2025, # SSH port (default: 22)
'username': 'admin', # SSH username
'password': 'admin', # SSH password
'device_params': {'name': 'default'},
'hostkey_verify':False,
"allow_agent":False
,"look_for_keys":False
}
def extract_value (xml_data):
xml_bytes = xml_data.encode("utf-8")
root = ET.fromstring(xml_bytes)
namespace = {'oc': 'http://openconfig.net/yang/platform',
'td': 'http://openconfig.net/yang/terminal-device'}
element = root.find('.//oc:component[oc:name="channel-4"]', namespace)
if element is not None:
parameter= element.find('.//td:frequency',namespace)
if (parameter is not None):
print(parameter.text)
else :
print("parameter is None")
else:
print(" element not found.")
def main () :
with manager.connect(**device) as m:
# Perform operations on the remote machine using the 'm' object
# For example, you can retrieve the running configuration:
#result =m.edit_config(target='running',config=edit_config)
running_config = m.get_config('running').data_xml
#extract_roadm_ports(running_config)
x,y= [1,3]
print (x)