码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-03-26 13:46:17      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:div   ==   env   service   app   def   pen   import   类型转换   

安装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模块

标签:div   ==   env   service   app   def   pen   import   类型转换   

原文地址:https://www.cnblogs.com/kunspace/p/10599256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!