Network Tools OS Interoperability Unix Admin

Most often used Network Sniffer flags for tcpdump and snoop

Most often used flags for tcpdump and snoop are below. Snoop is for SunOS while tcpdump is available for mostly all Unix/Linux kinds. We will discuss filters in another post. And to read back and process the captured files, I use wireshark which provides lots of options to deep dive into the packet stream.

Purpose snoop tcpdump Description
Select Interface -d <iface> -i <iface> Not needed if system has only one interface (ignoring localhost)
Capture full -s 0 -s 0 Snap length controls how much to capture. Zero means capture all.
Write capture to a file -o <filename> -w <filename>
Avoid DNS lookups for IPs in capture -r -n So no DNS lookups are performed when displaying real-time capture, good for efficiency
Apache Open Source Tools php Windows

Configure Apache to run PHP on Windows

Just rebuilt my Windows laptop and had to reload Apache, MySQL and PHP. Some may suggest WAMP or XAMPP projects to simplify things, but those are my last resorts. I like to have full control of the situation. So I downloaded the latest versions of each from respective site and installed them. And then the problem happened.

PHP installed successfully but had an error that it could not access httpd.conf so PHP files could not be executed. To get the short answer, I turned to Google. It took me about 10 minutes and sorting of 20 webpages to get to the right configuration. Only if I had gone directly to Apache it would be better. But here is what configuration in Apache has to be done to get PHP files recognized and run by Apache.

Modify httpd.conf file to have following.

# Tell Apache where to pick PHP Module from
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"

# Tell Apache that it should treat index.php just like index.html
DirectoryIndex index.html index.php

# Add type for PHP file handler
AddType application/x-httpd-php .php
# This is optional but allows user to access php source file (php files with extension  renamed) with full syntax highlighting
AddType application/x-httpd-php-source .phps
Hardware OS Interoperability Unix Admin

How to find number of CPUs on unix system

Newer processors are multi-core and could have hyper-threading enabled. So there are time when user may need to know how many cores (virtual) processors are available and how many Physical (real) processors are installed. CPU information can be retrieved via /usr/sbin/psrinfo on SunOS while it is available in /proc/cpuinfo on Linux. /proc/cpuinfo on Linux contains information about each available core. Following command combinations can be used to retrieve the number of CPUs.

OS Physical Processors Cores available
SunOS /usr/sbin/psrinfo -p /usr/sbin/psrinfo | wc -l
Linux grep “^physical id” /proc/cpuinfo | awk ‘{print $NF}’ | sort -u | wc -l grep processor /proc/cpuinfo | wc -l
IRIX hinv | grep -i processor | head -n1 | cut -dā€™ ā€˜ -f1

Update: Thanks Matias for tip on IRIX.

Bash Open Source Tools Unix Admin

screen : Run processes on re-attachable virtual terminals

Many a times I am doing production support from my Blackberry or have logged in on VPN which could get disconnected for one reason or another. Then there are times when I am working on some fixes and it is time to go home but I want to continue working from home later on the same task. The problem that all of these have in common is that all of your old sessions were running on the local system console which you no longer can access remotely. Some would say use a remote desktop client (like VNC, RDP etc) which would work but not only they are CPU intensive but worst of all Bandwidth consuming. You cannot do that on Blackberry or from a tethered connection on the road. So you need a text based solution with a very small bandwidth signature so it can be responsive enough for a tethered connection. Unix has a utility called screen which has been there for at least last 14 years (since I have been working on Unix systems. This was my first utility before vi that I started to love to use). Think of this as terminal multiplexer.

Bash Unix Admin

bash : Signal Handling

A script could have lock file or is producing temporary files which need to be renamed before exiting. What if the script receives a signal and quits in the middle leaving temporary files scattered around? Bash provides “trap” function to trap a signal and right a signal handler.

if [ ! -e $lockfile ]; then
        # Arm the handler
        trap "rm -f $lockfile; exit" INT TERM EXIT
        touch $lockfile
        rm $lockfile
        # Disarm the handler
        trap - INT TERM EXIT

