使用Asyncio的平扫描仪

networkscan的Python项目详细描述


网络扫描

networkscan是一个用python编写的快速主机扫描程序。它可以在命令行中使用,也可以用作python库。

该程序的优点是:

  • 它可以执行快速ping(由于使用了协程)
  • 它可以用作命令行程序或python库
  • 它可以创建一个IP地址主机列表作为输出,以便于IP地址操作
  • 它可以创建与nornir兼容的yaml主机清单

兼容性

NetworkScan需要Python3和以下Python库:

  • 异步
  • IP地址
  • 平台
  • 系统

它可以在windows和linux上运行。


安装

pip install networkscan


命令行用法

1-可用选项

以下是软件的可用选项:

(project1)[eorain@centos7 python]$ ./networkscan.py

Usage: networkscan.py network_to_scan [-h][-q][-m][-w [hosts.yaml]]

Options :
    network_to_scan      The network or IP address to scan using fast pings.
                         Examples: "192.168.0.0/24", "10.0.0.1", "172.16.1.128/28", etc.
    -h                   Help
    -m                   Mute mode (nothing is displayed on screen)
    -q                   Quiet mode (just the list of hosts found is displayed)
    -w [hosts.yaml]      Write a yaml host file with an optional filename (default name is hosts.yaml)(project1)[eorain@centos7 python]$

2-如何扫描A/24网络

networkscan.py 192.168.0.0/24

(project1)[eorain@centos7 python]$ ./networkscan.py 192.168.0.0/24
Network to scan: 192.168.0.0/24
Prefix to scan: 24
Number of hosts to scan: 254
Scanning hosts...
List of hosts found:
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.100
192.168.0.101
192.168.0.111
192.168.0.1
Number of hosts found: 7(project1)[eorain@centos7 python]$

3-如何扫描仅显示主机名称的A/28网络(安静模式)

networkscan.py 192.168.0.0/28 -q

(project1)[eorain@centos7 python]$ ./networkscan.py 192.168.0.0/28 -q
192.168.0.1
192.168.0.10
192.168.0.11
192.168.0.12
(project1)[eorain@centos7 python]$

4-如何扫描A/25网络,然后将主机列表保存到文本文件中(安静模式和将输出重定向到文件中)

networkscan.py 192.168.0.0/25 -q >inventory.txt

(project1)[eorain@centos7 python]$ ./networkscan.py 192.168.0.0/25 -q >inventory.txt
(project1)[eorain@centos7 python]$
(project1)[eorain@centos7 python]$ cat inventory.txt
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.12
(project1)[eorain@centos7 python]$

4-如何扫描A/23网络,然后将主机列表保存到与nornir语法兼容的yaml文件中(静音模式和创建yaml文件)


请注意,如果没有使用参数“-w”指定文件,则默认情况下会创建一个“hosts.yaml”文件。使用命令“networkscan.py 192.168.0.0/23-m-w foo.yaml”可以创建一个名为“foo.yaml”的文件。


networkscan.py 192.168.0.0/23 -m -w

(project1)[eorain@centos7 python]$ ./networkscan.py 192.168.0.0/23 -m -w
(project1)[eorain@centos7 python]$ ls hos*
hosts.yaml
(project1)[eorain@centos7 python]$

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_discovered

python库用法

1-一个简单的脚本

下面的脚本只是扫描一个网络,然后显示找到的主机列表。

python脚本:

#!/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)

输出:

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.12

2-高级脚本

此脚本扫描网络,然后创建一个包含找到的主机列表的yaml文件。

write_file()方法接受两个可选参数:

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 occured

python脚本:

#!/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)

性能

下面是使用asyncio和不使用asyncio的1秒超时ping的单个进程的结果。

Number of hosts12261430621262545101022
Network/32/31/30/29/28/27/26/25/24/23/22
Networkscan (sec)0,1841,1781,1631,2131,2321,4111,9512,235,1047,05518,196
Without asyncio (timeout 1 sec)N/A1,1361,1155,48510,19426,85267,258130,334253,168321,908865,858

