最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python使用nmap端口扫描的两种方法

来源:动视网 责编:小采 时间:2020-11-27 14:16:14
文档

python使用nmap端口扫描的两种方法

python使用nmap端口扫描的两种方法:第一版:只支持以逗号分隔的端口,不支持端口范围 Firstly: sudo apt-get install nmapSecondly:pip install python-nmapThirdly:copy the code bellow to a file like scan_network.py#!/usr/bin/env pyt
推荐度:
导读python使用nmap端口扫描的两种方法:第一版:只支持以逗号分隔的端口,不支持端口范围 Firstly: sudo apt-get install nmapSecondly:pip install python-nmapThirdly:copy the code bellow to a file like scan_network.py#!/usr/bin/env pyt
 第一版:只支持以逗号分隔的端口,不支持端口范围

Firstly: sudo apt-get install nmap

Secondly:pip install python-nmap

Thirdly:copy the code bellow to a file like scan_network.py

#!/usr/bin/env python
import nmap
import optparse

def nmapScan(tgtHost,tgtPort):
 nmScan = nmap.PortScanner()
 nmScan.scan(tgtHost,tgtPort)
 state=nmScan[tgtHost]['tcp'][int(tgtPort)]['state']
 print ("[*] " + tgtHost + " tcp/"+tgtPort +" "+state)

def main():
 parser = optparse.OptionParser('usage %prog '+
 '-H <target host> -p <target port>')
 parser.add_option('-H', dest='tgtHost', type='string',
 help='specify target host')
 parser.add_option('-p', dest='tgtPort', type='string',
 help='specify target port[s] separated by comma')
 
 (options, args) = parser.parse_args()
 
 tgtHost = options.tgtHost
 tgtPorts = str(options.tgtPort).split(',')
 
 if (tgtHost == None) | (tgtPorts[0] == None):
 print (parser.usage)
 exit(0)
 for tgtPort in tgtPorts:
 nmapScan(tgtHost, tgtPort)


if name == 'main':
 main

Forthly:chmod +x scan_network.py

fifthly: ./scan_network.py -H 192.168.1.1 -p 22,23

第二版:支持以逗号分割及以-分割的端口范围

#!/usr/bin/env python
import nmap
import optparse
def nmapScan(tgtHost,tgtPort):
 nmScan = nmap.PortScanner()
 nmScan.scan(tgtHost,tgtPort)
 state=nmScan[tgtHost]['tcp'][int(tgtPort)]['state']
 print ("[*] " + tgtHost + " tcp/"+tgtPort +" "+state)

def main():
 parser = optparse.OptionParser('usage %prog '+
 '-H <target host> -p <target port>')
 parser.add_option('-H', dest='tgtHost', type='string',
 help='specify target host')
 parser.add_option('-p', dest='tgtPort', type='string',
 help='specify target port[s] separated by comma')

 (options, args) = parser.parse_args()

 tgtHost = options.tgtHost



######this code bellow is to support scan port range like 66-88

 tgtPorts = []
 tgtPorts_cache = str(options.tgtPort).split(',')
 i = int(len(tgtPorts_cache))
 for m in range( 0,i ):
 tgtPorts_split = str(tgtPorts_cache[m]).split('-')
 if(len(tgtPorts_split) < 2):
 tgtPorts.extend(tgtPorts_split)
 #print(tgtPorts)
 else:
 for n in range(int(tgtPorts_split[0]),int(tgtPorts_split[1])+1):
 tgtPorts.append(str(n))
 #print(tgtPorts)

######above the tgtPorts are the ports list you want to scann

 #tgtPorts = str(options.tgtPort).split(',') 
 if (tgtHost == None) | (tgtPorts[0] == None):
 print (parser.usage)
 exit(0)
 for tgtPort in tgtPorts:
 nmapScan(tgtHost, tgtPort)


if name == 'main':
 main()

文档

python使用nmap端口扫描的两种方法

python使用nmap端口扫描的两种方法:第一版:只支持以逗号分隔的端口,不支持端口范围 Firstly: sudo apt-get install nmapSecondly:pip install python-nmapThirdly:copy the code bellow to a file like scan_network.py#!/usr/bin/env pyt
推荐度:
标签: 端口 的两个 python
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top