Python scans server ports in batches, and python scans server ports in batches.

Source: Internet
Author: User

Python scans server ports in batches, and python scans server ports in batches.

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.

 1 #!/usr/bin/env python 2  3 import sys 4 import time 5 import socket 6  7  8 def getaddresslist(addr): 9     """10     getaddresslist(addr) -> IP address file11 12     IP address read from the file.13     :param addr: IP file14     :return: Scan ip address list, or error message.15     """16     address = []17     try:18         with open(addr, "r") as iplist:19             line = iplist.readlines()20             for item in line:21                 address.append(item.strip("\n"))22         return address23 24     except (IOError, IndexError), e:25         return str(e)26 27 28 def scan(iplist, port=80):29     """30     scan() -> getaddresslist()31 32     getaddresslist() function returns the IP address of the list.33     :param iplist: getaddresslist() Function return value.34     :param port: Need to scan the port.35     :return: None36     """37     if not isinstance(iplist, list):38         sys.exit("Function getaddresslist() return error message: %s" % iplist)39     # start_time = time.time()40 41     for addr in iplist:42         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)43         s.settimeout(1)44         host = (addr, int(port))45 46         try:47             s.connect(host)48             print "Host %s:%s connection success." % (host[0], host[1])49         except Exception, e:50             print "Host %s:%s connection failure: %s" % (host[0], host[1], e)51 52         s.close()53 54     # print "Port scanning to complate, Elapsed time: %.2f" % (time.time() - start_time)55 56 if __name__ == '__main__':57 58     addrs = sys.argv[1]59     # ScanPort = raw_input("Enter the scan port <default is 80 port>: ")60 61     ScanPort = input("Enter the scan port: ")62     Total = input("Enter the scan time <minutes>: ")63     Interval = input("Enter the scanning interval <minutes>: ")64 65     EndTime = time.time() + Total * 6066 67     while time.time() < EndTime:68         scan(getaddresslist(addrs), ScanPort)69         time.sleep(Interval * 60)70         continue71     else:72         print "\nwhile end."

 

This script has a disadvantage. Although servers can be scanned in batches, ports cannot be scanned in batches. Of course, it is also easy to add this function, or which of the gods has a better script can also be posted for learning.

 

Related Article

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.