Skip to content

Commit 56cc16e

Browse files
committed
all python scripts
1 parent d2afb28 commit 56cc16e

37 files changed

Lines changed: 3653 additions & 0 deletions

‎scripts/apache_parcer.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
4+
import re
5+
from collections import defaultdict, namedtuple
6+
7+
format_pat= re.compile(
8+
r"(?P<host>[\d\.]+)\s"
9+
r"(?P<identity>\S*)\s"
10+
r"(?P<user>\S*)\s"
11+
r"\[(?P<time>.*?)\]\s"
12+
r'"(?P<request>.*?)"\s'
13+
r"(?P<status>\d+)\s"
14+
r"(?P<bytes>\S*)\s"
15+
r'"(?P<referer>.*?)"\s' # [SIC]
16+
r'"(?P<user_agent>.*?)"\s*'
17+
)
18+
19+
Access = namedtuple('Access',
20+
['host', 'identity', 'user', 'time', 'request',
21+
'status', 'bytes', 'referer', 'user_agent'] )
22+
23+
def access_iter( source_iter ):
24+
for log in source_iter:
25+
for line in (l.rstrip() for l in log):
26+
match= format_pat.match(line)
27+
if match:
28+
yield Access( **match.groupdict() )

‎scripts/apache_parser.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
4+
import sys
5+
6+
def dict_logline(line):
7+
split_line = line.split()
8+
return {'remote_host': split_line[0],
9+
'status': split_line[8],
10+
'bytes_sent': split_line[9]
11+
}
12+
13+
def generate_log_report(logfile):
14+
report_dict = {}
15+
for line in logfile:
16+
line_dict = dict_logline(line)
17+
print line_dict
18+
try:
19+
bytes_sent = int(line_dict['bytes_sent'])
20+
except ValueError:
21+
continue
22+
report_dict.setdefault(line_dict['remote_host'], []).append(bytes_sent)
23+
return report_dict
24+
25+
if __name__ == "__main__":
26+
if not len(sys.argv) > 1 :
27+
print __doc__
28+
sys.exit(1)
29+
infile_name = sys.argv[1]
30+
try:
31+
infile = open(infile_name, 'r')
32+
except IOerror:
33+
print __doc__
34+
sys.exit[1]
35+
36+
log_report = generate_log_report(infile)
37+
print log_report
38+
infile.close()
39+

‎scripts/bwspeed_http.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
import time
5+
import socket
6+
from threading import Thread
7+
8+
host = '184.173.198.48'
9+
port = 80
10+
11+
thread_count = 10
12+
13+
class Controller:
14+
def __init__(self):
15+
self.count_ref = []
16+
def start(self):
17+
for i in range(thread_count):
18+
agent = Agent(self.count_ref)
19+
agent.setDaemon(True)
20+
agent.start()
21+
print 'started %d threads' % (i + 1)
22+
while True:
23+
time.sleep(1)
24+
line = 'connects/sec: %s' % len(self.count_ref)
25+
self.count_ref[:] = []
26+
sys.stdout.write(chr(0x08) * len(line))
27+
sys.stdout.write(line)
28+
29+
class Agent(Thread):
30+
def __init__(self, count_ref):
31+
Thread.__init__(self)
32+
self.count_ref = count_ref
33+
def run(self):
34+
while True:
35+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36+
try:
37+
s.connect((host, port))
38+
s.shutdown(2)
39+
s.close()
40+
self.count_ref.append(1)
41+
except:
42+
print 'SOCKET ERROR\n'
43+
44+
if __name__ == '__main__':
45+
controller = Controller()
46+
controller.start()
47+

‎scripts/charts.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
import random
4+
import urllib
5+
6+
def list2String(x):
7+
""" from a list like [1,2,5]
8+
return a string like '1,2,5' """
9+
data = ""
10+
for i in x:
11+
data += str(i)+","
12+
return data[0:len(data)-1]
13+
14+
def makeChart(x,y,filename):
15+
query_url = "http://chart.apis.google.com/chart?chxt=x,y&chs=300x200&cht=s&chd=t:"
16+
query_url += list2String(x)+"|"+list2String(y)
17+
chart = urllib.urlopen(query_url) # retrieve the chart
18+
print "saving",query_url
19+
f = open(filename,"wb")
20+
f.write(chart.read()) # save the pic
21+
f.close()
22+
23+
x = random.sample(range(0,100),10) # list with
24+
y = random.sample(range(0,100),10) # random values in [0 100[
25+
makeChart(x,y,"chart.png")

