Package: networkscan
NetworkscanNetworkscan is a fast host scanner written in python. It can be used in command line or as a python library.The advantages of that program are:it can perform fast pings (thanks to the use of coroutines)it can be used as a command line program or as a python libraryit can create a list of IP address hosts as a output for easy IP address manipulationit can create a yaml host inventory compatible with NornirCompatibilityNetworkscan requires python 3 and the following python libraries:asyncioipaddressplatformsysIt works on Windows and Linux.Installationpip install networkscanCommand line usage1 - Available optionsHere are the available options for the software:(project1)[eorain@centos7python]$./networkscan.py

Usage:networkscan.pynetwork_to_scan[-h][-q][-m][-w[hosts.yaml]]Options:network_to_scanThenetworkorIPaddresstoscanusingfastpings.Examples:"192.168.0.0/24","10.0.0.1","172.16.1.128/28",etc.-hHelp-mMutemode(nothingisdisplayedonscreen)-qQuietmode(justthelistofhostsfoundisdisplayed)-w[hosts.yaml]Writeayamlhostfilewithanoptionalfilename(defaultnameishosts.yaml)(project1)[eorain@centos7python]$2 - How to scan a /24 networknetworkscan.py 192.168.0.0/24(project1)[eorain@centos7python]$./networkscan.py192.168.0.0/24
Networktoscan:192.168.0.0/24
Prefixtoscan:24Numberofhoststoscan:254Scanninghosts...
Listofhostsfound:192.168.0.10192.168.0.11192.168.0.12192.168.0.100192.168.0.101192.168.0.111192.168.0.1
Numberofhostsfound:7(project1)[eorain@centos7python]$3 - How to scan a /28 network displaying just the name of the hosts (quiet mode)networkscan.py 192.168.0.0/28 -q(project1)[eorain@centos7python]$./networkscan.py192.168.0.0/28-q192.168.0.1192.168.0.10192.168.0.11192.168.0.12(project1)[eorain@centos7python]$4 - How to scan a /25 network then to save the list of hosts into a text file (quiet mode and redirection of the output into a file)networkscan.py 192.168.0.0/25 -q >inventory.txt(project1)[eorain@centos7python]$./networkscan.py192.168.0.0/25-q>inventory.txt(project1)[eorain@centos7python]$(project1)[eorain@centos7python]$catinventory.txt192.168.0.100192.168.0.101192.168.0.111192.168.0.1192.168.0.10192.168.0.11192.168.0.12(project1)[eorain@centos7python]$4 - How to scan a /23 network then save the list of hosts into a yaml file compatible with Nornir syntax (mute mode and creation of a yaml file)Please note that when no file is specified with the parameter "-w" then a "hosts.yaml" file is created by default. With the command "networkscan.py 192.168.0.0/23 -m -w foo.yaml" you do create a file named "foo.yaml".networkscan.py 192.168.0.0/23 -m -w(project1)[eorain@centos7python]$./networkscan.py192.168.0.0/23-m-w(project1)[eorain@centos7python]$lshos*
hosts.yaml(project1)[eorain@centos7python]$hosts.yaml:---device1:hostname:192.168.0.1groups:-device_discovereddevice2:hostname:192.168.0.100groups:-device_discovereddevice3:hostname:192.168.0.101groups:-device_discovereddevice4:hostname:192.168.0.10groups:-device_discovereddevice5:hostname:192.168.0.11groups:-device_discovereddevice6:hostname:192.168.0.12groups:-device_discovereddevice7:hostname:192.168.0.111groups:-device_discoveredPython library usage1 - A simple scriptThe following script just scan a network then displays the list of host found.python script:#!/usr/bin/env python3# Import Python libraryimportnetworkscan# Main functionif__name__=='__main__':# Define the network to scanmy_network="192.168.0.0/24"# Create the objectmy_scan=networkscan.Networkscan(my_network)# Run the scan of hosts using pingsmy_scan.run()# Display the IP address of all the hosts foundforiinmy_scan.list_of_hosts_found:print(i)output:192.168.0.100
192.168.0.101
192.168.0.111
192.168.0.1
192.168.0.10
192.168.0.11
192.168.0.122 - An advanced scriptThis script scans a network then it creates a yaml file with the list of hosts found.write_file() method accepts two optional parameters:defwrite_file(self,file_type=0,filename="hosts.yaml"):""" Method to write a file with the list of the detected hosts """# Input:## - file_type (integer, optional): 0, Nornir file (default value)#                                  1, Text file as output file# - filename (string, optional): the name of the file to be written ("hosts.yaml"#   is the default value)## Ouput:# A text file with the list of detected hosts ("hosts.yaml" is the default value)# return 0 if no error occuredpython script:#!/usr/bin/env python3# Import Python libraryimportnetworkscan# Main functionif__name__=='__main__':# Define the network to scanmy_network="192.168.0.0/24"# Create the objectmy_scan=networkscan.Networkscan(my_network)# Display informationprint("Network to scan: "+str(my_scan.network))print("Prefix to scan: "+str(my_scan.network.prefixlen))print("Number of hosts to scan: "+str(my_scan.nbr_host))# Run the network scanprint("Scanning hosts...")# Run the scan of hosts using pingsmy_scan.run()# Display informationprint("List of hosts found:")# Display the IP address of all the hosts foundforiinmy_scan.list_of_hosts_found:print(i)# Display informationprint("Number of hosts found: "+str(my_scan.nbr_host_found))# Write the file on diskres=my_scan.write_file()# Error while writting the file?ifres:# Yesprint("Write error with file "+my_scan.filename)else:# No errorprint("Data saved into file "+my_scan.filename)PerformanceHere are the results I had with a single process with 1 second timeout ping using asyncio and without asyncio.Number of hosts12261430621262545101022Network/32/31/30/29/28/27/26/25/24/23/22Networkscan (sec)0,1841,1781,1631,2131,2321,4111,9512,235,1047,05518,196Without asyncio (timeout 1 sec)N/A1,1361,1155,48510,19426,85267,258130,334253,168321,908865,858What's next with the Nornir yaml file?When you have scanned a network you have typed the entered the following command and you have got that output:(project1)[eorain@centos7python]$./networkscan.py192.168.0.96/28-w
Networktoscan:192.168.0.96/28
Prefixtoscan:28Numberofhoststoscan:14Scanninghosts...
Listofhostsfound:192.168.0.100192.168.0.101
Numberofhostsfound:2Writtingfile
Datasavedintofilehosts.yaml(project1)[eorain@centos7python]$The file hosts.yaml has been created.hosts.yaml------device1:hostname:192.168.0.100groups:-device_discovereddevice2:hostname:192.168.0.101groups:-device_discoveredAfter scanning your network and creating a Nornir inventory file ("hosts.yaml") you might wonder what to do next. You can notice that the "hosts.yaml"  does not include the password, login and platform information.There are two cases:either all the devices are different devices with eventually different credentialsor they are from the same type and have the credentialsa) Different devicesIn the first case you will have to fill the login, password and platform reference on all the devices. 3 fields are added:usernamepasswordplatformHere is an example:hosts.yaml---device1:hostname:192.168.0.100username:ciscopassword:ciscoplatform:iosgroups:-device_discovereddevice2:hostname:192.168.0.101username:junipassword:perplatform:junosgroups:-device_discoveredb) Same devices and same credentialsIf you are in this situation things are easier. You probably noticed that all the devices belongs to the "device_discovered" group. You can create a group file with a reference to that group then to add the missing value to the group file (with no change on the "hosts.yaml" file).Here is an example with Cisco IOS equipments:groups.yaml---device_discovered:username:ciscopassword:ciscoplatform:iosc) Ready to use NornirIf you are done with a) or b) now you are ready to write your code with Nornir. It is better to put your yaml files into a folder (here "inventory" folder).Just copy the content of the "nornir_arp.py" into a file.nornir_arp.py#!/usr/bin/env python3# Import Python libraryfromnornirimportInitNornirfromnornir.plugins.functions.textimportprint_title,print_resultfromnornir.plugins.tasksimportnetworking# Main functiondefmain():# Create a Nornir objectnr=InitNornir(core={"num_workers":100},inventory={"plugin":"nornir.plugins.inventory.simple.SimpleInventory","options":{"host_file":"inventory/hosts.yaml","group_file":"inventory/groups.yaml"}})# Run Nornir task (here getting the ARP table of the devices)result=nr.run(task=networking.napalm_get,name=" ARP table ",getters=["arp_table"])# Display the resultprint_title("Display ARP table of the network devices")print_result(result)# Main function callif__name__=='__main__':main()You should get that file structure:|---nornir_arp.py|---inventory|---groups.yaml|---hosts.yaml|You can then run your Nornir program in python.(project1)[eorain@centos7python]$./nornir_arp.py
****DisplayARPtableofthenetworkdevices**********************************ARPtable*********************************************************************
*device1**changed:False***************************************************
vvvvARPtable**changed:FalsevvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvINFO{'arp_table':[{'age':2.0,'interface':'Ethernet2/0','ip':'192.168.0.1','mac':'AC:74:99:B3:27:EF'},{'age':10.0,'interface':'Ethernet2/0','ip':'192.168.0.11','mac':'8A:78:01:80:87:DD'},{'age':0.0,'interface':'Ethernet2/0','ip':'192.168.0.100','mac':'8A:FF:18:CC:00:48'}]}^^^^ENDARPtable^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*device2**changed:False***************************************************
vvvvARPtable**changed:FalsevvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvINFO{'arp_table':[{'age':2.0,'interface':'Ethernet2/0','ip':'192.168.0.1','mac':'AC:74:99:B3:27:EF'},{'age':10.0,'interface':'Ethernet2/0','ip':'192.168.0.11','mac':'8A:78:01:80:87:DD'},{'age':0.0,'interface':'Ethernet2/0','ip':'192.168.0.101','mac':'88:78:44:0A:DE:13'},{'age':0.0,'interface':'Ethernet2/2','ip':'192.168.255.1','mac':'88:78:44:0A:DE:19'}]}^^^^ENDARPtable^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^(project1)[eorain@centos7python]$