Junos Automation (Scripting)
Junos Automation (Scripting)

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

‎01-28-2019 04:44 PM

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.

11 REPLIES 11
Junos Automation (Scripting)

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

‎01-28-2019 10:12 PM

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.


--
Best regards,

Jonas Hauge Klingenberg
Juniper Ambassador & Technology Architect, SEC DATACOM A/S (Denmark)
Highlighted
Junos Automation (Scripting)

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

‎01-28-2019 10:56 PM

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

 

 

Junos Automation (Scripting)

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

‎01-29-2019 12:21 AM

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.

Junos Automation (Scripting)

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

‎01-29-2019 12:26 AM

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

Junos Automation (Scripting)

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

‎01-29-2019 12:28 AM

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.


--
Best regards,

Jonas Hauge Klingenberg
Juniper Ambassador & Technology Architect, SEC DATACOM A/S (Denmark)
Junos Automation (Scripting)
Solution
Accepted by topic author jar
‎01-29-2019 05:14 PM

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

‎01-29-2019 12:46 AM
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|\Smiley Indifferent0|\}|\\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|\Smiley Indifferent0|\}|\\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.
Junos Automation (Scripting)

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

‎01-29-2019 01:21 AM

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.

Junos Automation (Scripting)

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

‎01-29-2019 02:50 AM

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.

Junos Automation (Scripting)

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

‎01-29-2019 03:03 AM
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 Smiley Happy.
Junos Automation (Scripting)

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

‎01-29-2019 05:13 PM

Noted and thank you sir.

Junos Automation (Scripting)

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

‎01-29-2019 05:16 PM

Hi,

It works! thanks again. 

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

Thank you sir.