Automation

Expand all | Collapse all

Extract Juniper info (hostname, model, version, etc) via python netmiko

Jump to Best Answer
  • 1.  Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-28-2019 16:44

    Hi Team,

    First of all thank you for your support.

     

    I run simple script to get certain info such as hostname and model via netmiko module in python. Im not able to use pyez as no netconf enable on the devices. Thus, i just use netmiko and I run command below to get Hostname and Model, version info

    print(connection.send_command('show version | match Hostname'))

    print(connection.send_command('show version | match Model'))

    print(connection.send_command('show version | match Version:'))

     

    This will print below:-

    Hostname: vcx.lab01

     

    {master}

    Model: t640

     

    {master}

    Junos: 15.1R7-S1

    {master}

     

    I try using strip to remove the space and {master} but it didnt work. I want it to be print as follows, simple and clean

    Hostname: vcx.lab01

    Model: t640

    Junos: 15.1R7-S1

     

    I really hope experts here could lead me the way, I have doing this for a week and still no progress. I hope someone could advise further. Thank you for your attention and help. I appreciated it.



  • 2.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-28-2019 22:12

    The simple suggestion is to utilize the functionality of junos 'match' where you can do it all in one request:

     

    user@fw> show version | match "Hostname|Model|Release"
    Hostname: fw
    Model: srx300
    JUNOS Software Release [18.4R1.8]
    

    That should be something like print(connection.send_command('show version | match "Hostname|Model|Version"'))

    You would still have the {master} part once. You will have to strip this via python... I don't have any experience with this so somebody else have to suggest how to handle this.



  • 3.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-28-2019 22:57

    You can try using re (regex) in python "import re" .  You also may need to convert your outputs in string and need to use  slice,split etc, depnding on your script and the output  type.

     

    Thanks,

    Vikas

     

     



  • 4.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-29-2019 00:27

    Hi Sir,

    Thank you.

    I have test using slice,split etc.. but seems it doesnt works (maybe Im doing wrong ). 

    Perhaps if someone already encouter this issue (remove line space and {master}) in junos output can also advise how to do it.

    For now I still not able to do it



  • 5.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-29-2019 00:22

    Hi Sir,

    Thanks for the recommendation. 

    Why each output it will also print {master}? and seems like got space between the output request and {master}

    Hostname: xxxxx
    Model: xxxxxx

     

    {master}

    *Here got space between Model: and {master} that I need to remove.

    Anyway thanks for your suggestion.



  • 6.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-29-2019 00:28

    the "{master}" comes as your lab equipment has dual routing-engines. The line is shown to confirm that you are working on the primary routing engine.

     

    From a data extraction point it's not very valuable but when you are working on the device it's good to know.



  • 7.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko
    Best Answer

     
    Posted 01-29-2019 00:46
    Hi jar,

    You can do something like this:

    output1 = connection.send_command('show version| match Hostname')
    output1 += (connection.send_command('show version| match Model'))
    output1 += (connection.send_command('show version| match Junos:'))

    print (re.sub('\{|master|\:|0|\}|\\n','',output1))

    This removes newline and spaces from every line and gets you something like this:
    Hostname:R2Model:qfx5100-48s-6qJunos:14.1X53-D49_vjunos.79


    Else, do it like:

    output1 = connection.send_command('show version| match Hostname')
    output2 = (connection.send_command('show version| match Model'))
    output3 = (connection.send_command('show version| match Junos:'))

    print (re.sub('\\n','',output1))
    print (re.sub('\\n','',output2))
    print (re.sub('\\n','',output3))

    And remove the {master:} part using "print (re.sub('\{|master|\:|0|\}|\\n','',output1))" from whichever output it applies.

    -r.

    --------------------------------------------------

    If this solves your problem, please mark this post as "Accepted Solution."
    Kudos are always appreciated Smiley Happy.


  • 8.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

     
    Posted 01-29-2019 01:21

    The smiley part is \ followed by a colon :

     

    Hope this helps.

     

    -r.

    --------------------------------------------------

    If this solves your problem, please mark this post as "Accepted Solution."
    Kudos are always appreciated Smiley Happy.



  • 9.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-29-2019 02:50

    Hi sir

    I will test this and get back with the result

     

    can u confirm below command ..is it correct? 

    print (re.sub('\{|master|\:|\}|\\n','',output1))"

     

    Thank you.



  • 10.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

     
    Posted 01-29-2019 03:03
    Hi jar,

    Thank you for responding. Yes, in the command you shared, please add another |0| after the colon i.e. add this part after the colon without the quotes "|0|\}|\\n','',output1))".

    If you print output of each command output separately, you can find out which command or commands have the {master:0} in the output. Then use this part to print that output:
    print (re.sub('\{|master|\ followed by colon|0|\}|\\n','',output1))

    Hope this helps.

    -r.

    --------------------------------------------------

    If this solves your problem, please mark this post as "Accepted Solution."
    Kudos are always appreciated :).


  • 11.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-29-2019 17:16

    Hi,

    It works! thanks again. 

    This is just a starting point to extract more data from the devices.

    Thank you sir.



  • 12.  RE: Extract Juniper info (hostname, model, version, etc) via python netmiko

    Posted 01-29-2019 17:13

    Noted and thank you sir.