Perl: detect if a module is installed before using it  

I wanted to check if a Perl module was installed at runtime, and error out accordingly if it wasn't. This allows me to print intelligent error messages if a module is not installed.

eval { require Weird::Module; };
if ($@) { die("Module is not installed\n"); }
Leave A Reply

Tracking mail abuse with PHP  

We have a web server with lots of virtual hosts on it. One day the mail queue exploded to over 100k messages. Obviously someone had used our server to send lots of spam. Tracking down what script was responsible was not an easy task. Lucking, starting in PHP 5.3 you can put the following in your php.ini to track some of that information.

mail.add_x_header = On
mail.log = /var/log/php-mail.log

The first makes php add an X-PHP-Originating-Script header to each email that contains the UID of the script owner and the filename of the script. This alone is probably sufficient to track down any abuse, assuming you can catch one of the outgoing emails to check. The second creates a log file that tracks every time someone send email with the path to the script, the line the mail() was called from, and the To: field.

With this information it was easy to track down the offending script.

Leave A Reply

Using Bash text replacement for mass rename  

Bash has some very powerful text replacement options called parameter expansion. This allowed me to easy mass rename a bunch of files that were .avi.mp4 to .mp4

for i in *.avi.mp4 ; do mv "$i" "${i%.avi.mp4}.mp4" ; done;

With these tools you have the ability to manipulate files very easily.

Leave A Reply

Perl/PHP regexp for spliting on non-escaped characters  

I have a string like the following:

desc=Did you know 2 + 2 \= 4?

I want to split each chunk of that string into segments separated by the equal signs. I can't just split on the equal signs because the text has an equal sign in it. I need to split on the non-escaped equal signs.

@parts = split(/(?<!\\)=/,$str);

This is called positive and negative look behind.

In PHP the only difference is that you have to double escape your \

$parts = preg_split('/(?<!\\\\)=/',$str);
Leave A Reply

Monitor a Linux box using IPMI  

To monitor the hardware of a server (fans/power/chassis) you'll need to install and setup IPMI.

yum install ipmitool
service start ipmi

You should have a /dev/ipmi0 now that acts as the interface to the IPMI board. You can list all the sensors you're able to monitor:

ipmitool sdr

To setup an IP address use the following commands:

ipmitool lan print 1
ipmitool lan set 1 ipaddr 10.3.1.190
ipmitool lan set 1 netmask 255.255.255.0
ipmitool lan set 1 defgw ipaddr 10.3.1.1

You may have to reset the IPMI board for IP address changes to take effect.

ipmitool mc reset cold
Leave A Reply

FFMpeg: Rotate a video file 90 degrees  

I took a video with my camera tilted 90 degrees and wanted to correct that with ffmpeg. You use the video filter (-vf) transpose. A value of 1 does 90 degrees clockwise, and a value of 2 does counter-clockwise.

ffmpeg -y -i PICT0005.AVI -r 30 -ar 16000 -vb 1000k -vf transpose=1 /tmp/out.mp4
Leave A Reply

Using tcpdump/wireshark to capture low TTL values  

We suspected a network anonaly where packets were expiring because of a low TTL value. You can capture packets with a given TTL value with the following tcpdump filter:

tcpdump -v ip and 'ip[8]<32'

This will filter out any packet with a TTL lower than 32, because the TTL byte is the 8th byte in the IP header.

Leave A Reply

Upsample audio with FFMPEG  

I got a little RC camera that generates mjpeg/pcm video files. The audio is 8000Hz which causes all kinds of grief if you try and convert it to a usable format like mpeg4 or webm. You have to upsample the audio to a more standard rate. You can do this with the -ar option in ffmpeg. You may also need to use -r to tell ffmpeg what the framerate of the output should.

ffmpeg -y -i input.avi -r 30 -vb 2000k -ar 16000 /tmp/output.webm
Leave A Reply

Remove duplicate RPMs  

I had a yum transaction go haywire and ended up with a bunch of duplicate RPMs (two different versions) installed. There is a simple solution to fix it:

package-cleanup --cleandupes
Leave A Reply

Slow DNS Fedora fix  

My Fedora box was responding slowly after DNS requests and I finally found that the fix is to disable parallel DNS requests. It appears that some firewalls have problems with parallel DNS requests (IPv6 and IPv4).

Leave A Reply

IPTables rule generator  

I just wrote a quick tool to generate iptables rules so I won't have to read the man page every time.

Leave A Reply - 1 Reply

How floating point numbers are stored  

A float in C++ is either 4 or 8 bits. A 4 bit float can hold a number up to 38 digits! I wasn't sure how a computer stores a number that large in such a small amount of ram so I did some research. I found that it's stored using a method called IEEE 754-1985 which Wikipedia has a very explanatory article on.

Leave A Reply

Iterate over arguments in bash  

Here is how you iterate over the list of command line arguments (ARGV) in bash

for var in "$@"
do
    echo "$var"
done
Leave A Reply

Books of 2012  

Also see the list of 2011. The date indicated denotes the date I started reading the book.

2012-01-03 - Attack of the Deranged Mutant Killer Monster Snow Goons - 127 pages
2012-01-04 - Calvin and Hobbes - 95 pages
2012-01-06 - Batman: Terror - 128 pages
2011-01-09 - Just After Sunset - 539 pages
2011-01-19 - Coraline - 194 pages
2012-01-23 - Alphabetter juice - 283 pages
2012-01-30 - The Last Werewolf - 293 pages
2012-02-05 - Cycle of the Werewolf - 128 pages
2012-02-07 - Braveheart - 288 pages
2012-02-12 - Stardust - 336 pages
2012-02-19 - Origins - 336 pages
2012-02-27 - The Bachman Books - 923 pages
2012-03-19 - Death by Black Hole - 320 pages
2012-03-28 - Ender's Game (Ender, Book 1) - 384 pages
2012-04-04 - A Spell for Chameleon (Xanth, Book 1) - 352 pages
2012-04-11 - The Fountainhead - 704 pages
2012-05-02 - Cat's cradle - 287 pages
2012-05-15 - Aliens Omnibus Volume 4 - 384 pages
Leave A Reply

Control Linux sound volume via the command line  

I want to set the volume at the command line to 40% before I run a game. I found that amixer does a pretty good job of this:

amixer --card 0 sset 'Master' 26

The volume number is on a scale of 0 to 64 (which seems weird).

Leave A Reply