import datetime
import time
import calendar
#Returns string
d = datetime.datetime.now()
d.strftime('%s')
#another solution returns int
calendar.timegm(d.timetuple())
#Yet another solution, returns float
time.mktime(d.timetuple())
pretty
Tuesday, October 30, 2012
Python datetime to unix timestamp
The most simple solution for converting datetime to unix timestamp
Monday, October 15, 2012
Sanitize tshark date
A script to format date from tshark in a customizable way.
usage
tshark -tad -r example.pcap -T fields -e frame.time_epoch -e ip.src -e ip.dst | ./epochtodate.py
2009-12-16 12:25:37 570704 10.0.2.15 224.0.0.251
2009-12-16 12:25:38 802853 10.0.2.15 194.179.1.100
2009-12-16 12:25:43 808373 10.0.2.15 62.14.2.1
2009-12-16 12:25:43 976156 62.14.2.1 10.0.2.15
2009-12-16 12:25:43 979653 10.0.2.15 194.179.1.100
2009-12-16 12:25:48 983549 10.0.2.15 62.14.2.1
2009-12-16 12:25:49 148470 62.14.2.1 10.0.2.15
2009-12-16 12:25:49 148789 10.0.2.15 194.179.1.100
2009-12-16 12:25:49 228531 194.179.1.100 10.0.2.15
#!/usr/bin/python
'''
Convert tshark frame.time_epoch to readable date
'''
import datetime
import fileinput
import re
def epochtodate(line):
re_epoch = re.compile("([0-9]{10}\.[0-9]{9})")
found = re_epoch.search(line)
if found:
nowstring = datetime.datetime.fromtimestamp(float(found.group(1))).strftime('%Y-%m-%d %H:%M:%S %f')
line = re.sub("[0-9]{10}\.[0-9]{9}",nowstring,line)
print line.rstrip("\n")
else:
print line
for line in fileinput.input():
epochtodate(line)
Thursday, October 4, 2012
Sort a dictionary in python
Sort by value
http://stackoverflow.com/questions/613183/python-sort-a-dictionary-by-valueIf you want to have descending just add reverse=True
import operator
x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=True)
Sort by key
http://stackoverflow.com/questions/9001509/python-dictionary-sort-by-key
import collections
d = {2:3, 1:89, 4:5, 3:0}
od = collections.OrderedDict(sorted(d.items()))
Monday, September 10, 2012
Scapy and HTTP
Found a HTTP dissector for scapy
A test that displays Requests and Responses
A test that displays Requests and Responses
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
try:
import scapy.all as scapy
except ImportError:
import scapy
import HTTP
packets = scapy.rdpcap('example.pcap')
for p in packets:
if p.haslayer("HTTPRequest") :
#print p['TCP'].getfieldval('dport')
print p.getlayer('HTTP Request')
if p.haslayer("HTTPResponse"):
print p.getlayer('HTTP Response')
print "done"
Thursday, May 10, 2012
Supress Mysqldb warnings in python
from warnings import filterwarnings
import MySQLdb as Database
filterwarnings('ignore', category = Database.Warning)
Re enable
Thanks! You saved my day
from warnings import resetwarnings
resetwarnings()
rsync. copy folder recursive
dirum@lupus:~$ rsync -azv /var/log /tmp/temp/
< supressed output >
dirum@lupus:~$ ls /tmp/temp
log
dirum@lupus:~$
Get that datetime string in python
import datetime
nowstring = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
print (nowstring)
Subscribe to:
Posts (Atom)