Examples of multithreaded port scanning capabilities implemented by Python

Source: Internet
Author: User
Tags print print
The example in this paper describes the multi-threaded port scanning function implemented by Python. Share to everyone for your reference, as follows:

The following program gives the Python code for multithreaded scanning of a given IP host

#!/usr/bin/env python#encoding:utf-8import socket, sys, thread, Timeopenportnum = 0socket.setdefaulttimeout (3) def Usage (): print "" Usage:scan the port of one Ip:python port_scan_multithread.py-o <ip> Scan the port of one Ip:python port_scan_multithread.py-m <ip1, IP2, IP3, ip4 ...> ' print ' Exit ' Sys.exit (1) def socket_port (IP, P ORT): Global openportnum If port > 65535:print ' port scanning beyond the port range, interrupt to scan ' Sys.ex It (1) s = socket.socket (socket.af_inet, socket. SOCK_STREAM) result = S.CONNECT_EX ((IP, PORT)) if (result = = 0): Print IP, PORT, ' is open ' openportnum + = 1 s.close () def start_scan (IP): for port in range (0, 65535+1): Thread.start_new_thread (socket_port, (IP, int (port))) Time.sle EP (0.006) If __name__ = = ' __main__ ': t = 0 if Len (sys.argv) <2 or sys.argv[1] = = '-H ': usage () elif sys.argv[1] = = '-o ': one_ip = raw_input (' Please input IP of scanning: ') T = time.time () start_scan (ONE_IP) elIf sys.argv[1] = = '-M ': many_ip = raw_input (' Please input many IP of scanning: ') ip_seg = Many_ip.split (', ') T = Time.time () for I in Ip_seg:start_scan (i) Print print ' Total open port was%s, scan used time is:%f '% (OPENP Ortnum, Time.time ()-T)

Run

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.