Use Python to write simple port scanner instances

Source: Internet
Author: User
This article mainly introduces the example of writing a simple port scanner using Python. The article introduces the single-line and multi-thread implementation methods. If you need it, refer Single-threaded implementation
The single-thread implementation principle is relatively simple. Here we try to connect Soket to 3389. If the connection is successful, the port is opened. Otherwise, the remote service is not enabled. If you modify it, the Code is as follows, and the IP address is obtained.

#! /Usr/bin/env python import socket if _ name __= = '_ main _': port = 3389 s = socket. socket () for cnt in range (253,2,-1): address = 'xxx. XXX. XXX. '+ str (cnt) # XXX. XXX. xxx ip cidr block try: s. connect (address, port) print address handle T socket. error, e: print 'error OR Port Not opened'

Python code is simple and clear, but the function is not simple and the speed is slow. The main reason is single-line and network. To improve the speed, you can use a simple port scanner to scan specified network segments and ports. The speed of multithreading may be better.


Multithreading
I read a tutorial about using Python to scan ports a few days ago. After reading this tutorial, I also wrote a script to scan ports. Record it to facilitate future review.
Port Scan Port

Python scanner source code

#-*-Coding: utf8 -*-#! /Usr/bin/python # Python: 2.7.8 # Platform: Windows # Authro: wucl # Program: Port Scan # History: 2015.6.1 import socket, time, threadsocket. setdefatimetimeout (3) def socket_port (ip, port): "Enter the IP address and port number, scan to determine whether the port is open" try: if port> = 65535: print U' Port Scan ended's = socket. socket (socket. AF_INET, socket. SOCK_STREAM) result = s. connect_ex (ip, port) if result = 0: lock. acquire () print ip, U': ', port, U' port open 'lock. release () s. close () failed T: print U' Port Scan exception 'def ip_scan (ip): "Enter IP address, scan port 0-of IP Address" "try: print U' start scanning % s' % ip start_time = time. time () for I in range (): thread. start_new_thread (socket_port, (ip, int (I) print U' port scan is complete, total time: %. 2f '% (time. time ()-start_time) raw_input ("Press Enter to Exit") failed T: print U' ip scan error 'if _ name __= = '_ main __': url = raw_input ('input the ip you want to scan: \ n') lock = thread. allocate_lock () ip_scan (url)

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.