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?

Mittwoch, 16. Februar 2011

Teamspeak 3 Wav to Mp3

This is rather a note to myself. If you try to convert a wav-file with Lame, which had been written by Teamspeak 3, you'll probably encounter the following error:

Unsupported data format: 0x0003

One solution is, to resample it with Sox:

sox ts3.wav -b 16 -r 44100 converted.wav

Now, you may convert it with lame (or oggenc):

lame -V9 converted.wav final.mp3

or directly by piping:

sox ts3.wav -b 16 -r 44100 -t wav - | lame \
-V9 - final.mp3


I've chosen -V9 since that voice quality was still good for me. The original file had 906 MB, the other wav (generated by Sox) had 417 MB. I finally got mp3's of 18 MB (with -V9) and 26 MB (with -V8).