Python basic teaching UDP port scan

Source: Internet
Author: User
Tags snmp snmp port
I. Overview
Task Description:
Develop a program to obtain a list of host IP addresses for the SNMP service on the LAN and write the corresponding files for use by other programs.
Background knowledge:
SNMP is UDP-based, and the standard SNMP service uses 161 and 162 ports.
Ideas:
1, get the local area online host list;
2, get the SNMP port of each host (for example, 161) to open the State;
3. Write specific files in a specific format.
Only the first two steps are implemented here.

Second, NMAP implementation
1. Installing Nmap
Linux platforms (CentOS for example):

Yum Install Nmap-y
Widows platform (download address):
Http://nmap.org/download.html#windows

2. Get a list of online hosts
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 condition
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 scanning
Nmap returns results:

Open: Opening
Closed: Off
Filtered: Port blocked by firewall ids/ips, unable to determine its status
Unfiltered: The port is not blocked, but it needs to be further determined if it is open
Open|filtered: The port is open or blocked
Closed|filtered: The port is closed or blocked
4, Nmap Shortcut

Scan the 161 ports of the 192.168.1.0/24 network segment as follows:
Nmap-p 161-su 192.168.1.0/24
Third, Python implementation (with Python-nmap)
The return value of Nmap has a lot of data and needs to be interpreted by its own program, such as the result of scanning the 161 port of 192.168.1.100:

Nmap Scan Report for 192.168.1.100
Host is up (0.00024s latency).
PORT State SERVICE
161/UDP closed SNMP
MAC address:10:bf:5a:6a:ba:48 (Unknown)
There is a Python parsing library, which is developed by invoking the NMAP command and parsing the results, returning the data structures that Python can recognize:
Name: Python-nmap
Url:http://xael.org/norman/python/python-nmap/python-nmap-0.1.4.tar.gz

Example (scan SNMP service on LAN for each host):

The code is 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))

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.