Newer
Older
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
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
57
58
59
60
61
62
63
edit_config_t = '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<terminal-device xmlns="http://openconfig.net/yang/terminal-device">
<logical-channels>
<channel>
<index>1</index>
<config>
<admin-state>DISABLED</admin-state>
</config>
</channel>
</logical-channels>
</terminal-device>
</config>
'''
edit_config_r_media_channel = '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<wavelength-router xmlns="http://openconfig.net/yang/wavelength-router">
<media-channels >
<channel operation="delete" >
<index>1</index>
<config>
<index>1</index>
</config>
</channel>
</media-channels>
</wavelength-router>
</config>
'''
edit_config_r_optical_band = '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<wavelength-router xmlns="http://openconfig.net/yang/wavelength-router">
<optical-bands xmlns="http://flex-scale-project.eu/yang/flex-scale-mg-on" >
<optical-band >
<index>1</index>
<config>
<index>1</index>
<admin-status>DISABLED</admin-status>
</config>
</optical-band>
</optical-bands>
</wavelength-router>
</config>
'''
'host': '10.0.2.4', # IP address or hostname of the remote machine
'username': 'admin', # SSH username
'password': 'admin', # SSH password
'device_params': {'name': 'default'},
'hostkey_verify':False,
"allow_agent":False
,"look_for_keys":False
}
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)
# # 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'))