Skip to content
Snippets Groups Projects
test.py 3.17 KiB
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




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
    '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'))