"Original" Python gettitle.py

Source: Internet
Author: User

#!/usr/bin/env python# coding=utf-8import threadingimport requestsimport queueimport sysimport reimport timeimport Warningsimport datetimeimport argparse__author__ = ' Depycode ' warnings.filterwarnings ("ignore") #ip to numdef ip2num (IP    ): IP = [INT (x) for x in Ip.split ('. ')] Return ip[0] << 24 | IP[1] << 16 | IP[2] << 8 | IP[3] #num to ipdef num2ip (num): Return '%s.%s.%s.%s '% (num & 0xff000000) >>24, (                            Num & 0x00ff0000) >>16, (Num & 0x0000ff00) >>8, Num & 0x000000ff) #def ip_range (Start, end): return [Num2ip (num) for NUM in range (Ip2num (start), Ip2num (end) + 1) I F Num & 0xFF] #def bthread (iplist): Threadl = [] queue = Queue.queue () for host in Iplist:queue.put (ho    ST) for X in Xrange (0, Int. (setthread)): Threadl.append (TThread (queue)) for T in Threadl:t.start () For T in Threadl:t.join () #create ThreaDclass TThread (Threading. Thread): Def __init__ (self, queue): Threading. Thread.__init__ (self) self.queue = Queue def run (self): And not Self.queue.empty (): host = s Elf.queue.get (Block=false) try:checkserver (host) Except:continuedef Checkserver (host): UA = {' user-agent ': ' mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/47.0.2526.106 safari/537.36 '} ports = [80,81,82,83,84,85,86,87,88, 89,90,91,92,93,94,95,96,97,98,99,8000,8001,8002,8080,8081,8082,8083,8084,8085,8086,8087,8088,8089,8090,8888,9002,443,873 , 2601,2604,4848,8008,8880,9999,3128,5432,2049,7001,9200,9871,4440,6082,8099,8649,9000,9090,50000,50030,50070] #                Ports = [80,8080] for k in ports:try:if k==443:aimurl = "https://" +host #print Aimurl response = requests.get (url = aimurl,headers = Ua,verify=false,timeout = 8) el Se               Aimurl = "http://" +host+ "+str (k) #print aimurl response = requests.get (URL            = Aimurl,headers = Ua,timeout = 8) #print response.headers status = Response.status_code            Try:servertext = response.headers[' server '] Except:servertext = "" Try:titletext = Re.findall (R ' <title> (. *?) </title> ', Response.content.decode (' utf-8 ', ' ignore '). Encode (' utf-8 ', ' ignore ') [0] Except:t Itletext = "" SaveData = {"IP": Host, "Port": Str (k), ' Aimurl ': Aimurl, "status": Status, "Server": Servertext, "title": T Itletext} print SaveData data.append (saveData) except:passdef cmd (): iplist_a = [] Parser = Argparse. Argumentparser (description= ' GET TITLE: Author::d epycode ') group = Parser.add_mutually_exclusive_group () group.add_argument ('-I ', act Ion= "Store",                       Dest= "IPRange", help= "useage:: Python gettitle.py-i 10.100.1.1-10.100.1.254                        "," group.add_argument ('-f ', action= "store", dest= "Ipfile", help= "Usage:: Python gettitle.py-f ip.txt", type=str,) args = Parser.parse_ar     GS () ipfile = args.ipfile IP = args.iprange if ip:iplist_a = Ip_range (Ip.split ('-') [0], Ip.split ('-') [1]) Elif ipfile:iplist_tmp = open (ipfile). ReadLines () for I in Iplist_tmp:iplist_a.append (i.str IP ()) Else:parser.print_help () exit () Return IPLIST_ADEF report (data): T = Time.strftime (' %y-%m-%d-%h-%m ', Time.localtime (Time.time ())) F = open (' Title ' + '-' +str (t) + '. html ', ' w+ ') table1 = "<meta http-equiv = ' Content-type ' content= ' text/html; Charset=utf-8 ' ><table border= ' 1 ' >\n<tr><th>url</th><th>stauts_code</th><th>server</th><th>title</th>\n "F.write (table1) for i in data:rows =" <tr>&lt ; td><a target= ' _blank ' href= '%s ' >%s</a></td><td>%s</td><td>%s</td>        <td>%s</td></tr>\n "% (i[' aimurl '],i[' IP ']+": "+i[' Port '],i[' status '],i[' server '],i[' title ']) F.write (rows) table2 = "</table>" F.write (table2) f.close () def report2txt (data): T = time.strftime ('%y-%        M-%d-%h-%m ', Time.localtime (Time.time ())) F = open ("ip-" +t+ ". txt", "w+") for i in data:url = i[' Aimurl ']     F.write (URL) f.write ("\ n") f.close () if __name__ = = ' __main__ ': Global setthread global data data = [] StartTime = Datetime.datetime.now () try:setthread = IPList = cmd () print ' \n[info] would s         Can ' +str (len (iplist)) + "host...\n" Bthread (iplist) except Keyboardinterrupt:print ' Keyboard interrupt! ' Sys.exit () report (Data) rePort2txt (Data) Endtime = Datetime.datetime.now () print "finished in" +str ((endtime-starttime). seconds) + "S" 

  

usage:getTitle.py [-h] [-I iprange |-f ipfile]get TITLE. Author::d epycodeoptional arguments:  -H,--help show this help message and exit-I  iprange  useage:: Python gettitle.py-i 10.100.1.1-10.100.1.254-  F ipfile   usage:: Python gettitle.py-f ip.txt

"Original" Python gettitle.py

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.