Five Tips for Easter
22 March 2008
Happy Easter everyone, I don't have enough chocolate eggs for you all, so instead, here are five tips.
1. Give your screens names
I often have processes running in a GNU screen for long periods. For example, I almost always have an IRC client running, perhaps a screen for Gentoo Linux's emerge command, and so on.
You can resume a screen using the process number, for example:
screen -r 18203
However, it is more fun to give the screens human readable names. To do this, use the -S argument when you create the screen:
screen -S chat
Then you can resume the screen using:
screen -r chat
2. Go on the rampage with xargs rm
Sometimes I have a lot of files that I need to sort in a hurry, without going into specifics, usually the issue for me is that after I have gotten out the information that I really need; I then want to have a quick look at the rest before throwing it away.
So at this point, we need to separate almost useless data from completely useless data. In this I am not too concerned about the individual files themselves, but I just want to separate the noise out and look at the rest, normally before chucking those away too.
If the aim is throwing away as much unwanted data as possible, I will make a backup of the files first (in case I type the command wrong), then use "xargs rm" to decimate the working copy. If you feed a list of filenames to "xargs rm" then it will delete them.
So for example, lets throw away every .txt file in the working directory and below that is newer than two days old:
find * -iname "*.txt" -mtime -2 -print | xargs rm
To take another example, the comments in this blog are stored as XML files. If I get spams on this blog, then they usually are predictable, one recent spammer always used the same string in the author field. This command will delete all his comments:
grep -l *.xml "<author>Spammer" | xargs rm
3. Generate temporary filenames uniquely
Temporary files that have predictable filenames could (in theory) be used in race condition attacks or denial of service attacks, it is better to let the operating system assign a unique filename to use as a temporary file.
The way to do this in scripts is to use the mktemp command, this generates a unique name which you can then use in scripts. mktemp started in OpenBSD and appeared for a while in Linux distributions as an individual package. It is now included in coreutils by default.
Run the following command as many times as you like:
mktemp /tmp/example.XXXXXXXXXX
Each time it will substitute the Xs with random letters followed by the process number. This gives a unique filename.
The number of Xs is up to you. The manual page tells us that "ten 'Xs' will result in mktemp testing roughly 26 ** 10 combinations", that is 141.2 trillion plus change.
For example, our imaginary script records a program output to a file.
Lets start by making an environment variable called TMPFILE. The marks below are not single quotes, they are back-ticks (left of the 1 key on a British keyboard). This assigns the output of mktemp to the variable TMPFILE.
TMPFILE=`mktemp /tmp/datewatch.XXXXXXXXXX`
Now we can pipe output to $TMPFILE:
date >> $TMPFILE
Now lets print the output to the screen:
cat $TMPFILE
4. Include an RSS feed as a static component
Django has not taken over the world yet, and sometimes sites do not need to be that complicated. Some websites just use server sides includes to put together static content, for example it might have a header and a footer and the central content.
If you want to display items from another site's RSS feed, then you could just create another include for the feed; and then have that include recreated using cron.
Download this python script that I created. Do not forget to change the URL variable to contain the URL of the feed you want to include.
5. View your wireless history
Sometimes you might want to look at all the wireless networks that you have connected to in the past. If your laptop's Linux distribution uses NetworkManager, for example Ubuntu, then you can use my wireless network history script.
Download this file and rename it so the file ends with .py. Now run the script.
You can do this like so:
python netwomb.py
Or you can make the script executable:
chmod +x netwomb.py
Then just use
./netwomb.py
You can also import it as a module and do far more with it. For example, start the python interpreter:
python
and type the following commands:
import netwomb
wifihistory = netwomb.Network()
You now have an object called wifihistory that contains all the information about your past wireless connections.
To see the networks:
print wifihistory
Now take one of the results, for example, say one is called BTOpenzone. We can see when we set up the connection:
print wifihistory.BTOpenzone.time()
The other information available will depend on the type of connection. Imagine this connection used a WPA Certificate, we could see what certificate we used by going:
print wifihistory.BTOpenzone.wpa_eap_ca_cert_file
Each network is a regular python dictionary. You can see all the information about that connection by going:
print wifihistory.BTOpenzone.__dict__
Please let me know if you do anything cool with it.
That's all folks. Happy Easter!




1 e says...
What's the use of naming your screen sessions? Why not put all the windows into one screen and name them in there (with ^a A)? Then if you only have one screen session, you don't need to supply an argument to screen -r. And having everything in the same one lets you use the paste buffer between them.
Posted at 5:20 p.m. on March 22, 2008
2 John Reese says...
Just as an FYI, Ubuntu Hardy includes a new tool called Network Manager Editor, which allows you to use a nice GUI to browse your past network,s change keys, delete entries, etc. It's actually quite nice and easy to use. Just my $0.02 (worthless these days...)
BTW, your text-box coloring isn't friendly to those of us with dark themes on FF3, and your human test isn't friendly to us Americans who are too lazy to know much about foreign politics and have to look it up on Wikipedia. ;)
Posted at 5:43 p.m. on March 22, 2008
3 Zeth says...
Oh dear, oh dear. If you don't know the difference between Mr Bean, Mr Brown and Mr Stalin; then I am afraid nothing I do is going to help much ;)
Posted at 6:04 p.m. on March 22, 2008
4 John Reese says...
Well, I obviously know who Stalin is, but my knowledge of UK politics is not good enough to where I can blatantly assume the last name of 'Bean' is only a joke... ;)
Posted at 6:40 p.m. on March 22, 2008
5 Dirk R. Gently says...
I've been using dtach lately instead of screen. Screen has a few key bindings that conflict with a couple of my programs. Using dtach is easy, create a dtach temp file and add the program:
dtach -n /tmp/irssi.dtach irssi
To attach to the program specify the dtach temp file:
dtach -a /tmp/irssi.dtach
and ctrl will release it.
Just thought I'd mention it ;)
Posted at 1:32 p.m. on March 24, 2008
6 Lingerance says...
find * -iname ".txt" -mtime -2 -print | xargs rm Is the same as find * -iname ".txt" -mtime -2 -delete There is usually never a real need to pipe find output to something else as find offers -exec as well.
Posted at 4:46 p.m. on March 24, 2008
7 Tom says...
find -exec is nice, but escaping can become complicated if you want to execute, say, awk using a weird pattern. My preferred way is not elegant at all but very robust and flexible:
find * -iname ".txt" | while read i; do echo rm "$i"; done
I run it once like that, and if I am sure it does what I meant I remove the echo. This also takes care of annoyances like spaces in filenames.
Another way of dealing with all sorts of strange characters is to tell find and xargs to use null bytes as characters for terminating:
find * -iname ".txt" -print0 | xargs -0 rm
Posted at 12:59 a.m. on June 17, 2008
8 John says...
Zeth,
The link to this file (for view wireless history) doesn't bring up a dialogue. Could you fix this?
Posted at 8:04 p.m. on August 20, 2008