Junos Automation (Scripting)
Junos Automation (Scripting)

listing device ip address

[ Edited ]
‎06-19-2019 03:17 AM

Hi all,

 

There is a high number of running junos and cisco devices on the infrastructure. Each junos device has "host-name" in the configuration.  I am trying to make my own list in .TXT file with a python script that runs on my windows laptop. How can be making a list of Ip address along with device's host-name in a txt file with python script. Any idea or technics or any working py script?

Unfortunately I don't know  all device Ip address and its host-name....

 

With py script what I want is like the following list in creating txt file:

1. 192.168.0.1------->ex5060

2. 192.168.110.1------->ex2142

3. 192.168.130.130------->ex5112

...

...

500. 192.168.250.249------->ex1116

501. 192.168.250.250------->ex1258

 

I just checked the Nmap.....With Nmap network sweep scan on subnet of 192.168.0.0/16, it can be helped with not 100 %. But I want to do with py script. Hope it is clear now...

 

 

Thanks

 

Arr

 

 

7 REPLIES 7
Junos Automation (Scripting)

Re: listing device ip address

‎06-19-2019 04:02 AM

Out of interest, are you connecting to these devices via a list of IP addresses already, or via a list of dhcp registered names?

 

Just trying to figure out if you already know the iP addresses and then want to add just the host-name, or if it's all a bit of an unknown quantity.   For example you could be connecting to a device via an IP address that actually isn't fxp0.0 or em0.0 etc.  Or connecting to an address on the backup routing-engine rather than a master-only address etc. etc.

 

But if you have a list of IP addresses already, and just want to add the hostname then that should be significantly more straight-forward to perform.

 

Regards

Junos Automation (Scripting)

Re: listing device ip address

‎06-19-2019 04:18 AM

You can login to the devices and read their hostname using PyEZ.  And many ways to do this, for example simply get device facts like this:

https://www.juniper.net/documentation/en_US/junos-pyez/topics/task/program/junos-pyez-program-device...

 

Hope this helps.

 

Regards,
-r.

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

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

Junos Automation (Scripting)

Re: listing device ip address

‎06-19-2019 04:24 AM

Just a thought:  If there are a lot of devices, there was talk of 1500 SRX in another topic, then a get facts on that number of devices will be painfully slow.

 

Junos Automation (Scripting)

Re: listing device ip address

‎06-19-2019 08:25 PM

Yea, sweeping through 1500 devices will take time anyways but doable.

 

@Arix, I've tried something similar to the Nmap sweep with Python. 

 

Here's another way to do it, the script sweeps across an IP subnet 10.10.10.0/24 and records any devices that respond.  However, this assumes you know at least one username/password common to all devices.  Hope you can play around with this one to meet your requirement.  Let us know and mark the post accordingly to benefit others as well.

Just so you know, others do it with netmiko to get device facts as well: 
https://forums.juniper.net/t5/Junos-Automation-Scripting/Extract-Juniper-info-hostname-model-version...

 

from netmiko import ConnectHandler
from netaddr import IPNetwork
import re

net=IPNetwork('10.10.10.0/24')
i=0
while i<255:
	my_ip=str(net[i])
	try:
		connection = ConnectHandler(ip=my_ip,device_type='juniper',username='labroot',password='lab123')
		output1 = connection.send_command('show version| match Hostname')
		output1 = (re.sub('Hostname\:\ ','',output1))
		#print (re.sub('\\n','',output1))
		f = open("hostnames.txt", "a")
		f.write(my_ip+": ")
		f.write(re.sub('\\n','',output1)+"\n")
		f.close()
		connection.disconnect()
	except: 
		print ("Can't login to device {0}").format(net[i])
	
	i=i+1

Sample Output in the file named "hostnames.txt":

10.10.10.17: s07-28
10.10.10.23: JuniperSRX300-r005
10.10.10.24: JuniperSRX300-r004
10.10.10.25: JuniperSRX340-r004
10.10.10.36: SW-for-r001-r002-r021
10.10.10.37: s08-5
10.10.10.39: SSW02> {master:0}labroot@JNP01> 
10.10.10.43: JuniperEX4300-48P-r014
10.10.10.45: s08-13
10.10.10.47: 24P-r01
10.10.10.48: JuniperEX4200-48T-r036
10.10.10.49: JuniperEX4200-48T-r026
10.10.10.50: JuniperEX4200-48T-r049
10.10.10.51: sw4
10.10.10.58: JuniperSRX1500-r023

Hope this helps.

 

Regards,
-r.

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

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

Junos Automation (Scripting)

Re: listing device ip address

‎06-25-2019 05:14 AM

Hi Arix,

 

It would be kind of you if you can update if we've answered your query and if that helped.  Appreciate your feedback so all of us benefit from the post.

 

Hope this helps.

Regards,
-r.

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

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

 

 

 

 

Junos Automation (Scripting)

Re: listing device ip address

[ Edited ]
‎06-25-2019 09:44 AM

Hi Arix,

 

One simple way of implementing the functionality you desire it to complete the following -

 

1. Iterate through the hosts in a given subnet.

2. Connect to the Juniper devices using the PyEZ "Device" module". If connection is unsuccesul, print error and move on to next host.

3. Print the host name. Other details like HW information can also be included if you want.

 

For devices which are not Juniper devices, you want to ignore the exceptions which may occur in the for loop, but here is a sample code I have tested -

 

nw=ipaddress.ip_network('10.85.173.128/25')
for addr in nw:
    dev=Device(host=str(addr),user=####,password=####)
    try:
        dev.open()
    except ConnectError as err:
        print ("Cannot connect to device: {0}".format(err))
        continue
    sw=dev.rpc.get_software_information()
    hn=sw.findtext('host-name')
mod=sw.findtext('product-model')
print("Hostname of device with IP=", str(addr), "is", str(hn), "Product Model =", str(mod))
dev.close()

Sample Script Run:

Cannot connect to device: ConnectTimeoutError(10.85.173.128)
Cannot connect to device: ConnectRefusedError(10.85.173.129)
Cannot connect to device: ConnectRefusedError(10.85.173.130)
Cannot connect to device: ConnectRefusedError(10.85.173.131)
Device(10.85.173.132)
Hostname of device with IP= 10.85.173.132 is bacardi Product Model = ex9214
Cannot connect to device: ConnectTimeoutError(10.85.173.133)
Device(10.85.173.134)
Hostname of device with IP= 10.85.173.134 is bacardi Product Model = ex9214
Device(10.85.173.135)
Hostname of device with IP= 10.85.173.135 is m02-17 Product Model = qfx10002-72q
Device(10.85.173.136)
Hostname of device with IP= 10.85.173.136 is m02-19 Product Model = qfx10002-72q
Device(10.85.173.137)
Hostname of device with IP= 10.85.173.137 is m02-21 Product Model = qfx10002-72q

Here are the libraries I used -

import ipaddress
from ipaddress import ip_network
import jnpr.junos
from jnpr.junos.exception import LockError
from jnpr.junos.exception import ConnectError
from jnpr.junos import Device
import lxml
from lxml import etree
import warnings

=======================

Please mark my solution as Accepted if it works as intended.

Kudo are always appreciated Smiley Happy

 

Junos Automation (Scripting)

Re: listing device ip address

‎07-01-2019 08:11 PM

Hello Arix,

 

Any update on this post? Did any of the advises help?

 

Would you mind sharing the resolution and marking it as resolution if your query is resolved? As I said, that'll make the post useful for others.

 

Regards,
-r.

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

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