nmap端口扫描结果不同,nmap端口扫描状态有哪些

  

  使用python模块nmap,读取excel数据,扫描多个ip端口,弹指一挥间,每个人都少了一个睾丸。   

  

  1.工欲善其事,必先利其器。安装python-nmap模块。检查pip list #cmd或powershell下已安装的模块。至于pip命令环境变量,自己解决pip安装python-nmap。   

  

  写一个简单的nmap单端口运行,看看效果。‘’如果python-nmap模块安装后无法使用,那就自己安装nmap图形界面,然后添加到环境变量中,这样就ok了。我的是这样的;' ' '导入nmap np=nmap。port scanner()a=NP . scan(hosts=' 42 . 247 . 22 . 192 ',ports='80 ',arguments='-v-n-T4 ')print(a)   

  

     

  

  结果:(我用红色标出了重要的地方,主要是看返回端口的状态和服务名)   

  

  { ' nmap ' : { ' command _ line ' : ' nmap-oX--p 80-v-n-T4 42 . 247 . 22 . 192 ',' scaninfo ' : { ' TCP ' : { ' method ' : ' syn ',' services': '80'},' scanstats ' : { ' timestr ' : ' 2011年6月10日星期四现在,对获得的数据的输出进行整理,使其看起来简洁。在“扫描”“TCP”中为I导入nmap NP=nmap . portscanner()A=NP . scan(hosts=' 42.247.22.192参数='-v-n-T4') IP=' 42.247.22.192 '。keys()3360 state=a ' scan ' ' TCP ' ' state ' name=a ' scan ' ' TCP ' '   

  

  看起来整洁多了。   

  

  后来,关于excel阅读和   

线程的代码我直接贴了,今天事情多不细说了。。

  

3、使用python调用excel模块和线程模块,读取excel数据,对多条数据进行扫描。import nmapimport threadingfrom openpyxl import load_workbookfrom xlwt import Workbookwk = Workbook(encoding='utf-8')wsheet = wk.add_sheet('Worksheet')co = {}ls = <>def read_excel_file(): wb = load_workbook('test/diqu.xlsx') #读取excel文件 # sheets = wb.get_sheet_names() # print(sheets) sheet = wb<'暴露面资产全量'> # print(sheet) m = sheet<'G'> #读取excelG列,我的G列是 ip:port,例:1.1.1.1:80 for cell in m: #这个for循环用于分割ip和端口,存到co字典 # print(cell.value) mn = cell.value.split(':') if mn<0> in co: co>.append(mn<1>) else: try: co> = > except: co> = <>def thread(ip_port): # 设置线程 thread_num = threading.Semaphore(20) # 设置线程数 thread_list = <> for IP, port in ip_port.items(): # 创建线程 t = threading.Thread(target=nmap_ping_scan, args=(IP, port, thread_num,)) thread_list.append(t) # print(t) for t in thread_list: # 开始线程 t.start() for t in thread_list: # 等待线程 t.join() print('线程结束')def nmap_ping_scan(ip,port,thread_num): #使用nmap扫描,结果存入ls列表 global ls strport = ','.join(ports for ports in port) thread_num.acquire() # 线程锁 try: nm = nmap.PortScanner() global result np = nm.scan(hosts=ip,ports=strport,arguments="-v -n -T4") for i in np<'scan'><'tcp'>.keys(): state = np<'scan'><'tcp'><'state'> name = np<'scan'><'tcp'><'name'> ls.extend(<>) # print(ip,i,state) except Exception as e: # print(e) pass thread_num.release()def excel_write(ls): #把ls列表的数据保存到新的excel中 try: for u in range(len(ls)): p = 0 for k in ls: wsheet.write(u,p,k) p += 1 # print(u,p,k) except: passif __name__ == '__main__': #程序启动 read_excel_file() thread(co) excel_write(ls) # print(ls) wk.save('ceshi.xls') # nmap_dan_scan(co) # print(ls)#ok,上述就是全部代码了,上面是开了线程的,下面再加个单线程的方法吧#使用方法,把def thread 和 def nmap_ping_scan 注释掉# 再最后if里把 nmap_dan_scan(co) 注释解掉,上面俩个调用注释掉就行。def nmap_dan_scan(ip_port): #单线程跑跑 for ip,port in ip_port.items(): strport = ','.join(ports for ports in port) try: nm = nmap.PortScanner() np = nm.scan(hosts=ip,ports=strport,arguments="-v -n -T4") for i in np<'scan'><'tcp'>.keys(): state = np<'scan'><'tcp'><'state'> print(ip,i,state) except: pass 扫描的结果大概就是这个样子。

  

#注#如果不想使用脚本这么麻烦的话,建议使用masscan命令比较简洁,这个命令有时扫描会存在波动,大多情况下还是不错的;(我是在centos下运行的)

  

写个平时用的栗子:masscan -p0-65535 -iL ip.txt --rate=2000 > masscan-scan.txt

相关文章