【原创】python gettitle.py

#!/usr/bin/env python
# coding=utf-8

import threading
import requests
import Queue
import sys
import re
import time
import warnings
import datetime
import argparse

__author__ = ‘depycode‘

warnings.filterwarnings("ignore")

#ip to num
def ip2num(ip):
    ip = [int(x) for x in ip.split(‘.‘)]
    return ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3]

#num to ip
def 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) if num & 0xff]

#
def bThread(iplist):

    threadl = []
    queue = Queue.Queue()
    for host in iplist:
        queue.put(host)

    for x in xrange(0, int(SETTHREAD)):
        threadl.append(tThread(queue))

    for t in threadl:
        t.start()
    for t in threadl:
        t.join()

#create thread
class tThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):

        while not self.queue.empty():
            host = self.queue.get(block=False)
            try:
                checkServer(host)
            except:
                continue

def 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)
            else:
                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:
                titleText = ""

            saveData = {"ip":host,"port":str(k),‘aimurl‘:aimurl,"status":status,"server":serverText,"title":titleText}
            print saveData
            Data.append(saveData)
        except:
            pass

def cmd():
    iplist_a = []
    parser = argparse.ArgumentParser(description=‘GET TITLE .. Author::depycode‘)
    group = parser.add_mutually_exclusive_group()

    group.add_argument(‘-i‘,
                        action="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_args()
    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.strip())

    else:
        parser.print_help()
        exit()
    return iplist_a

def 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><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 = 200

        iplist = cmd()

        print ‘\n[INFO] Will scan ‘+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::depycode

optional 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

时间: 2024-10-10 12:04:08

【原创】python gettitle.py的相关文章

Python pydoc.py

1. 查看帮助,我们可以在python命令行交互环境下用 help函数,比如: 查看 math 模块: >>> help('math')Help on built-in module math: NAME math DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(..

django 运行python manage.py sqlall books 时报错 app has migration

出现这个问题的原因是版本之前的不兼容,我用的django版本是1.8.6 而 这条python manage.py sqlall books 是基于django1.0版本的. 在django1.8.6版本中生成一个表的语句是    $ python manage.py makemigrations books $ python manage.py sqlmigrate books 0001 django book2 是一本不错的教程,但是就是版本太老了,可以通过看book2对django有一个比

python/c++接口库比较(SWIG,boost.python, pycxx, py++, sip, Weave, Pyrex )

<python/c++接口库比较(SWIG,boost.python, pycxx, py++, sip, Weave, Pyrex )>http://blog.csdn.net/lainegates/article/details/19565823 目前有很多开源的Python/c++ 绑定工具,搜了好多岾子,这里稍微总结下. SWIG 支持 Python 2 and 3 配置正确的话,可以全自动完成封装(*.i文件需要自己写) 当不是全自动的时候,它大多会重复你的.h文件并给出提示 除了P

python manage.py shell之后的一些错误:

1. 在执行python manage.py shell之后的一些错误: [email protected]:~/myobject$ python manage.py shell /usr/lib/python2.7/dist-packages/pkg_resources.py:1049: UserWarning: /home/wyl/.python-eggs is writable by group/others and vulnerable to attack when used with

python安装Fabrix出现Command &quot;python setup.py egg_info&quot;错误

python安装Fabrix出现Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ynJnAl/pynacl/ 解决: 向上查看发现No package 'libffi' found,原因是这个libffi库未安装 所以使用:yum install libffi-devel -y 最后安装成功

python __init__.py用途

转自http://www.cnpythoner.com/post/2.html Python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的.py文件所在的目录,然后import.这样的做法,对于少数文件是可行的,但如果程序数目很 多,层级很复杂,就很吃力了.有没有办法,像Java的Package一样,将多个.py文件组织起来,以便在外部统一调用,和在内部互

python web.py

web.py 1.安装: wget http://webpy.org/static/web.py-0.37.tar.gz tar xvfz web.py-0.37.tar.gz cd web.py-0.37 sudo python setup.py install 2.需求: 先说下需求,http://10.75.7.237:8080/?ipaddress=10.75.7.29&count=100 ,最终得到这个ip的日志的前100行 3.讲解: #!/usr/bin/python import

python __init__.py

python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的.py文件所在的目录,然后import.这样的做法,对于少数文件是可行的,但如果程序数目很 多,层级很复杂,就很吃力了.有没有办法,像Java的Package一样,将多个.py文件组织起来,以便在外部统一调用,和在内部互相调用呢?答案是有的.主要是用到python的包的概念,python __ini

解决安装ipython时Command &quot;python setup.py egg_info&quot; failed with error code 1 in /tmp

最近使用ubuntu16.04 server版安装ipython的时候一直在报错: IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.      When using Python 2.7, please install IPython 5.x LTS Long Term Support version. python2.7 ipython Command "python setup.py egg_info"