日志分析脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/local/python27/bin/python2.7
# coding=utf-8
 
import pygal
import re
import os
import sys
import datetime
 
BASE_PATH = "./"
 
 
def extract(path):
    ip = dict()
    time = dict()
    status = dict()
    version = dict()
    method = dict()
    with open(path) as f:
        for line in f:
            matched = reobj.search(line)
            if not matched:
                continue
            log_obj = matched.groupdict()
            if log_obj.get("ip"not in ip.keys():
                ip[log_obj.get("ip")] = 0
            ip[log_obj.get("ip")] += 1
            origin_time = log_obj.get("time")
            if not origin_time:
                continue
            dt = datetime.datetime.strptime(origin_time,'%d/%b/%Y:%H:%M:%S')
            dt_s = dt.strftime('%Y-%m-%d %H')
            if dt_s not in time.keys():
                time[dt_s] = 0
            time[dt_s] += 1
            if log_obj.get('status'not in status.keys():
                status[log_obj.get('status')] = 0
            status[log_obj.get('status')] += 1
            if log_obj.get('version'not in version.keys():
                version[log_obj.get('version')] = 0
            version[log_obj.get('version')] += 1
            if log_obj.get("method"not in method.keys():
                method[log_obj.get("method")] = 0
            method[log_obj.get("method")] += 1
    return ip,time,status,version,method
 
def make_path(dt):
    path = os.path.join(BASE_PATH,dt)
    if not os.path.exists(path):
        os.makedirs(path)
    return path
 
def time_graph(time,dt):
    chart = pygal.Bar()
    chart.title = "Access of %s" % dt
    keys = ['%s %2d' % (dt,x) for in range(24)]
    values = [time.get(key) for key in keys]
 
    chart.x_labels = [x.split()[1for in keys]
    chart.add("Access",values)
    path = make_path(dt)
    chart.render_to_file(os.path.join(path,"time.svg"))
 
 
def status_graph(status,dt):
    chart = pygal.Pie()
    chart.title = "Status of %s" % dt
    for k,v in status.items():
        chart.add(k,v)
    path = make_path(dt)
    chart.render_to_file(os.path.join(path,"status.svg"))
 
 
 
def version_graph(vsersion,dt):
    chart = pygal.Pie()
    chart.title = "versions of %s" % dt
    for k,v in version.items():
        chart.add(k,v)
    path = make_path(dt)
    chart.render_to_file(os.path.join(path,"version.svg"))
 
 
def method_graph(method,dt):
    chart = pygal.Pie()
    chart.title = "methods of %s" % dt
    for k,v in method.items():
        chart.add(k,v)
    path = make_path(dt)
    chart.render_to_file(os.path.join(path,"method.svg"))
 
if __name__ == '__main__':
    log_file = sys.argv[1]
    dt = sys.argv[2]
    ip,time,status,version,method = extract(log_file)
    time_graph(time,dt)
    status_graph(status,dt)
    version_graph(version,dt)
    method_graph(method,dt)


文件差异对比:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/local/python27/bin/python2.7
import difflib
import sys
 
try:
    textfile1=sys.argv[1]
    textfile2=sys.argv[2]
except e:
    print("Error:" + str(e))
    sys.exit()
 
def readfile(filename):
    try:
        fileHandle = open(filename,'rb')
        text=fileHandle.read().splitlines()
        fileHandle.close()
        return text
 
    except IOError as a:
        print("Error:" + str(a))
        sys.exit()
 
if textfile1 =="" or textfile2=="":
    print('Usage:filediif.py filename1 filename2')
    sys.exit()
 
text1_lines = readfile(textfile1)
text2_lines = readfile(textfile2)
 
= difflib.HtmlDiff()
= d.make_file(text1_lines,text2_lines)
print(a)

输出结果:

./filediff.py aa1.txt aa2.txt >diff.html

wKioL1X4yHCRLwFyAACXbe1qGgE183.jpg

HTTP访问脚本

1
2
3
4
5
6
#!/usr/local/python27/bin/python2.7
import urllib2
 
req = urllib2.urlopen('http://weather.yahooapis.com/forecastrss?w=2151849&u=c')
 
print(req.read())


HTTP状态检测—适用于单域名多主机DNS负载均衡场景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/local/python27/bin/python2.7
import dns.resolver
import os
import httplib
 
iplist = []
 
appdomain='www.hello.com'
 
def get_iplist(domain=""):
    try:
        = dns.resolver.query(domain,'A')
    except e:
        print("dns resolver error:" + str(e))
        return
 
    for in A.response.answer:
        for in i.items:
            iplist.append(j.address)
    return True
 
def checkip(ip):
    checkurl=ip+":80"
    getcontent=""
    httplib.socket.setdefaulttimeout(5)
    conn=httplib.HTTPConnection(checkurl)
 
    try:
        conn.request("GET","/",headers = {"Host": appdomain})
 
        = conn.getresponse()
        getcontent = r.read(15)
    finally:
        if getcontent == "<!DOCTYPE html>":
            print (ip+" [OK]")
        else:
            print (ip+" [Error]")
 
if __name__ == "__main__":
    if get_iplist(appdomain) and len(iplist)>0:
        for ip in iplist:
            checkip(ip)
    else:
        print("dns resolver error")


DNS解析脚本

1
2
3
4
5
6
7
8
#!/usr/local/python3/bin/python3
import dns.resolver
 
domain = input('please input an domain: ')
= dns.resolver.query(domain,'A')
for in A.response.answer:
    for in i.items:
        print(j.address)


MX记录解析

1
2
3
4
5
6
7
#!/usr/local/python3/bin/python3
import dns.resolver
domain = input('please input an domain: ')
#这里接收两个参数的传入,需要解析的域名,解析的记录类型;
MX = dns.resolver.query(domain,'MX')
for in MX:
    print('MX preference =', i.preference, 'mail exchanger = ', i.exchange)