Or you can write a function like below.

        rm -f $lockfile
if [ ! -e $lockfile ]; then
        # Arm the handler
        trap Handle_Exit_Safely INT TERM EXIT
        touch $lockfile
        rm $lockfile
        # Disarm the handler
        trap - INT TERM EXIT

perl : Extract filename from full path

To extract the name of the file the quick way is to use substr and rindex commands to strip the path from filename as shown below.


my $path='/path/to/search/filename';
my $fname= substr($path, rindex($path,"/")+1, length($path)-rindex($path,"/")-1);
print "$fnamen";

If you have the luxury of using Perl Packages, then use following solution which is very clean.


use File::Basename;
my $path='/path/to/search/filename';
my $fname = basename($path);
print "$fnamen";

perl : Read filenames from a Directory

Perl provides built in glob function which can be used to read the list of files in a directory.

@files = </path/to/search/pattern_to_search>;

Here is working code example that prints all filenames from current directory, one per line. The glob happens when the <> characters pulls the filenames into the @files array.

@files = <*>;
foreach $file (@files) {
        print "$filen";

So if you were to find all php files in your webserver directory use

@files = </var/www/htdocs/*.php>
Bash OS Interoperability Perl Unix Admin

find broken symlinks

If you have a big project with multiple shared libraries, there is a chance to run into soft links that are no longer pointing anywhere. Here are couple of ways to find those dangling soft links that no longer point to any real file using the Unix find command.

These are in preferred order.

find /path/to/search -type l | (while read FN ; do /usr/bin/test -e "$FN" || echo "$FN"; done)
find /path/to/search -type l ! -exec /usr/bin/test -r {} ; -print
find -L /path/to/search -type l

So here is how these can be used to clean out those dangling soft links (again in preferred order). We use rm to ensure that any aliases don’t kick in. First one is the best way which is portable to most Unix platforms.

find /path/to/search -type l | (while read FN ; do /usr/bin/test -e "$FN" || rm -f "$FN"; done)
find -L /path/to/search -type l -exec rm -f {} ;
find -L /path/to/search -type l -delete

Note: When using in script, make sure to escape the properly.

Interoperability Issues between SunOS and Linux

-delete is not supported on SunOS find. It is available on GNU findutils. So using -exec to invoke rm command would be portable. As well as the test for existence /usr/bin/test -e is portable. Make sure to use /usr/bin/test instead of shell built in test because on sh the flag -e is not available.

Update (2011-08-12) : /usr/bin/test should be used for interoperability.

Bash OS Interoperability Unix Admin

bash : Automate add/modify/delete of cron jobs from a script

If you have auto installing packages there could be times when a cron job needs to be added. So the script has to be able to create new cron entries or delete old ones. One solution is to create temporary files in between to hold the other unaffected cron entries that are currently installed, add the new entry and then install this file using crontab. Creating of temporary files should be avoided in between as there are risks. So here is an elegant solution which uses piping in the output of multiple commands.

To remove already existing cron job (rdate for user unixite in example below) use a syntax like

crontab -l -u unixite | grep -v rdate | crontab -u unixite -

This pipe chain lists the existing crontab entries, removes any containing the string rdate, then reloads the resulting data by piping it back to crontab of user unixite. “-” is for reading from the stream or terminal (see Note below). No useless temporary file, no security risk.

To add new crontab entries


BlackBerry : Play PAC-MAN

Here is a tip on 30th birthday of PAC-MAN. Go to Google today to play right on the website.

UPDATE: April 6, 2011

Download for BlackBerry from Benny Chow is no longer available. All the best things come to an end and Namco who owns the Pac-Man no longer wants it to be free. Come on Namco!!! Grow up. You already got all the money you want. Do you want us to pay every time we even say Pac-Man?

It was available for download on your BlackBerry. Thanks to Benny Chow. It was at which no longer exists. The latest info from Benny is at

Here is a screenshot from my Blackberry Curve 8310.
PAC-MAN on BlackBerry