Friday, February 11, 2011

How to find length of a string in Linux

Suppose you have following variables in your shell
str="foobar"
and you want to output the length of this string (length of this string is 6) then run the following command
echo ${#str}

Thursday, February 3, 2011

Calculate IP's between specified range

If you want to calculate all IP's between the specified range then following perl script can be used.
--------------------------------------------------------------------------
#!/usr/bin/perl
$numArgs = $#ARGV + 1;
if ($numArgs ne "2"){
        print "USAGE: perl $0 <start ip address> <end ip address>\n";
        exit;
}

$sip=$ARGV[0];
$eip=$ARGV[1];

($sip1,$sip2,$sip3,$sip4)=split(/[.]/,$sip);
($eip1,$eip2,$eip3,$eip4)=split(/[.]/,$eip);

$next_ip=$sip;
$end_ip=$eip;
while ($next_ip ne $end_ip){

        $next_ip="$sip1.$sip2.$sip3.$sip4";
        print $next_ip."\n";

        $sip4++;
        if ( $sip4 > 255){
                $sip4=0;
                $sip3 =$sip3 + 1;
        }

        if ( $sip3 > 255){
                $sip3=0;
                $sip2 =$sip2 + 1;
        }

       if ( $sip2 > 255){
                $sip2=0;
                $sip1 =$sip1 + 1;
        }

}
-------------------------------------------------------------------------------
Usage
perl create_ip_ranges.pl 192.168.0.0  192.168.0.2
will return
192.168.0.0
192.168.0.1
192.168.0.2
 
 
Following site can also be used. 
To find all IP's within a range, use http://www.ipaddresslocation.org/ip-address-ranges.php

How to see non printable special and control characters in Unix

Sometimes while troubleshooting data issues I want to see non printable characters on the stdin.
Following is how to do that

1. Create a file with non printable characters
     echo -e "Quick brown fox\tjumped over lazy\0002dog"  > test

2. View the file
     cat test
You will see
Quick brown fox jumped over lazydog

3. Now to see non printable speical characters use command od as
      od -c test
Output will be
0000000   Q   u   i   c   k       b   r   o   w   n       f   o   x  \t
0000020   j   u   m   p   e   d       o   v   e   r       l   a   z   y
0000040 002   d   o   g  \n
0000045