This is just a blog to try and spread some of the knowledge that has been freely given to me by the wider community, without which I'd get absolutely nothing accomplished. I hope this benefits some of you out there.

Friday, July 31, 2009

Behold: Awk



I've never really used awk before, but today I had reason to to help with some iptable rules I was using, and for my purposes it was quite easy and straight forward (even a bit of fun)

I had written some rules to do some port forwarding on the iptables nat table, but may have found a
problem with the routing as running 'up2date' no longer works.

I found some rules that should allow connections from rhn.redhat.com, where up2date needs to connect to in order to run properly, but they had the ip address hard coded. While this probably will be ok for a while, the ip address could easily change so I wanted something more dynamic.

All of the following is in a bash script that builds my iptable rules. I've only included the relevent parts below. This is what I came up with:

...

redhatip=`host rhn.redhat.com | head -n 1 | awk '{print $4}' | awk -F "\." ' $1 <= 255 && $2 <=255 && $3 <= 255 && $4 <= 255'`

if [ -z "$redhatip" ]
then
echo "invalid IP address for rhn.redhat.com! Please double check the script for correctness."
exit
fi

iptables -A OUTPUT -o eth0 -p tcp -d $redhatip -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp -s $redhatip -m state --state ESTABLISHED -j ACCEPT

I've included the rules in the iptables just for completeness incase anybody else needs to know how to get up2date to start working again.

Please note that I haven't had a chance to test the iptable rules yet, so you are on your own as far as that is concerned, but as far as getting the ip address from using host and then checking it for correctness, that all works. Enjoy!

Thursday, July 16, 2009

Programmers Take Note



I recently read a blog entry on Reddit that got me thinking. The gist of it was something like this: take notes of everything you want to know how to do because it will become your most valuable tool.

I truly believe that. They don't have to be fancy or in order, they just have to be there and searchable by using grep. A record of your experience will become invaluable and I have started making notes myself.

Here is a small bash script I wrote to help make making notes easier:

#!/bin/bash
today="note_`eval date +%Y_%m_%d`"
filename="/home/dir/notes/$today"
if [ -z "$1" ]
then
exec vi $filename
else
echo -e "\n\n" >> $filename
`cat $1 >> $filename`
exec vi $filename
fi

This will allow you to either just type "makenote" and start/add to today's note, or "makenote your_script.sh" to append the script to the bottom of your new/current note. From there you can add your comments about the script.

Put a symbolic link to this script, or simply copy it, into your path (probably /usr/bin will do)
and you can use the command makenote from anywhere and the note will go to the right place.

Here is a small example of some of the notes I've made, maybe it'll inspire some of you:

FIND FILE BY NAME

About

The `locate` command will find all files that have the name you
specify. Probably the results will be too much, so pipe it through
as many greps as you need to find what you're looking for.

Example

locate python | grep sqlite | grep transactions


MYSQL SHOW COLUMNS

Example

SHOW COLUMNS FROM mytable FROM mydb;
SHOW COLUMNS FROM mydb.mytable;


FIND ALL PORTS CURRENTLY LISTENING

Run

nestat -an|grep -i listen

Extra

Most port numbers are listed in /etc/services


The only problem with taking notes is that these days we all work on many different machines. Some are at home, others at multiple work stations. How do we keep them all in sync so we don't lose any? Good question. Maybe something with the google docs api to sync our notes across all computers, like a simple command to grab the latest in google docs and then write them back when you're done. If anyone has an idea to solve this problem please let me know!

Followers