Automation

 View Only
last person joined: 6 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Help about Python script for fxp0 ip recovery.

    Posted 06-30-2020 08:19

    Hi all,

    I wrote a script that saves the configuration of an mx960 on a remote server when an operator gives the command "op copyconf.py".

    I would like the script to automatically take the ip of the master fxp0 from the local configuration and then be able to use it as the scp source.

    I can't understand which method to use ... in all the documentation I read about Pyez, but I can't get it to work on the local configuration. I suppose it works only  for remote connections or am I wrong?

    simone@LAB-RE0> show configuration groups re0 interfaces fxp0 unit 0 family inet | display xml 
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/19.1R0/junos">
        <configuration junos:commit-seconds="1593435526" junos:commit-localtime="2020-06-29 14:58:46 CEST" junos:commit-user="simone">
                <groups>
                    <name>re0</name>
                    <interfaces>
                        <interface>
                            <name>fxp0</name>
                            <unit>
                                <name>0</name>
                                <family>
                                    <inet>
                                        <address>
                                            <name>192.168.23.23/28</name>
                                        </address>
                                        <address>
                                            <name>192.168.23.25/28</name>
                                            <master-only/>
                                        </address>
                                    </inet>
                                </family>
                            </unit>
                        </interface>
                    </interfaces>
                </groups>
        </configuration>
        <cli>
            <banner>{master}</banner>
        </cli>
    </rpc-reply>

    I'm interested in taking the IP 192.168.23.25 and putting it in a variable.

    Thanks for help.


    #Python


  • 2.  RE: Help about Python script for fxp0 ip recovery.

    Posted 06-30-2020 08:28

    Hi simonev, 

     

    Greetings, 

    To answer your first part, how to fetch the fxp IP you could use something like below : 

    from jnpr.junos import Device
    from lxml import etree
    
    with Device(host='router.example.com', use_filter=True) as dev:
        sax_input = '<interface-information><physical-interface><name/></physical-interface></interface-information>' >>Use relevant xml tag, address in our case.
        result = dev.rpc.get_interface_information(filter_xml=sax_input) 
        print (etree.tostring(reply, encoding='unicode'))

    Link: https://www.juniper.net/documentation/en_US/junos-pyez/topics/task/program/junos-pyez-xml-parser-specifying.html

     

    Hope this helps. Smiley Happy
    Mark "Accept as solution" if this answers your query.  Kudos are appreciated too! 

     

    Regards, 

    Sharat Ainapur


    #pyez
    #Python
    #tag
    #xml


  • 3.  RE: Help about Python script for fxp0 ip recovery.

    Posted 07-03-2020 08:49

    HI Sharat,

    I had to adapt and change my target. Now in my script I have: 

     

    root@LAB-RE0:/var/db/scripts/op # more copyconf.py
    from jnpr.junos import Device
    from lxml import etree
    import jxmlease
    import json
    from junos import Junos_Configuration
    import jcs
    
    def main():
       dev = Device()
        dev.open()
    
        filter = '<system><ntp><source-address></source-address></ntp></system>'            
        data = dev.rpc.get_config(filter_xml=filter, options={'format':'set'}) 
        print (etree.tostring(data, encoding='unicode', pretty_print=True))  
        dev.close()
    
    
    if __name__ == "__main__":
        main()
    	

    when I run it I have this result:

    root@LAB-RE0> op copyconf.py    
    <configuration-set>
    set system ntp source-address 192.168.23.25
    </configuration-set>
    

    OK, now How can I extrapolate only the ip '192.168.23.25' and insert it in a variable?

     

    Thanks



  • 4.  RE: Help about Python script for fxp0 ip recovery.

     
    Posted 07-03-2020 18:00

    Hi

     

    You can use the following to only get the IP address from the set command output:

     

    from jnpr.junos import Device
    from lxml import etree
    import jxmlease
    import json
    from junos import Junos_Configuration
    import jcs

    def main():
    dev = Device()
    dev.open()

    filter = '<system><ntp><source-address></source-address></ntp></system>'
    data = dev.rpc.get_config(filter_xml=filter, options={'format':'set'})


    #print (etree.tostring(data, encoding='unicode', pretty_print=True))
    dev.close()
    data=data.text
    data=data.split(" ")
    data=data[4]
    print data

    if __name__ == "__main__":
    main()

     

    Hope this helps



  • 5.  RE: Help about Python script for fxp0 ip recovery.
    Best Answer

    Posted 07-04-2020 16:06

    Instead of parsing large get_config you can try following script:

     

    rsp = dev.rpc.get_interface_information(interface_name='fxp0', terse=True)
    I = rsp.xpath("//ifa-local")[0].text                               #Variable with FXP0 IPaddress with mask
    i = I.split("/")[0]                                                            #Spliting and capturing only IP address



  • 6.  RE: Help about Python script for fxp0 ip recovery.

    Posted 07-06-2020 07:54

    Thank you,
    this is also a working solution! I thought I'd use it in case I wasn't able to get the master fxp0's ip.