Python batch scan server port 80 Status, python scan port 80

Source: Internet
Author: User

Python batch scan server port 80 Status, python scan port 80

I wrote a simple Port Scan script in Python. The simple logic is as follows:

1. python DetectHostPort. py iplist.txt (stores the text of the IP address list to be scanned, one address per line)

2. Enter the scan port, scan time, and scan interval.

3. Output scan information.

 

The source code is pasted below. You are welcome to make a brick.

#!/usr/bin/env pythonimport sysimport timeimport socketdef getaddresslist(addr):    """    getaddresslist(addr) -> IP address file    IP address read from the file.    :param addr: IP file    :return: Scan ip address list, or error message.    """    address = []    try:        with open(addr, "r") as iplist:            line = iplist.readlines()            for item in line:                address.append(item.strip("\n"))        return address    except (IOError, IndexError), e:        return str(e)def scan(iplist, port=80):    """    scan() -> getaddresslist()    getaddresslist() function returns the IP address of the list.    :param iplist: getaddresslist() Function return value.    :param port: Need to scan the port.    :return: None    """    if not isinstance(iplist, list):        sys.exit("Function getaddresslist() return error message: %s" % iplist)    # start_time = time.time()    for addr in iplist:        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        s.settimeout(1)        host = (addr, int(port))        try:            s.connect(host)            print "Host %s:%s connection success." % (host[0], host[1])        except Exception, e:            print "Host %s:%s connection failure: %s" % (host[0], host[1], e)        s.close()if __name__ == '__main__':    addrs = sys.argv[1]    ScanPort = input("Enter the scan port: ")    Total = input("Enter the scan time <minutes>: ")    Interval = input("Enter the scanning interval <minutes>: ")
EndTime = time.time() + Total * 60 while time.time() < EndTime: scan(getaddresslist(addrs), ScanPort) time.sleep(Interval * 60) continue else: print "\nwhile end."

Only one port can be scanned during running, but the code can be modified to scan multiple ports.

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.