- J-Net
- :
- Forums
- :
- Junos Automation (Scripting)
- :
- Re: Extract Juniper info (hostname, model, version...
- Application Acceleration 
- BLOG: Community Talk 
- BLOG: Information Experience (iX) 
- Community Feedback 
- Contrail Platform Developers 
- Ethernet Switching 
- Identity & Policy Control - SBR Carrier & SRC 
- Intrusion Prevention 
- Junos 
- Junos Automation (Scripting) 
- Junos Space Developer 
- Junosphere 
- Management 
- Routing 
- ScreenOS Firewalls (NOT SRX) 
- SRX Services Gateway 
- Training, Certification, and Career Topics 
- vMX 
- vSRX 
- Wireless LAN 
- Juniper Open Learning 
- Day One Books Archive 
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Extract Juniper info (hostname, model, version, etc) via python netmiko
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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
Systems Engineer, SEC DATACOM A/S (Denmark)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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
Systems Engineer, SEC DATACOM A/S (Denmark)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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|\

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|\

-r.
--------------------------------------------------
If this solves your problem, please mark this post as "Accepted Solution."
Kudos are always appreciated Smiley Happy.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
Noted and thank you sir.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Re: Extract Juniper info (hostname, model, version, etc) via python netmiko
Hi,
It works! thanks again.
This is just a starting point to extract more data from the devices.
Thank you sir.