Skip to content

Instantly share code, notes, and snippets.

@glennzw
glennzw / gist:5238660
Created March 25, 2013 16:56
Get device MAC address in Python
import netifaces
iface='wlan0'
mac=netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]['addr']
@glennzw
glennzw / gist:5238672
Last active December 15, 2015 09:29
Tell vim to use 4 spaces for <tab>
:set expandtab
:retab
:w
cat >> ~/.vimrc
set listchars=tab:>-,trail:_ list " Show tabs and trailing characters
@glennzw
glennzw / gist:5238689
Created March 25, 2013 17:01
Python dicts - adding if not exist
#BAD:
if tbl not in all_data:
all_data[tbl] = data
else:
all_data[tbl] += data
#GOOD:
all_data.setdefault(tbl, []).extend(data)
@glennzw
glennzw / git_tips.txt
Last active December 15, 2015 09:29
Git tips
#Setup
git config --global user.name "My Name"
git config --global user.email "user@domain.com"
#Initial commit
cd my_project
git init
git commit -m "My initial commit message"
git remote add origin git@example.com:my_project.git
git push -u origin master
@glennzw
glennzw / gist:5277544
Last active December 15, 2015 14:49
Scapy Hackery
#Created and send a client probe request
iface="mon0"
ssid="helloWorld"
mac="11:22:33:44:55"
p=RadioTap()/Dot11(addr1='ff:ff:ff:ff:ff:ff', addr3='ff:ff:ff:ff:ff:ff',addr2=mac)/Dot11ProbeReq() / Dot11Elt(ID='SSID', info=ssid)
sendp(p,iface=iface)
#Signal strength
@glennzw
glennzw / gist:5308728
Created April 4, 2013 08:26
Python - calling external programs
if you want the process's exitcode, subprocess.call() returns that. If you want to execute something and raise an exception if it fails, subprocess.check_call() does that. If you want something more complicated, including a conversation with the process you're starting or simply not waiting for the process to end, subprocess.Popen is what you want.
import subprocess
r=subprocess.check_call(["ls"])
r=subprocess.call(["ls"])
r = subprocess.Popen(["ls"])
@glennzw
glennzw / gist:5308730
Created April 4, 2013 08:26
Python webserver
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
@glennzw
glennzw / gist:5308888
Last active December 15, 2015 19:09
Hardened python
restarting the *program* on unknown exception is probably a better start
this would typically be implemented/configured by your init system
in upstart, it's just a matter of adding "respawn" to the config file
(there's options for saying how many times to try, with what sort of delay, etc)
systemd will have something as well
with an old-style shell-script based system, you'd probably want to look into the help "start-stop-daemon", which again has options for this sort of thing
Also:
http://supervisord.org/
@glennzw
glennzw / gist:5364873
Created April 11, 2013 16:25
Python monotonic clock
Sometimes you want a timer indepdent of the system clock, in case the system clock changes (e.g. ntpdate).
os.times() is the solution under Linux:
"Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children’s user time, children’s system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page times(2) or the corresponding Windows Platform API documentation. On Windows, only the first two items are filled, the others are zero."
Under Windows, you could use time.clock(), which will return seconds since the process started. But on Linux the same function returns CPU usage.
Window One:
>>> import time
@glennzw
glennzw / nmap_page_shot.sh
Created July 26, 2013 17:01
nmap web page
#!/bin/bash
# install http-screenshot script for 32bit BackTrack
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2
tar -jxvf wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2
cp wkhtmltoimage-i386 /usr/local/bin/
git clone git://github.com/SpiderLabs/Nmap-Tools.git
cd Nmap-Tools/NSE/
sed -i 's/wkhtmltoimage-i386 -n/timeout 5 wkhtmltoimage-i386 -n/' http-screenshot.nse