from ncclient import manager
from ncclient.xml_ import *
import lxml.etree as ET
import re
from typing import Optional, Union
from uuid import UUID, uuid4, uuid5
import logging
edit_config_t = '''
1
DISABLED
'''
edit_config_r_media_channel = '''
1
1
'''
edit_config_r_optical_band = '''
1
1
DISABLED
'''
device = {
'host': '10.0.2.4', # IP address or hostname of the remote machine
'port': 2026, # 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_status (xml_data:str,index:int):
xml_bytes = xml_data.encode("utf-8")
root = ET.fromstring(xml_bytes)
namespaces = { "td": "http://openconfig.net/yang/terminal-device"}
channels = root.findall(f".//td:terminal-device/td:logical-channels/td:channel",namespaces)
for channel in channels :
index_ele= channel.find(f".//td:config[td:index='{index}']/td:admin-state",namespaces)
if index_ele is not None :
print(index_ele.text)
if __name__ == '__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_r_optical_band)
#print(result,file=open("context.log",'w'))
running_config = m.get_config('running').data_xml
# extract_status(running_config,index=1)
print(running_config,file=open("xml.log",'w'))