Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Port scanning based on Python

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Mistakenly deleted the previous shell script, the previous shell script because the scalability is not strong, it is intended to use python to re-implement.

There are many ways to scan the port. I am here to process the results in json format and give them to the back-end django to process.

#! / usr/bin/env python#-*-coding: utf-8-*-"Date:2018-05-14Author:BobDescription:Processing nmap scan results" import osimport timeimport jsonimport requestsimport subprocessfrom xml.etree import ElementTree as ETurl = 'http://10.200.56.80:8000/portscan/portScanInterface/'def remove_file (del_file): if os.path.exists (del_file): os.remove (del_file) return del_filedef alive_ Ip (): # Detecting live ip with open ('ip_subnet.txt' 'r') as f: for ip in f: ip = ip.strip () cmd ='/ usr/bin/nmap-sP-PI-PT% s > > alive_ip.txt'% ip p = subprocess.Popen (cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) print p.stdout.read (), # Scan live ip ips = [] with open ('alive_ip.txt' 'r') as f: for lines in f: if lines.startswith ('Nmap scan'): ip = lines.split ('') ip = ip [4] .strip () ips.append (ip) ip_str = '.join (ips) nmap_scan =' / usr/bin/nmap-sV-oX nmap_scan_output .xml% s > / dev/null 2 > & 1'% ip_str p = subprocess.Popen (nmap_scan Stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) print p.stdout.read (), def deal_scan_xml (xml_file): # Processing nmap scan results bd = open (xml_file,'r'). Read () root = ET.fromstring (bd) data = [] for host in root.findall ('host'): # Print the number of child elements in the host tag # print len (host) # Get ip address ip_ = host [1] .get ('addr') # Get ports, protocols And other information if len (host) = = 5: for port in host [3] [0:]: # print port port_ = str (port.get ('portid')) protocol_ = str (port.get (' protocol')) if port [0] .tag = = 'extrareasons': continue State_ = port [0] .get ('state') service_ = str (port [1] .get (' name')) product_ = str (port [1] .get ('product')) version_ = str (port [1] .get (' version')) extrainfo_ = str (port [1] .get ('extrainfo')) Ip_ = ip_ data.append ({"ip": ip_ "port": port_, "protocol": protocol_, "state": state_, "service": service_, "product": product_, "version": version_, "extrainfo": extrainfo_}) json_data = json.dumps ({"detail": data}) json_data = requests.post (url {"detail": json_data}) print json_data.textdef main (): remove_file ('alive_ip.txt') remove_file (' nmap_scan_output.xml') alive_ip () deal_scan_xml ('nmap_scan_output.xml') if _ _ name__ = =' _ main__': main ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report