端口扫描器--利用python的nmap模块

Posted kunspace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了端口扫描器--利用python的nmap模块相关的知识,希望对你有一定的参考价值。

安装nmap模块挺麻烦的,搞了半天

不仅要安装pip install nmap

还要sudo apt install nmap

给出代码,没有设多线程,有点慢,注意端口的类型转换,搞了很久

#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import optparse
import nmap

#usage:python scan.py -H 119.75.217.26 -p 3306 80 22

def nampScan(tgtHost,tgPort):
    nmScan = nmap.PortScanner()
    try:
        result = nmScan.scan(tgtHost,str(tgPort))
        state =  result[scan][tgtHost][tcp][int(tgPort)][state]
        service = result[scan][tgtHost][tcp][int(tgPort)][product]
        version = result[scan][tgtHost][tcp][int(tgPort)][version]
        print "[*]" + tgtHost + " tcp/" + str(tgPort) + " " + state + " " + service + " " + version
    except:
        pass


def main():
    parse = optparse.OptionParser(usage %prog -H <targethost> -p <targetport>)
    parse.add_option(-H,dest = tgtHost,type = string,help = specify target host)
    parse.add_option(-p,dest = tgtPort,type = int,help = specify target port)
    (option,args) = parse.parse_args()
    tgtHost = option.tgtHost
    tgtPort = str(option.tgtPort)
    args.append(tgtPort)
    if(tgtHost == None)|(tgtPort == None):
        print "[-]usage : python scan.py -H 192.168.2.3 -p 3306 80 22"
        exit(0)
    for tgport in args:
        nampScan(tgtHost,tgport)

if __name__ == __main__:
    main()

 

以上是关于端口扫描器--利用python的nmap模块的主要内容,如果未能解决你的问题,请参考以下文章

python学习-python-nmap实现高效的端口扫描器

利用nmap进行特定端口扫描

python模块学习----nmap模块

Python 第三方模块pythonnmap来实现高效的端口扫描

python-nmap 端口扫描示例

python实现端口状态扫描