python基础教程之udp端口扫描
一、概述
任务描述:
开发一个程序,用于获取局域网中开启snmp服务的主机ip地址列表,并写入相应文件以便其它程序使用。
背景知识:
SNMP是基于UDP的,而且标准的SNMP服务使用161和162端口。
思路:
1、获取局域在线主机列表;
2、获取各个主机的snmp端口(比如161)开启状况;
3、以特定格式写入特定文件。
这里只实现前两步。
二、nmap实现
1、安装nmap
Linux平台(CentOS为例):
yum install nmap -y
widows平台(下载地址):
http://nmap.org/download.html#windows
2、获取在线主机列表
以192.168.1.0/24网段为例:
nmap -sn 192.168.1.0/24
或者指定ip范围扫描:
nmap -sn 192.168.1.1-254
参数解释:
-sn: Ping扫描,只进行主机发现,不进行端口扫描。
3、获取主机端口开启状况
以192.168.1.100为例
nmap -p 161 -sU 192.168.1.100
参数解释:
-p 161 : 扫描161端口
-sU : 进行UDP扫描
nmap返回结果:
open : 开放
closed : 关闭
filtered : 端口被防火墙IDS/IPS 屏蔽,无法确定其状态
unfiltered : 端口没有被屏蔽,但是否开放需要进一步确定
open|filtered : 端口是开放的或被屏蔽
closed|filtered : 端口是关闭的或被屏蔽
4、nmap捷径
扫描192.168.1.0/24网段的161端口如下:
nmap -p 161 -sU 192.168.1.0/24
三、python实现(借助python-nmap)
nmap的返回值有很多数据,需要自行写程序进行解析,比如对192.168.1.100的161端口进行扫描的结果:
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)
这里有个python开发的nmap解析库,原理是调用nmap命令,并对其结果进行解析,返回python能识别的数据结构:
名称 : python-nmap
url : http://xael.org/norman/python/python-nmap/python-nmap-0.1.4.tar.gz
示例(扫描局域网各个主机的snmp服务开启状况):
代码如下:
#! /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))

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Windows 10 has a free antivirus program called Windows Defender, which provides real-time protection and can scan your computer. This also allows you to perform customized scans, whereby you can specify specific folders or drives to scan for malware. Because you only need to scan this folder, the scan time will be much faster than scanning the entire machine. As shown below, we offer two ways to customize the scan for your specific folders. How to use Windows Defender to scan folders for malware in Win10. To scan an individual folder and its subfolders, the easiest way is to right-click the folder and select Scan with Windows Defender

After using HP printers to scan documents, many users want to scan them directly into a PDF file, but they don't know how to do it successfully. They just need to use a scanner program on their computer. How to scan an HP printer into a PDF: 1. First open the scanner program on your computer. 2. Then select "Save PDF" in the page settings. 3. Then press "Scan" in the lower right corner to start scanning the first file. 4. After completion, click the "+" icon in the lower left corner to add a new scan page. 5. You will see a new scan box next to the original file. 7. When finished, select "Save" to save these PDF files.

1. Open NetEase Cloud Music, click My, then click Local Music. 2. Click the three dots in the upper right corner. 3. Click Scan local music. 4. Click Scan Settings below. 5. Swipe left to filter audio files shorter than 60 seconds. 6. Go back and click Full Scan to scan all local music.

In Quark software, a variety of functions bring convenience and fun to users, among which the scanning function is particularly popular. Through the scanning function, users can easily scan the QR code, whether it is to quickly log in to the website, add friends, or download applications, all can be done with one click. So, how to use the scanning function of Quark QR code? Players who still don’t know how to use it must not miss it. Come and follow the article brought by the editor of this website to learn more about it. How to scan the Quark QR code and answer: [Quark]-[Three horizontal icons]-[Take photo and scan]. Specific steps: 1. First open the Quark software. After entering the homepage, we click the [three horizontal icons] in the lower right corner; 2. Then slide up on the My Page to the bottom of the page to find [Photo Scan]

Processing method: 1. Open the scanned image to be processed in the PS software; 2. Click "Filter" - "Sharpening" - "Smart Sharpening" on the top toolbar; 3. In the pop-up window, select according to your needs Sharpen the radius and click "OK"; 4. Click "File" - "Save As" and save it as a file.

How to use QR code scanning in PHP? QR code scanning has become very common in modern life. Whether it is Alipay, WeChat Pay or various other applications, you can quickly pay or transfer information by scanning the QR code. In PHP, we can also easily implement the QR code scanning function. This article will introduce how to use PHP for QR code scanning and provide corresponding code examples. First, we need a PHP QR code generation library. Here we choose to use the "PHPQRCode" library.

In 2022, Apple added a new feature to the Notes app on iPhone and iPad that allows you to quickly scan printed or handwritten text and save it in a digital text format. Read on to learn how it works. On earlier versions of iOS and iPadOS, scanning text into Apple's Notes app required tapping the note's text field and then tapping the "Live Text" option in the pop-up menu. However, Apple is making it easier to digitize real-world notes in 2022. The following steps show you how to do this on a device running iOS 15.4 or iPadOS 15.4 and above. On your iPhone or iPad, open "

In C# development, with the continuous development of network technology, security issues have become more and more serious. To ensure the security of applications, developers need to pay attention to scanning and repairing security vulnerabilities. This article will introduce precautions from the following aspects. 1. Pay attention to data transmission security In applications, data transmission security is very important. Especially when using the network to transmit data, secure protocols and encryption algorithms should be used wherever possible. In order to ensure data integrity and confidentiality, it is recommended to use Secure Socket Layer (SSL) or Transport Layer Security (TLS), etc.
