Wednesday, November 12, 2008

Stupid (but useful) one liner for Visually Monitoring Free Disk Space

I'm sure there is a better way of doing this (or a prebuilt tool that does) but I felt like doing some oldschool shell scripting while I update my local Ubuntu mirror for Intrepid Ibex.

One liner:
while true; do clear; date; df -ah; sleep 10; done

The output looks like:
Wed Nov 12 20:10:43 PST 2008
Filesystem Size Used Avail Use% Mounted on
/dev/hda1 72G 49G 20G 71% /
tmpfs 506M 0 506M 0% /lib/init/rw
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
procbususb 0 0 0 - /proc/bus/usb
udev 10M 44K 10M 1% /dev
tmpfs 506M 0 506M 0% /dev/shm
devpts 0 0 0 - /dev/pts
/dev/hdb1 459G 59G 377G 14% /var/spool/apt-mirror

Monday, August 25, 2008

Ping a range of IP addresses using Python (or whatever else you need to do)

#!/usr/bin/env python
#
# IPRange. Do stuff with IP addresses.
#
# this is code is designed to parse a range of ip addresses and
# do something with them. Example uses are ping, nmap, ssh, etc.
#
# have fun, but don't do illegal things and if it breaks everything
# around you it isn't my fault. Use this only for good, not evil.
#
# contact author(Sam Levine):
# host = darkscyon
# domain = geemail.com (you know, google mail)
# no spam or complaints please. patches are welcome. feedback
# (good or bad) is very welcome.
#
# Be excellent to one another.

def IPRange(octets, func=""):
# octets = "192.168.50-55.1-100"
# func = (some function you've written)
# objects passed to func are strings

if func == "":
def func():
pass

octets = (octets.split('.'))
ranges = []
loop = 0
for octet in octets:
# this is ugly
# basically the idea is that if the user enters a range of ip
# addreses delimited by '-' it parses out the first part of the
# range and the second part.
# it works, just not pretty or readable
if octet.find('-') != -1:
spot = octet.find('-') + 1
octets[loop] = int(octet[:octet.find('-')])
ranges.append(int(octet[spot:]) + 1)
else:
octets[loop] = int(octet)
ranges.append(int(octet) + 1)
loop += 1
CurrentAddress = ""
loop = 0
output = []
for one in range(octets[0], ranges[0]):
for two in range(octets[1], ranges[1]):
for three in range(octets[2], ranges[2]):
for four in range(octets[3], ranges[3]):
for item in (one, two, three, four):
CurrentAddress += str \
((one, two, three, four)[loop]) + "."
loop += 1
CurrentAddress = CurrentAddress[:-1]
output.append(func(CurrentAddress))
CurrentAddress = ""
loop = 0
# return a list of the output
return output

if __name__ == '__main__':
import os
# self test code
def func(x):
print x

test = IPRange("192.168.1-2.10-15", func)
print "The output should be none", test

# example usage
def pinger(x):
input, output = os.popen4('ping -c2 ' + x)
return output
pinglines = IPRange("192.168.1.1-254", pinger)
for streams in pinglines:
for line in streams.readlines():
print line,

Wednesday, August 13, 2008

A python script to find and play all the videos on your computer

#!/usr/bin/env python
#
# a silly script I wrote to find and play
# all the movies on my hd.
#
class FileLister:
def __init__(self, suffixes, directory=None):
self.suffixes = suffixes
if directory:
self.cwd = directory
else:
self.cwd = os.getcwd()

def list_files(self):
list = []
items = os.listdir(self.cwd)
for item in items:
for suffix in self.suffixes:
if item.endswith(suffix):
list.append(item)
return list

def change_dir(self, directory):
self.cwd = directory

def change_filter():
pass

def walk_dir(self):
movies = []
for root, dirs, files in os.walk(self.cwd):
self.change_dir(root)
list = self.list_files()
if list:
for item in list:
movies.append(os.path.join(root, item))

return movies

class MoviePlayer:
def __init__(self, movie_list, player='vlc'):
self.player =[player]
self.mlist = movie_list

def play(self):
command = self.player
for movie in self.mlist:
command.append(movie)

subprocess.call(command)

