Posts mit dem Label bash werden angezeigt. Alle Posts anzeigen
Posts mit dem Label bash werden angezeigt. Alle Posts anzeigen

Mittwoch, 14. Dezember 2011

Bash: Converting IPs to Hostnames within a file or stream

I recently stumbled upon an interesting problem.

Imagine you have many IP addresses in a file or piped input. How to reverse DNS these entries, that is, how to substitute them with host names?

Example:

Blah;10.1.73.70:8800;10.1.73.69:8800;10.1.73.68:8800;10.1.73.67:8800;10.1.73.66:8800
Lalalala;10.1.73.70:8800;10.1.73.69:8800;10.1.73.68:8800;10.1.73.67:8800;10.1.73.66:8800;1
...

Well... took me a few hours, but once again, Perl magic to the rescue:

cat file | perl -pe 'sub rep{$host=`host $_[0]`;$host=~'s#\\n##g';$host=~'s#\(.+\ \|\.$\)##g';return $host} s#([^0-9]+)?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)#$1.rep($2)#eg';

So, for "LALALA---8.8.8.8---BLAAAAH---4.2.2.2---LALALA" you'd get "LALALA---google-public-dns-a.google.com---BLAAAAH---vnsc-bak.sys.gtei.net---LALALA".

Cool or cool?

Dienstag, 2. Juni 2009

rsdl v1.1

Here comes the next version of my cool Rapidshare download tool for the bash command line. It works out of the box on most Linux distributions and on Macintosh.

Download (md5) // Update January 2010: Mirror is down at the moment. Sorry.

Make sure to have read my previous post on rsdl. This is basically a bugfix release, the following things have been done:
  • Many bugfixes
  • Temporary files get stored in /tmp now
  • Output fits better into the terminal
  • Time gets counted down
  • rsdl won't overwrite files
  • More error checks for Rapidshare (file deleted/server busy and so on)
PS: There seems to be a similiar tool for premium users of Rapidshare which goes by the same name, rsdl.

Samstag, 14. März 2009

rsdl

I present you the first English entry in my blog and the first release of my 'rsdl' tool for Linux. It's basically a tiny shell script which does all the pesky work for you. There's still a few things to fix, but please try it out, you won't be disappointed.

Be careful though, as it won't (yet) check if it would overwrite a file or not - one could say that it is still beta software. The only "special" requirement is 'curl', so it should still run on most linux distributions. If you make a list of your RS links, you'll be able to do the following:

cat list | xargs rsdl

If you find or even fix some error, please let me know or send me patches. Further updates will always be available here.

Download (md5)

Have fun, and don't tell these guys from RS ;-)

PS: This program has been written only for the "Free Download" feature, and not (yet?) for the premium zone.

15. 3. : Meanwhile I've found out, that there exist other tools which do a similar job. However, I still prefer this one at the moment because of it's simplicity: No overblown interface, no config files and almost no dependencies.

Freitag, 23. Januar 2009

// TODO: Liste

Mir ist vorhin aufgefallen, dass ich wirklich viele TODO Kommentare in meinen Sourcen verwende.

Beispiel:
// TODO: Free Resources

Nervig wird es dann, wenn man mal ein paar erledigen möchte - denn die müsste ich dann erstmal überall zusammensuchen. Schön wäre eine Liste die mir gleich alle Vorkommnisse, inklusive Dateinamen, Zeilennummer und Inhalt anzeigt. Also hab ich mal ein kleines Skript zusammengehax0rt welches genau das tut für mich. So sieht die Ausgabe aus:

control.h, 161  : Remove this later
prog.h, 7 : Implement error handling. Exceptions and Assertions.
prog.h, 57 : This is a temporary solution.
prog.h, 61 : Make a ref counter for destroying this
test.cpp, 151 : Free Resources Here */
test.cpp, 182 : Make this dynamic
test.cpp, 188 : Free resources

Und das alles (praktisch) mit einer einzigen Zeile:
#/bin/bash
egrep -n 'TODO' src/*.{h,cpp} | \
perl -pe 's#(?:src/)+(.+):+([0-9]+):+.*TODO:?\s?(.+)$#"\1" \2 "\3" #g' | \
xargs printf '%20s, %-4d : %s\n'

Ist das nicht genial? Der blaue Part sollte angepasst werden. :-)

Freitag, 16. Januar 2009

WLAN Signal Power Beep Script

Wieder einmal wurde ich beeindruckt von der IT-Solution™-Power, die Linux dem Normalsterblichen völlig selbstverständlich zur Verfügung stellt. Mein Problem war im Prinzip, dass sich mein WLAN Adapter draußen befindet und ich beim Ausrichten nicht auf meinen Bildschirm schauen konnte wie gut die Signalstärke war. Also schrieb ich mir schnell ein Skript welches mir die 'Link Quality' in Audio Signale mittels PC-Speaker ausgibt - denn die konnte ich draußen noch sehr gut hören.

while true; do

QUALY=`/sbin/iwconfig wlan0 | /bin/grep "Link Quality" | /usr/bin/perl -pe 's/.*=(\d+)\/\d+.*/$1/g'`
FREQ=$(($QUALY * 100))

if [ $FREQ == 0 ]; then
FREQ=100;
fi

echo "Beeping at freq $FREQ"
sleep 1 && /usr/bin/beep -f $FREQ -r 2

done

Natürlich hätte man das auch mit Windows machen können, aber eben nicht so schnell & einfach. Das 'Link Quality' in Zeile 3 gilt anzupassen, sollte es in deutsch sein. Das Kernel Modul für den PC-Speaker sollte geladen sein sowie das Programm `beep` installiert.

Update: Zugegeben, beep ist nicht gerade sehr flexibel. Man kann die Lautstärke nicht einstellen, Ausgabe über Kopfhörer nicht möglich usw. Außerdem musste ich mittlerweile feststellen dass die 'Link Quality' nicht sehr ausschlaggebend für die Gesamt-Qualität ist. Selbst bei über 50% kann die Verbindung immer noch schnell zusammenbrechen wenn der Signal Level einfach zu schlecht ist. Also habe ich mein Skript ein wenig modifiziert, Voraussetzung diesmal ist übrigens sox und eine geeignete Sound-Datei. Ich gehe hier von einem Mindest Signal Level von -90 aus und mein Interface ist wlan0:

while true; do

QUALY=`sudo iwconfig wlan0 | grep "Signal level" | perl -pe 's#.*Signal level:(-\d+) dBm.*#$1#'`
PITCH=`echo "scale=2; $(((92-($QUALY*-1))))/0.01" | bc`
echo -ne "\r$PITCH"
sleep 0.5 && play -q "beep.wav" pitch $PITCH

done