python3 port scanning script

Ah Long time did not write the code, thinking multithreaded what are almost forgotten 
on the following matter,
#coding=utf-8
import socket,threading
from socket import *

def tcp_connect(ip,port):
    tcpsock=socket(AF_INET,SOCK_STREAM)
    tcpsock.settimeout(0.5)
    try:
        result=tcpsock.connect_ex((ip,port))
        if(result==0):
            print ("%s:%s 开启" % (ip, port))
        else:
            #print ("%s:%s 关闭" % (ip, port))
            pass
        tcpsock.close()
    except socket.error as e:
        print ("[!]错误",e)

def url_ip(url):
    if('http://' in url or 'https://' in url):
        url=url[url.find('://')+3:]
        targetip = socket.getaddrinfo(url, 'http')[0][4][0]
        print (targetip)
        port_scan(targetip)
    else:
        targetip Socket.getaddrinfo = (URL, ' HTTP ' ) [0] [. 4 ] [0]
         Print (TargetIP) 
        port_scan (TargetIP) 

DEF port_scan (IP):
     for I in Range (200 is ): 
        the tcp_connect (IP, I) 

DEF main ():
     Print  " ------------- " 
    Print  " . 1: IP port scan " 
    Print  " 2: scan port URL " 
    Print  " ------------- " 
    Q = the raw_input ( " select: " )
     IF (int (Q). 1 ==):
        w=raw_input("ip=")
        t=threading.Thread(target=port_scan,args=(w,))
        t.start()
    else:
        e=raw_input("url=")
        url_ip(e)
main()
 
 

 



#
!/usr/bin/python3 # -*- coding: utf-8 -*- from socket import * import threading lock = threading.Lock() openNum = 0 threads = [] def portScanner(host,port): global openNum try: s = socket(AF_INET,SOCK_STREAM) s.connect((host,port)) lock.acquire() openNum+=1 print('[+] %d open' % port) lock.release() s.close() except: pass def main(): setdefaulttimeout(1) for p in range(1,6558): t = threading.Thread(target=portScanner,args=('14.215.177.38',p)) threads.append(t) t.start() for t in threads: t.join() print('[*] The scan is complete!') print('[*] A total of %d open port ' % (openNum)) if __name__ == '__main__': main()

 

This is the coroutine

# - * - Coding: UTF-. 8 - * - 
# @time: 2018/9/7 11:07 
# @author: gumgui 
# @file: port scanning .py 
Import Time
 Import Socket
 from GEVENT Import Monkey 
monkey.patch_all () 
Import GEVENT
 Import gevent.pool 


DEF tcp_connect (IP, Port):
     "" " simulated TCP connections " "" 
    TCP_sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 
    TCP_sock.settimeout ( 0.5)   # set connection timeout 
    the try : 
        Result =TCP_sock.connect_ex ((ip, int (Port)))
         IF the Result == 0:
             Print ( " [*]% S port open \ t " % Port)
         the else :
             # Print ( "% S port is closed [!]"% Port) 
            Pass 
        TCP_sock.close () 
    the except socket.error AS E:
         Print ( " error [!]: " , E)
 DEF scan_ip ():
     "" " scan targets the IP " "" 
    IP = the raw_input ( " [+] input scan target IP: " )
     Print ( "[*] Scanning ") 
    Scan_port (ip) 

DEF scan_web ():
     "" " Scan Destination URL " "" 
    Web = raw_input ( " [+] input scan URL: " )
     IF  " HTTP: // "  in Web or  " HTTPS: // "  in Web: 
        Web = Web [web.find ( ' : // ' ) +3 :]
         Print (Web)
         Print ( " [*] are analyzing web server IP " )
     the try : 
        server_ip =socket.gethostbyname (STR (Web))
         Print ( " [*] is the IP server S% " % server_ip) 
        scan_port (server_ip) 
    the except Exception AS E:
         Print ( " [!] Server IP acquisition failure " )
         Pass 


DEF scan_port (IP ):
     "" " port scanning " "" 
    Print ( " [*] to start scanning the target port " ) 
    start = time.time () 
    G = gevent.pool.Pool (50) # set the number of threads 
    run_list = []
     for port inRange (1,200 ): 
        run_list.append (g.spawn (tcp_connect, IP, Port)) 
    gevent.joinall (run_list) 
    End = the time.time ()
     Print ( " [*] Processed Total S% " % The time.strftime ( " % H:% M:% S " , time.gmtime (END- Start))) 


DEF main ():
     Print (
      "" " 
    1. by scanning IP port 
    2. port scanning by the URL 
    " "" 
    ) 
    UC = int (the raw_input ( " [+] enter select: " ))
     IF . 1 == UC: 
        scan_ip () 
    elif 2 ==uc: 
        scan_web () 
    the else :
         Print ( " you make a mistake [!] " ) 

IF  __name__ == " __main__ " : 
    main ()

 

Guess you like

Origin www.cnblogs.com/Tkitn/p/11622803.html