Python Implements port detection

Source: Internet
Author: User

First, background:

In peacetime work has encountered port detection, to see whether the service port specific ports are open, commonly used nmap,tcping,telnet, etc., but also can use webmaster tools such as web scanning ports.
But when using webmaster tools to find:

    • Only one detected address can be entered at a time;
    • Although you can enter multiple ports, you cannot specify a port range for batch detection;
    • No bulk task logging, etc.;

Python-nmap is not used because it avoids port detection anomalies caused by LAN detection of initiator network limitations
You want to do this by calling the Webmaster Tools

    • Multiple addresses or domain names can be detected in a single time
    • Word can specify port range, batch detection
    • Record log
Two, code: 2.1 Structure

2.2 Code

GitHub Address
Part of the Code

#配置文件 # Port detection configuration [Port_check_info] #检测ip地址或域名 #address = baidu.com#address = 127.0.0.1address = www.anchnet.com, www.51cto.com,www.baidu.com# check ports, such as multiple ports used, separated, port ranges using '-' #ports = 80,8080....ports = 20-25,80,443,1433,1521,3306,3389,6379,8080,27017# log configuration [loginfo] #日志目录logdir_name = logdir# log file name logfile_name = Check _port.log def _get_body (self): "" "Get Address and port:return:list" "Address_list = SE        Lf.address_list.split (', ') port_list = Self.port_list.split (', ') # handles the port range, returns range Range_flag = False                Port_range = None Content_list_range = [] for port in Port_list:if '-' in port:                Range_flag = True Port_range = range (int (port.split ('-') [0]), int (port.split ('-') [1]) +1) Port_list.remove (port) # processing overall list for add in Address_list:if range_flag:for PO    RT in Port_range:content_List_range.append (add + ': ' + str (port))    # Merge range and normal List content_list = [add+ ': ' +port for Add in address_list for Port in Port_list] content_l         Ist_range.extend (content_list) return Content_list_range def run (self): "" "for Port Detection: return:                "" "for content in Self._get_body (): Content_list = Content.split (': ') BODY = { ' Host ': content_list[0], ' Port ': content_list[1], ' encode ': ' Tlchs1u3igf4sc57m6kop 3OAJ1Y1KFLQ '} try:response = Requests.post (url=self.url,data=body,headers=self.head                    ERS) Port_status = Re.findall ("msg: ' (. *?) '", Response.text) If Len (Port_status) > 0: # print ('%s,port status is:%s '% (content, port_status)) self.logoper.info ('%s,port stat US is:%s '% (content, port_status)) Else:self.logoper.info ('%s,port status is:%s '% (CO    Ntent, Port_status))                # print (' Occer error! Please enter the correct address and Port ') except Exception as E:print (e)
III. Test: 3.1 View test results


Here you can see that the 8080 port of 51cto is also open.

3.2 Viewing logs

Iv. Improvement:
    • Late can add asynchronous multi-process, etc. to improve efficiency
    • Can compare multiple site detection results to make the results more accurate
    • Integrated Nmap intranet can also be detected

Python Implements port detection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.