I'm busy writing a reader for my network sniffer. It writes all sort of data into a MySQL database. I'm trying to print the data in a readable way into my console. I've build two function to print my data
def printHeader(destination, source, protocol):
print('Ethernet Frame:')
print(('| - Destination: {}, Source: {}, Protocol: {}')
.format(destination, source, protocol))
def printDNS(version, header_length, ttl, protocolEGP, source, target, source_port, destination_port, length):
print('| - IPv4 Packet:')
print((' | - Version: {}, Header Length: {}, TTL: {},'
).format(version, header_length, ttl))
print((' | - Protocol: {}, Source: {}, Target: {}'
).format(protocolEGP, source, target))
print(' | - UDP Segment:')
print((' | - Source Port: {}, Destination Port: {}, '
'Length: {}').format(source_port, destination_port, length))
def openCON(query):
conn = cymysql.connect(host='*', port=3306, user='root', passwd='*', db='python')
cur = conn.cursor()
cur.execute(query)
data = []
for row in cur:
data.extend(row)
cur.close()
conn.close()
return data
Now when I run a query I retrieve something like this:
[19, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 52698, 53, 28485, 18, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 52681, 53, 28485, 20, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 34310, 53, 28502]
I'm trying to iterate over the array to send the correct array indexes to the functions
Example : first array[1:3] should be sent to printHeader() the second part array[4:11] should be sent to printDNS() the first array is the packet number, not using that one in this example. then I needs to send array [13:15] to printHeader() etc.
I can't seem to figure how to code this in a nice fashion, if tried something like, but as we all know its horrible; also tried looping but with none success so far
DNS = openCON(query)
z = 1
x = 2
c = 3
a = 4
s = 5
d = 6
g = 7
h = 8
j = 9
k = 10
l = 11
p = 12
while True:
if p < len(DNS):
printHeader(DNS[(z)], DNS[(x)], DNS[(c)])
printDNS(DNS[(a)], DNS[(s)], DNS[(d)], DNS[(g)], DNS[(h)], DNS[(j)], DNS[(k)], DNS[(l)], DNS[(p)])
z += 12
x += 12
c += 12
a += 12
s += 12
d += 12
g += 12
h += 12
j += 12
k += 12
l += 12
else:
break
Can somebody help me push in the right direction to code this nice and efficient? I've thought maybe I should not query the entire table but only a row at the time.
Thanks in advance.