I. Overview
Task Description:
Develop a program to obtain a list of host IP addresses in the LAN on which the SNMP service is opened, and write the corresponding files for use by other programs.
Background knowledge:
SNMP is based on UDP, and the standard SNMP service uses ports 161 and 162.
Ideas:
1, access to the local domain online host list;
2, to obtain the SNMP port of each host (such as 161) open condition;
3. Write specific files in a specific format.
Only the first two steps are implemented here.
Second, NMAP implementation
1. Installation Nmap
Linux platform (CentOS for example):
Yum Install Nmap-y
Widows platform (download address):
Http://nmap.org/download.html#windows
2. Get online host list
Take 192.168.1.0/24 network segment as an example:
NMAP-SN 192.168.1.0/24
Or specify an IP range scan:
NMAP-SN 192.168.1.1-254
Parameter explanation:
-sn:ping Scan, only host discovery, no port scan.
3, get the host port open state
Taking 192.168.1.100 as an example
Nmap-p 161-su 192.168.1.100
Parameter explanation:
-P 161: Scan 161 ports
-SU: for UDP scans
Nmap return Result:
Open: Opening
Closed: Off
Filtered: Port is masked by firewall ids/ips, unable to determine its status
Unfiltered: The port is not blocked, but opening needs to be further determined
Open|filtered: port is open or blocked
Closed|filtered: port is off or blocked
4, Nmap Shortcut
Scan the 192.168.1.0/24 network segment 161 ports are as follows:
Nmap-p 161-su 192.168.1.0/24
Three, Python implementation (with the help of Python-nmap)
The return value of the nmap has a lot of data and needs to be parsed by its own program, such as the result of a scan of the 192.168.1.100 161 port:
Nmap Scan for 192.168.1.100
The Host is up (0.00024s latency).
PORT State SERVICE
161/UDP closed SNMP
MAC address:10:bf:5a:6a:ba:48 (Unknown)
Here's a python-developed Nmap parsing library that calls the Nmap command and parses its results to return the data structures that Python can identify:
Name: Python-nmap
Url:http://xael.org/norman/python/python-nmap/python-nmap-0.1.4.tar.gz
Example (scan for SNMP service on each host on LAN):
Copy Code code as follows:
#! /usr/bin/python
Import Nmap
NM = Nmap. Portscanner ()
Nm.scan (hosts= ' 192.168.1.0/24 ', arguments= '-P 161-su ')
Hosts_list = [(x, Nm[x][u ' UDP '][161][' state ')] for x in Nm.all_hosts ()]
For host, status in Hosts_list:
Print (' {0}:{1} '. Format (host, status)