Google IP Usability detection script implemented by Python _python

Source: Internet
Author: User

Python 3.4+ is required, and a parameter is used to select a test search service or a GAE service. Testing GAE services requires modifying the first two variables. Read the IP address or IP segment (like 192.168.0.0/16) list from standard input, one per line. Available IP output to standard output. Real-time test results output to standard error. 50 thread concurrency.

Checkgoogleip

#!/usr/bin/env python3 Import sys from IPAddress import ipv4network import http.client as client from Concurrent.future S import threadpoolexecutor import argparse import SSL Import Socket # First modify the following lines according to your own situation app_id = ' your_id_here ' App_path = '/fetch.py ' context = SSL. Sslcontext (SSL. PROTOCOL_TLSV1) Context.verify_mode = SSL. Cert_required context.load_verify_locations ('/etc/ssl/certs/ca-certificates.crt ') class HTTPSConnection (client. httpsconnection): Def __init__ (self, *args, Hostname=none, **kwargs): Self._hostname = hostname super (). __init__ (*arg S, **kwargs def Connect (self): super (client. Httpsconnection, self). Connect () If Self._tunnel_host:server_hostname = Self._tunnel_host Else:server_hostna me = Self._hostname or self.host sni_hostname = Server_hostname if SSL.
  Has_sni else None self.sock = Self._context.wrap_socket (Self.sock, Server_hostname=sni_hostname) If not self._context.check_hostname and self._check_hostname:tRy:ssl.match_hostname (Self.sock.getpeercert (), server_hostname) except Exception:self.sock.shutdown (socket. SHUT_RDWR) Self.sock.close () Raise def check_ip_p (IP, func): if Func (IP): print (IP, flush=true) def Check_f Or_gae (IP): return _check (app_id + ". Appspot.com ', App_path, IP) def check_for_search (IP): return _check (' www.google.c Om ', '/', IP def _check (host, Path, IP): For chance in Range (1,-1,-1): Try:conn = Httpsconnection (IP, Timeo
   UT = 5, Context = context, hostname = host,) conn.request (' Get ', path, headers = {' Host ': Host,}) Response = Conn.getresponse () if Response.Status < 400:print (' good: ', IP, file=sys.stderr) else:rais E Exception (' HTTP Error%s%s '% (Response.Status, Response.reason)) return True except Keyboardinterrupt:r Aise except Exception as E:if isinstance (E, SSL. Certificateerror): Print (' WARN:%s is not google\ ' s! '% IP, file=sys.stderr) chance =0 if chance = = 0:print (' Bad: ', IP, E, file=sys.stderr) return False else:print (' RE: ', IP, E, file=sy S.STDERR) def main (): parser = Argparse. Argumentparser (description= ' Check Google IPs ') parser.add_argument (' Service ', choices=[' search ', ' Gae '), help= ' Service to check ') args = Parser.parse_args () func = Globals () [' check_for_ ' + args.service] Count = 0 with Threadpo Olexecutor (max_workers=50) as executor:for l in sys.stdin:l = L.strip () if '/' in L:for IP in Ipv4network (l ). Hosts (): Executor.submit (check_ip_p, str (IP), func) Count = 1 else:executor.submit (check_ip_p, L, fun
 c) Count = 1 print ('%d IP checked. '% count) if __name__ = = ' __main__ ': Main ()

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.