‎scripts/checknet.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import urllib2
2+
import sys
3+
4+
def internet_on():
5+
try:
6+
response = urllib2.urlopen('http://google.com', timeout = 3)
7+
print True
8+
except urllib2.URLError as err:
9+
pass
10+
print False
11+
internet_on()

‎scripts/cpugraph.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# cpu graph (needs gnu/linux, grep, top, tail)
3+
# 2009 mennonite
4+
# public domain
5+
import os
6+
import time
7+
while True:
8+
f = os.system("top -n 2 | grep -i cpu\(s\) | tail -1 > /tmp/cpugr")
9+
f = open("/tmp/cpugr")
10+
ff = f.readlines()
11+
f.close()
12+
f = os.system("rm /tmp/cpugr")
13+
f = "".join(ff)
14+
ff = f.split("\x1b[") # clean up some of the ansi codes
15+
f = "".join(ff)
16+
f = f[:f.find("us")] # left of "us"
17+
f = f[:f.find(".")] # left of "."
18+
f = f[f.rfind(" "):] # right of bsearch " "
19+
f = int(float(int(f)) * 4 / 5) # 80 / 100... 100% = 80 columns
20+
ff = f
21+
if f < 0: f = 0
22+
if f > 79: f = 79
23+
print "]" * f
24+
time.sleep(1)

‎scripts/cpuuse.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
3+
import time
4+
5+
TIMEFORMAT = "%m/%d/%y %H:%M:%S"
6+
INTERVAL = 2
7+
8+
def getTimeList():
9+
statFile = file("/proc/stat", "r")
10+
timeList = statFile.readline().split(" ")[2:6]
11+
statFile.close()
12+
for i in range(len(timeList)) :
13+
timeList[i] = int(timeList[i])
14+
return timeList
15+
16+
def deltaTime(interval) :
17+
x = getTimeList()
18+
time.sleep(interval)
19+
y = getTimeList()
20+
for i in range(len(x)) :
21+
y[i] -= x[i]
22+
return y
23+
24+
if __name__ == "__main__" :
25+
while True :
26+
dt = deltaTime(INTERVAL)
27+
timeStamp = time.strftime(TIMEFORMAT)
28+
cpuPct = 100 - (dt[len(dt) - 1] * 100.00 / sum(dt))
29+
print timeStamp + "\t" + str('%.4f' %cpuPct)

‎scripts/cpuuse2.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/python
2+
3+
import psutil as ps
4+
5+
class cpu_percent:
6+
7+
def __init__(self):
8+
self.last = ps.cpu_times()
9+
10+
def update(self):
11+
12+
last = self.last
13+
current = ps.cpu_times()
14+
15+
total_time_passed = sum([current.__dict__.get(key, 0) - last.__dict__.get(key, 0) for key in current.attrs])
16+
17+
sys_time = current.system - last.system
18+
usr_time = current.user - last.user
19+
20+
self.last = current
21+
22+
if total_time_passed > 0:
23+
sys_percent = 100 * sys_time / total_time_passed
24+
usr_percent = 100 * usr_time / total_time_passed
25+
return sys_percent + usr_percent
26+
else:
27+
return 0

