Archive for June, 2007

Linux iptables blocking IP address

Problem You do a netstat -an and see a raft of connections to your apache host, coming in at 10 – 20 a second.It shows ::ffff:10.0.0.10:80 ::ffff:212.248.1.1:1403 in netstat output. These are IPV6 connections AFAIK.Also you see ::1 – – [DD/MMM... in apache log. Restarting apache does nothing, the connections just reappear. Solution You need [...]

UNIX Lesson Six

Problem You want to write elegant code, which integrates well (pipes) with other UNIX commands. Solution Learn these UNIX Laws, from the inventor of UNIX pipes. Example No examples – see the reference tab. Reference 1. Make each program do one thing well. To do a new job, build afresh rather than complicate old programs [...]

UNIX Lesson Five

Problem Coding in UNIX is an art form, where certain philosophies prevail. Solution To really excel at UNIX coding and produce elegant, efficient, low maintenance code – you need to learn UNIX philosophy. Example No examples – just see the reference tab. Reference 1. Rule of Modularity: Write simple parts connected by clean interfaces.2. Rule [...]

UNIX Lesson One

Problem You are a complete beginner and keen to learn UNIX! Solution Okay in the examples, I am going to start very basically.From the outset, you need to understand some basic terms.Firstly – what is UNIX. Unix is an operating system. Other operating systems including Windows, Apple Mac, Novell, etc. It is what turns the [...]

File handling – UNIX stat

Problem You want to view seconds since last change or use Perl to process a number of files.Or maybe you just want a programmatic way to deal with file details, using something similar to the UNIX stat C routine. Solution Run stat against a file and display inode change time since epoch. See my other [...]

Check specific number arguments

Problem You want to check a specific number of arguments, have been supplied to your Perl script. Solution This snippet at the example tab, demonstrates how to check that 2 args, were postfixed to the Perl script.If you only want to check one, then use ARGV!=0, for three 2, etc. Example Arguments are supplied via [...]

Debugging Perl

Problem You have an error in your Perl script and want to debug it. Solution Debugging in Perl is very extensive, this demo is very basic! Check the reference for further instruction.You can manipulate PERLDB_OPTS variable, to force some debugging – then just pass through a -d option to your perl script.Also system debugging is [...]

Perl in file pattern substitution

Problem You want to substitute a pattern in a number of files, making backups and modifying in place on the fly.And you can do this in Perl – with a one liner! Solution An amazing bit of Perl and so quick!This one liner will replace all occurrences of a pattern, with your replace and even [...]

Date handling in Perl

Problem You want to convert epoch into readable date and time.Maybe you have a log file, which is pumping out lines prefixed with the epoch? Solution We use the perl function localtime to convert epoch into a date and time.At the example tab is the code to convert that epoch, into a recognizable date and [...]

Column handling in Perl

Problem During my first encounters with Perl many years ago, I asked how can I get a specific column.It was so easy in AWK! Solution Well here it is in Perl – see example tab.BTW you can always write an awkscript and run it through a2p – very good for learning Perl! Example Show column [...]