if __name__ == "__main__":
import os, subprocess, time
# replace the magic string below with the location you store your videos
dir = os.path.expanduser('/media/storage')
# you may wish to edit the list below to add filetypes you use
lister = FileLister(['.avi', '.mpg', '.flv', '.mkv'], dir)
movies = lister.walk_dir()
movies.sort()
# mplayer can be substituted with gmplayer or left out to use
# vlc by default
playme = MoviePlayer(movies, 'mplayer')
playme.play()

Wednesday, July 30, 2008

My current python "IDE"

The script below is one that I'm currently using to quickly setup my python coding environment.
#!/usr/bin/python
import os
os.popen4('gnome-terminal -e python --working-directory ~/scripts/python -t PYTHON')
os.popen4('gnome-terminal --working-directory ~/scripts/python')
os.popen4('xterm -e "cd scripts/python && /bin/bash"')
os.popen4('firefox http://docs.python.org/ http://www.pygtk.org/pygtk2tutorial/index.html')

You may be asking yourself why I'm opening 3 terminals. I personally like having lots of text in front of me at once so I can focus on reading/thinking and not using the mouse (or the keyboard).

Xterm is there because it is really really fast. Gnome-terminal is there due to it's superior copy/paste support (I grew up on Windows, sue me).

Saturday, July 12, 2008

determining your visible IP address using perl and whatipmyip.com

A while back Ashish Shukla put together
a great hack for using perl an whatismyip.com to determine your IP address. My modified version of this is below:
#!/usr/bin/perl -w
#whatismyip.pl - This script uses http://www.whatismyip.com/ service.

use LWP::Simple;
use strict;

my $url = "http://www.whatismyip.com/automation/n09230945.asp";

my $content = get($url);
print "$content\n";

Monday, June 30, 2008

fun with nmap

I had some fun with nmap today.

Should you care to see some of the horrible pipe-fitting I did please see the commands below:
nmap -A -vv 192.168.1.1-254 > nmapoutputwithosdetection.txt  
grep "open port" nmapoutputwithosdetection.txt | awk '/[0-9]+\.[0-9]+\.[0-9]\./ {print $6}' | \
sort -u > iplisttoscan.txt
nmap -T sneaky -iL iplisttoscan.txt
What does this do? nothing productive whatsoever really. I wanted a script to go through and determine the hosts up on my local network, then simulate someone being "sneaky" when using nmap so I could watch the scan using tcpdump (note to self: start using a better sniffer like wireshark).

Sunday, June 15, 2008

my .fluxbox/keys file

Although I love gnome and kde, they are are both way too heavy for my EEE PC 4G 700. Speed is extremely important on a device with limited cpu power, more so on a mobile device (hence why putting Vista on a UMPC is insane). My choice for a window manager on my EEE PC is Fluxbox. It is easily configurable (with a little reading) and is very responsive, even on slower hardware.

Below is the contents of my .fluxbox/keys file, hopefully someone will get something out of seeing it.

OnDesktop Mouse1 :HideMenus
OnDesktop Mouse2 :WorkspaceMenu
OnDesktop Mouse3 :RootMenu
OnDesktop Mouse4 :NextWorkspace
OnDesktop Mouse5 :PrevWorkspace
# 100 = left 102 = right 61 = / 98 = up 104 = down
Control Mod1 100 :PrevWorkspace
Control Mod1 102 :NextWorkspace
Control Mod1 61 :ToggleDecor
Control Mod1 98 :MaximizeWindow
Control Mod1 104 :Exec gnome-do
Mod1 F1 :Workspace 1
Mod1 F2 :Workspace 2
Mod1 F3 :Workspace 3
Mod1 F4 :Workspace 4
Mod1 F5 :Workspace 5
Mod1 F6 :Workspace 6
Mod1 F7 :Workspace 7
Mod1 F8 :Workspace 8
Mod1 F9 :Workspace 9
Mod1 F10 :Workspace 10
Mod1 F11 :Workspace 11
Mod1 F12 :Workspace 12
Control Mod1 f :Exec firefox
Control Mod1 t :Exec xterm
# turn on bluetooth and connect to my n75
Control Mod1 i :Exec gksu netz
# turn off bluetooth
Control Mod1 n :Exec gksu nonetz
Control Mod1 p :Exec pidgin
Control Mod1 c :Exec gcalctool
Control Mod1 k :Exec conky
Control Mod1 e :Exec FBReader
Control Mod1 0 :Exec gksu /etc/init.d/networking restart
Control Mod1 9 :Exec gksu ifconfig ath0 down
Menu :RootMenu