‎scripts/dns.py‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/python
2+
3+
import socket
4+
import struct
5+
import StringIO
6+
7+
# OCTET 1,2 ID
8+
# OCTET 3,4 QR(1 bit) + OPCODE(4 bit)+ AA(1 bit) + TC(1 bit) + RD(1 bit)+ RA(1 bit) +
9+
# Z(3 bit) + RCODE(4 bit)
10+
# OCTET 5,6 QDCOUNT
11+
# OCTET 7,8 ANCOUNT
12+
# OCTET 9,10 NSCOUNT
13+
# OCTET 11,12 ARCOUNT
14+
15+
class DnsHeader:
16+
def __init__(self):
17+
self.id = 0x1234
18+
self.bits = 0x0100 # recursion desired
19+
self.qdCount = 0
20+
self.anCount = 0
21+
self.nsCount = 0
22+
self.arCount = 0
23+
def toBinary(self):
24+
return struct.pack('!HHHHHH',
25+
self.id,
26+
self.bits,
27+
self.qdCount,
28+
self.anCount,
29+
self.nsCount,
30+
self.arCount);
31+
def fromBinary(self, bin):
32+
if bin.read:
33+
bin = bin.read(12)
34+
(self.id,
35+
self.bits,
36+
self.qdCount,
37+
self.anCount,
38+
self.nsCount,
39+
self.arCount) = struct.unpack('!HHHHHH', bin)
40+
return self
41+
def __repr__(self):
42+
return '<DnsHeader %d, %d questions, %d answers>' % (self.id, self.qdCount, self.anCount)
43+
44+
class DnsResourceRecord:
45+
pass
46+
47+
class DnsAnswer(DnsResourceRecord):
48+
pass
49+
50+
class DnsQuestion:
51+
def __init__(self):
52+
self.labels = []
53+
self.qtype = 1 # A-record
54+
self.qclass = 1 # the Internet
55+
def toBinary(self):
56+
bin = '';
57+
for label in self.labels:
58+
assert len(label) <= 63
59+
bin += struct.pack('B', len(label))
60+
bin += label
61+
bin += '\0' # Labels terminator
62+
bin += struct.pack('!HH', self.qtype, self.qclass)
63+
return bin
64+
65+
class DnsPacket:
66+
def __init__(self, header = None):
67+
self.header = header
68+
self.questions = []
69+
self.answers = []
70+
def addQuestion(self, question):
71+
self.header.qdCount += 1
72+
self.questions.append(question)
73+
def toBinary(self):
74+
bin = self.header.toBinary()
75+
for question in self.questions:
76+
bin += question.toBinary()
77+
return bin
78+
def __repr__(self):
79+
return '<DnsPacket %s>' % (self.header)
80+
81+
class BinReader(StringIO.StringIO):
82+
def unpack(self, fmt):
83+
size = struct.calcsize(fmt)
84+
bin = self.read(size)
85+
print bin.encode('hex')
86+
return struct.unpack(fmt, bin)
87+
88+
class DnsPacketConverter:
89+
def fromBinary(self, bin):
90+
reader = BinReader(bin)
91+
header = DnsHeader().fromBinary(reader)
92+
packet = DnsPacket(header)
93+
for qi in range(header.qdCount):
94+
q = self.readQuestion(reader)
95+
packet.questions.append(q)
96+
for ai in range(header.anCount):
97+
a = self.readAnswer(reader)
98+
packet.answers.append(a)
99+
return packet
100+
def readQuestion(self, reader):
101+
question = DnsQuestion()
102+
question.labels = self.readLabels(reader)
103+
(question.qtype, question.qclass) = reader.unpack('!HH')
104+
return question
105+
def readAnswer(self, reader):
106+
print "reading answer"
107+
answer = DnsAnswer()
108+
answer.name = self.readLabels(reader)
109+
(type, rrclass, ttl, rdlength) = reader.unpack('!HHiH')
110+
answer.rdata = reader.read(rdlength)
111+
print answer.rdata
112+
def readLabels(self, reader):
113+
labels = []
114+
while True:
115+
(length,) = reader.unpack('B')
116+
if length == 0: break
117+
# TODO suppport compression
118+
label = reader.read(length)
119+
labels.append(label)
120+
return labels
121+
122+
123+
124+
if __name__ == '__main__':
125+
header = DnsHeader()
126+
bin = header.toBinary()
127+
print 'header', bin.encode('hex')
128+
129+
question = DnsQuestion()
130+
question.labels = ['google', 'com']
131+
print 'question', question.toBinary().encode('hex')
132+
133+
packet = DnsPacket(header)
134+
packet.addQuestion(question)
135+
print 'packet', packet.toBinary().encode('hex')
136+
137+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
138+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
139+
# dest = ('127.0.0.1', 5353)
140+
dest = ('8.8.4.4', 53)
141+
sock.sendto(packet.toBinary(), dest)
142+
(response, address) = sock.recvfrom(1024)
143+
print 'respon', response.encode('hex')
144+
145+
conv = DnsPacketConverter()
146+
packet = conv.fromBinary(response)
147+
print packet
148+

0 commit comments

Comments
 (0)