下一步诺尼尔山药文件是什么?

扫描网络后,您输入了以下命令,并得到了输出:

(project1)[eorain@centos7 python]$ ./networkscan.py 192.168.0.96/28 -w
Network to scan: 192.168.0.96/28
Prefix to scan: 28
Number of hosts to scan: 14
Scanning hosts...
List of hosts found:
192.168.0.100
192.168.0.101
Number of hosts found: 2
Writting file
Data saved into file hosts.yaml
(project1)[eorain@centos7 python]$

已创建hosts.yaml文件。

hosts.yaml

------device1:hostname:192.168.0.100groups:-device_discovereddevice2:hostname:192.168.0.101groups:-device_discovered

扫描网络并创建nornir库存文件(“hosts.yaml”)之后,您可能会想知道下一步该做什么。您可以注意到,“hosts.yaml”不包括密码、登录名和平台信息。

有两种情况:

  • 所有设备都是具有最终不同凭据的不同设备
  • 或者他们是同一类型的,并且有资格证书

a)不同设备

在第一种情况下,您必须在所有设备上填写登录名、密码和平台参考。添加了3个字段:

  • 用户名
  • 密码
  • 平台

下面是一个示例:

hosts.yaml

---device1:hostname:192.168.0.100username:ciscopassword:ciscoplatform:iosgroups:-device_discovereddevice2:hostname:192.168.0.101username:junipassword:perplatform:junosgroups:-device_discovered

b)相同的设备和相同的证书

如果你在这种情况下,事情就容易多了。您可能注意到所有设备都属于“设备发现”组。您可以使用对该组的引用创建一个组文件,然后将丢失的值添加到该组文件中(不更改“hosts.yaml”文件)。

以下是Cisco IOS设备的示例:

groups.yaml

---device_discovered:username:ciscopassword:ciscoplatform:ios

c)准备好使用诺尼尔

如果你已经完成了a)或b)现在你已经准备好用nornir编写你的代码了。最好将您的yaml文件放入一个文件夹(这里是“inventory”文件夹)。

强制执行t将“nornir_arp.py”的内容复制到文件中。

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()

您应该获得该文件结构:

|--- nornir_arp.py
|--- inventory
|    --- groups.yaml
|    --- hosts.yaml
|

然后可以用python运行nornir程序。

(project1)[eorain@centos7 python]$ ./nornir_arp.py
**** Display ARP table of the network devices **********************************
 ARP table *********************************************************************
* device1 ** changed : False ***************************************************
vvvv  ARP table  ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
{'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'}]}
^^^^ END  ARP table  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* device2 ** changed : False ***************************************************
vvvv  ARP table  ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
{'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'}]}
^^^^ END  ARP table  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(project1)[eorain@centos7 python]$

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
基于作者姓名的java Jersey消息筛选应返回多个值   捕获到java意外异常:com。ptc。pfc.实施。运行AsyncInstallTest时PFCEExceptions$XToolkitGeneralError   java Maven找不到apache RandomStringGenerator   java共享音频和文本   maven编译期间创建的java神秘目录   Java算法将2D数组向右移动,最后一列放在第一列   java是否可以在不激活插件的情况下触发RCPPlugin中的一系列操作?   握手期间java通信失败。是否有MySQL服务器运行在localhost:3306上   java如何为从多个表连接的对象定义Ignite缓存   java如何在我的网站上放置一个服务器类,以便我的客户机类可以从不同的计算机与之通信?   java jQuery是否有内置函数来执行长轮询?   如何在一个数组中编译多个Java文件   java JVM最大堆大小可以是动态的吗?   java JMAP命令,调试对象的错误为64位   java如何从契约状态获取stateAndRef?   安卓 MediaPlayer在状态4中调用java Start错误?   java如何使用ApacheKaraf/OSGi构建桌面应用程序?   classpath Linux path变量未在Java程序中正确设置   java activemq 5.9.0在Windows和Maven中失败   java Android未在库中创建文件夹