netaddr模块的一些基本用法:
In [1]: from netaddr import * In [2]: ip = IPAddress('172.16.100.39') In [3]: ip. ip.bin ip.is_hostmask ip.is_netmask ip.packed ip.bits ip.is_ipv4_compat ip.is_private ip.reverse_dns ip.format ip.is_ipv4_mapped ip.is_reserved ip.sort_key ip.info ip.is_link_local ip.is_unicast ip.value ip.ipv4 ip.is_loopback ip.key ip.version ip.ipv6 ip.is_multicast ip.netmask_bits ip.words In [3]: ip. ip.bin ip.is_hostmask ip.is_netmask ip.packed ip.bits ip.is_ipv4_compat ip.is_private ip.reverse_dns ip.format ip.is_ipv4_mapped ip.is_reserved ip.sort_key ip.info ip.is_link_local ip.is_unicast ip.value ip.ipv4 ip.is_loopback ip.key ip.version ip.ipv6 ip.is_multicast ip.netmask_bits ip.words In [3]: ip.version Out[3]: 4 In [4]: ip.format Out[4]:In [5]: ip.format() Out[5]: '172.16.100.39' In [6]: repr(ip) Out[6]: "IPAddress('172.16.100.39')" In [7]: int(ip) Out[7]: 2886755367 In [8]: ip.bits() Out[8]: '10101100.00010000.01100100.00100111' In [9]: ip.words Out[9]: (172, 16, 100, 39) In [10]: ip = IPNetwork('172.16.100.39/24') In [11]: ip. ip.broadcast ip.is_ipv4_compat ip.iter_hosts ip.size ip.cidr ip.is_ipv4_mapped ip.key ip.sort_key ip.first ip.is_link_local ip.last ip.subnet ip.hostmask ip.is_loopback ip.netmask ip.supernet ip.info ip.is_multicast ip.network ip.value ip.ip ip.is_private ip.next ip.version ip.ipv4 ip.is_reserved ip.prefixlen ip.ipv6 ip.is_unicast ip.previous In [11]: ip.network Out[11]: IPAddress('172.16.100.0') In [12]: ip.broadcast Out[12]: IPAddress('172.16.100.255') In [13]: ip.ip Out[13]: IPAddress('172.16.100.39') In [14]: ip.size Out[14]: 256 In [15]: ip.net ip.netmask ip.network In [15]: ip.netmask Out[15]: IPAddress('255.255.255.0') In [16]: ip.hostmask Out[16]: IPAddress('0.0.0.255') In [17]: ip.pr ip.prefixlen ip.previous In [17]: ip.prefixlen Out[17]: 24
下面是netaddr结合python-nmap实现的端口扫描程序:
[root@controller-39 ~]# yum install -y nmap # 安装nmap [root@controller-39 ~]# pip install python-nmap # 安装python-nmap模块 [root@controller001 home]# cat nmap_test.py # 扫描同网段机器的端口 from netaddr import * import nmap ip = '192.168.141.2' prefix = '24' ipn = IPNetwork(ip+'/'+prefix) network = str(ipn.network) hosts = network+'/'+prefix port= '22-443' try: nm = nmap.PortScanner() except nmap.PortScannerError: print('Nmap not found', sys.exc_info()[0]) sys.exit(0) except: print('Nmap not found', sys.exc_info()[0]) sys.exit(0) nm.scan(hosts, port) for host in nm.all_hosts(): print("---------------------------") print("Host : %s" % (host)) print("State : %s" % nm[host].state()) for proto in nm[host].all_protocols(): print("----------------") print("Protocol : %s" % proto) lport = nm[host][proto].keys() lport.sort() for port in lport: print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state'])) [root@controller001 home]# python nmap_test.py # 执行结果 --------------------------- Host : 192.168.141.17 State : up ---------------- Protocol : tcp port : 22 state : open port : 80 state : open port : 111 state : open --------------------------- Host : 192.168.141.2 State : up ---------------- Protocol : tcp port : 22 state : open port : 80 state : open --------------------------- Host : 192.168.141.26 State : up ---------------- Protocol : tcp port : 22 state : open port : 111 state : open --------------------------- Host : 192.168.141.28 State : up ---------------- Protocol : tcp port : 22 state : open port : 53 state : open port : 80 state : open port : 111 state : open --------------------------- Host : 192.168.141.30 State : up ---------------- Protocol : tcp port : 22 state : open port : 80 state : open port : 111 state : open
参考链接
http://xael.org/pages/python-nmap-en.html