Showing posts with label PERL. Show all posts
Showing posts with label PERL. Show all posts

Monday, March 10, 2014

PERL odds and sods

Dumping some of the PERL scripts that I had written from my home directory into this blog entry

Array:
 array_count.pl
#!/usr/bin/perl -w

#
# This script simply gives the length of the array
#

@numbers = qw(one two three four);

$array_length  = @numbers;

print "Array length = $array_length\n";


array_reversal.pl
#!/usr/bin/perl -w

#
# This script give an example of how an array is reversed in perl
#
@teams = qw(chelsea liverpool mancity arsenal);

foreach my $team (reverse @teams)
{
    print ("$team \n");
}


Capture command line output:
mysqld_port.pl

#!/usr/bin/perl -w

#
# This script redirects the mysql infor from netstat into a file
#
open (MYHANDLE, "netstat -tulpn|");

open (OUTHANDLE, ">> mysql.out");

while(<MYHANDLE>) {
    if (/mysqld/) {
        print OUTHANDLE $_;
    }
}

close (OUTHANDLE);
close (MYHANDLE);


num_files.pl

#!/usr/bin/perl -w

#
# This scripts counts the number of files in the current directory
#

sub file_count {
    open (MYHANDLER,"ls -1 |");
    $count = 0;

    while (<MYHANDLER>) {
        $file = $_;
        chomp($file);
        if (-f $file) {
            $count = $count + 1;
        }
    }

    print "There are $count files in the directory\n";
}

file_count();


open_netstat.pl
#!/usr/bin/perl -w

#
# This script redirects the output of netstat into a file
#
open (MYHANDLE, "netstat -tupl|");

open (MYOUT, ">>mynetstat.txt");

while(<MYHANDLE>)
{
    print MYOUT $_;
}


File IO:
check_logfiles.pl
#!/usr/bin/perl -w

#
# This script accepts log files as input and checks for the string 'connection
# accepted from'
#
while ($data = <>) {
        if ($data =~ /\bconnection accepted from\b/) {
                print "$data";
        }
}


get_nameserver.pl

#!/usr/bin/perl -w

#
# This function get the name server ip addresses from the resolv.conf file
#

sub get_nameserver {
    open(MYHANDLER, "/etc/resolv.conf") || die ("ERROR: unable to open file\n");

    while(<MYHANDLER>) {
        if(/nameserver\b/)
        {
            @ip_addr = split(/\s+/,$_);
            open(MYOUT,">>outfile");
            print OUT "$ip_addr[1]\n";
    }
   }
   close(MYHANDLER);
   close(MYOUT);
}

get_nameserver();


number_of_lines.pl

#!/usr/bin/perl -w

#
# This function asks the user to enter a file name
#
sub enter_filename {
    print "Enter a file name: ";
    $file_name = <STDIN>;
    chomp($file_name);
    return($file_name);
}

#
# This function counts the number of lines in the file
#
sub number_lines {
    $i = 0;
    $file_name = $_[0];
    if (-e $file_name) {
        open (MYHANDLER, $file_name) || die("ERROR: unable to open file";

        while (<MYHANDLER>)
        {
            $i=$i+1;
        }

        close(MYHANDLER);
        return($i);
    } else {
        print "File $file_name does not exist.\n";
    }
}

$user_file_name = enter_filename();
$number_of_lines = number_lines($user_file_name);

print "Number of lines in the file: $number_of_lines\n";


read_hosts.pl
#!/usr/bin/perl -w

$file_name = "/etc/hosts";

#
# This function returns host names and ip address from the /etc/hosts file
#

sub host_ipaddr {
    open(MYHANDLE,$file_name);

    while(<MYHANDLE>)
    {
        chomp;
        if (/^[0-9]/)
        {
            @fields = split(/\s+/,$_);
            print ("$fields[0] $fields[1]\n");
        }
    }
   close(MYHANDLE);
}

host_ipaddr($file_name);


read_passwd.pl
#!/usr/bin/perl -w

#
# This function reads the password file and outputs it onto the screen/stdout
#

sub read_passwd {
    open (FILEHANDLE, "/etc/passwd") || dir("ERROR: unable to open file\n");

    while ($name = <FILEHANDLE>)
    {
        chomp($name);
        print "$name \n";
    }

    close (FILEHANDLE);
}

read_passwd();


remove_blank.pl
#!/usr/bin/perl -w

#
# This script removed the blank lines from the file passed to it. The out
# gets displayed on the screen/stdout.
#

if (! defined $ARGV[0]) {
    usage();
} else {
    $my_file = "$ARGV[0]";
}

if (-e $my_file) {
    print ("File exists\n");
} else {
    print ("File does not exists.\n");
    exit 1;
}

open (MYHANDLER, $my_file) || dir("ERROR: unable to open file\n");

while(<MYHANDLER>) {
     if (/[^\s+]/) {
        print $_;
    }
}

close(MYHANDLER);

sub usage {
    print ("USAGE: .\/remove_blank.pl <file_name>\n");
    exit 1;
}




write_passwd.pl
 #!/usr/bin/perl -w

#
# This function reads the password file and outputs it onto the file outfile.txt
#

sub write_passwd {
    open (FILEHANDLE, "/etc/passwd") || dir("ERROR: unable to open file\n");
    open (OUTFILE, ">outfile.txt") || dir("ERROR: unable to open file\n");

    while ($name = <FILEHANDLE>)
    {
        print OUTFILE $name;
    }

    close (FILEHANDLE);
    close (OUTFILE);
}

write_passwd();

Loops:
print_num.pl
#!/usr/bin/perl -w

print "Enter a number: ";
$my_num = <STDIN>;
chomp($my_num);

print_number($my_num);

sub print_number {
    $prtnum = $_[0];
    print "$prtnum\n";

    for ($i=$prtnum-1; $i >=0 ; $i--) {

        print_number($i);

    }
}

Match:
grep_user.pl
#!/usr/bin/perl -w

while (<>)
{
 if (/oracle/)
 {
  print $_;
 }
}


number_range.pl

#!/usr/bin/perl -w

sub user_interface {
    print "Enter a number between 1 to 9: ";
    $my_number = <STDIN>;
    chomp ($my_number);
    return $my_number;
}

sub print_number {
    my $my_number = $_[0];
    if (($my_number >= 1) && ($my_number <= 9))
    {
        print "The number entered is: $my_number\n";
    } else {
        print "The number $my_number is out of range.\n";
    }
}

$num_entered = user_interface();
print_number($num_entered);


single_digit.pl

#!/usr/bin/perl -w

$_ = 32;

if (/[0123456789]/)
{
 print "Match: $_ \n";
 }

Split:
greatest_number.pl

#!/usr/bin/perl -w

sub user_interface {
    print "Enter some numbers: ";
    $numbers = <STDIN>;
    chomp($numbers);
    return $numbers;
}

sub greatest_number {
    @number_array = split(/\s+/,$_[0]);
    $x = $number_array[0];

    foreach $i (@number_array) {
        if ($i > $x) {
            $x = $i;
        }
    }

    print "The greatest number is $x\n";
}

$num_entered = user_interface();
greatest_number($num_entered);



split_ipaddr.pl

#!/usr/bin/perl -w

$ip_addr = "129.40.71.195";

@addr = split(/\./,$ip_addr);

foreach $num (@addr) {
    print "$num\n";
}


split.pl

#!/usr/bin/perl -w

#
# This script finds if user oracle exists in the /etc/passwd file
#
open (MYHANDLER,"/etc/passwd");

while (<MYHANDLER>)
{
    @fields = split(/:/,$_);
    $_ = $fields[0];
    if (/oracle/)
    {
        print "User oracle exists.\n";
        print "$_\n";
    }
}

String:
reverse.pl

#!/usr/bin/perl -w

$mystring = "hello";
$mylength = length($mystring);

@fields = split(//,$mystring);

for ($i=$mylength-1; $i>=0; $i--)
{
    print $fields[$i];
}
print "\n";


strcmp.pl

#!/usr/bin/perl -w

sub user_interface {
    print "Enter the first string: ";
    $strs = <STDIN>;
    chomp($strs);
    return ($strs);
}


sub string_compare {
    print "string_compare function $_[0]\n";
    @strings = split(/\s+/,$_[0]);
    print "The strings after splitting: $strings[0] $strings[1]\n";

    $len_one = length($strings[0]);
    $len_two = length($strings[1]);

    if($len_one != $len_two)
    {
        print "The length of the strings is different\n";
    } else {
        print "The length of the strings is same\n";

        @first_array = split(//,$strings[0]);
        @second_array = split(//,$strings[1]);

        for ($i=0; $i < $len_one; $i++) {
            if ($first_array[$i] ne $second_array[$i]) {
                print "The words are different\n";
                exit 1;
            }
        }
        print "The words are the same\n";
    }
}


$my_strings = user_interface();
print "The strings to be compared are $my_strings\n";
string_compare($my_strings);
exit;


strcpy.pl

#!/usr/bin/perl -w

$orig_string = $ARGV[0];
print ("Original: $orig_string \n");
$final_string = $orig_string;

print ("Result: $final_string \n");


[root@isvx3 string_manipulation]# cat strcpy.pl
#!/usr/bin/perl -w

$orig_string = $ARGV[0];
print ("Original: $orig_string \n");
$final_string = $orig_string;

print ("Result: $final_string \n");

stringlen.pl
#!/usr/bin/perl -w

$myword = "silly me";

$mylen = length($myword);

print "Length = $mylen";

string_reversal.pl

#!/usr/bin/perl -w

$myword = "silly me";

$mylen = length($myword);

print "Length = $mylen";


upper_lower.pl
#!/usr/bin/perl -w

print "Enter a world: ";
$myword = <STDIN>;
chomp($myword);

$myword =~ s/\W.*//;

print "The word entered is: $myword \n";


user_exists.pl
#!/usr/bin/perl -w

$file = $ARGV[0];
if (-e $file) {


open(MYHANDLER, $file);

while(<MYHANDLER>)
{
    chomp;
    if (/\bHadoop\b/)
    {
        print "$_ \n";
    }
}
close(MYHANDLER);
} else {
    print "File $file does not exist\n";
}


wordcountmap.pl
#!/usr/bin/perl -w

while (<STDIN>) {
    chomp;
    @words = split(/\s+/,$_);
    foreach $w (@words) {
        print "$w\n";
    }
}



Wednesday, February 26, 2014

Handy PERL regular expressions



Pattern

Description
/a./ The . matches any singe character except newline(\n)  eg.ab a
/[abcde]/

Matches a sting containing any one of the first five letters a lowercase alphabet eg.dog box
/[aeiouAEIOU]/ Matches any of the five vowels in either lower or uppercase eg.India egg
/[0123456789]/ Match if any single digit is present  eg. 0 3 41 100
/[0-9]/    Matches any single digit like the above                0 3
/[0-9\-]/ Match 0-9, or minus                        - 0 3
/[a-z0-9]/ Match any single lowercase letter or digit            a 0 9
/[a-zA-Z0-9_]/ Match any single letter, digit, or underscore            a X 9 _
/[^0-9]/ Match any single non-digit                    a
/[^aeiouAEIOU] Match any single non-vowel                    M N L
/[^\^]/ Match single character except an up-arrow            M N L
/[\da-fA-F]/ Match one hex digit
/fo+ba?r/ Matches an f followed by one or more o's followed by a b, followed by an optional a, followed by an r.
s/x+/boom/ Always replaces all consecutive x's with boom (resulting in fred boom barney), rather than just one or two x's, even though a shorter set of x's would also match the same regular expression. ie. $_ = "fred xxxxxxxxxx barney";
/x{5,10}/ Matches five to ten x's
/x{5,}/ Means five or more in this case
/x{5}/  Means "exactly this many" (five x's).
/x{0,5}/ Matches five or less x's
/a.{5}b/ Matches the letter a separated from the letter b by any five non-newline characters at any point in the string.
/a.*?c.*d/ matches the fewest characters between the a and c, not the most characters.
/oracle/ True if oracle matches. Note this will also match XoracleY
/\boracle\b/ True if only oracle matches
/ora\b/    True for anything that ends with ora, but fails for oracle. The \b is the word boundary.
/\bo/ True for oracle oozie operator, basically anything that begins with o
/\bora/ True only for anything that begins with ora
/^ora/ True only if it begins with ora, orc will fail
/^(o|h)/ True for only words begining with o or h
/^x|y/ Matches x at the beginning of line, or y anywhere
/abc*/ Matches ab, abc, abcc, abccc, abcccc, cab, zabc
/a|bc|d/ Matches a, bc, d, but not xyz, xbz
/(a|b)(c|d)/ Matches ac, ad, bc, or bd
/(song|blue)bird/ Matches songbird or bluebird
if (<STDIN> =~ /^y/i) True if the input begin with a y
s/foo/bar/g Replaces foo with bar in the string $_

Tuesday, February 11, 2014

Handy Linux and AIX system information




Linux

AIX
Number of Processors Number of  Physical CPU sockets
# cat /proc/cpuinfo  | grep "physical id" | sort | uniq | wc -l

Number of Cores
# cat /proc/cpuinfo | egrep "core id|physical id" | tr -d "\n" | sed s/physical/\\nphysical/g | grep -v ^$ | sort | uniq | wc -l

Total number of hyperthreads
# cat /proc/cpuinfo | grep processor | wc -l
prtconf | grep "Number of Processors"
Processor Type cat /proc/cpuinfo | grep "model name" | uniq

prtconf | grep "Processor Type"
Processor Clock Speed cat /proc/cpuinfo | grep "model name" | uniq prtconf | grep "Processor Clock Speed"
Disk information fdisk -l lspv
Network Card lspci | grep Ethernet

#lsdev -Cc if
# lsdev -Cc adapter
# entstat -d en0


Memory cat /proc/meminfo | grep MemTotal prtconf  | grep "Memory Size"
IP Address hostname -i prtconf | grep "IP Address"
HBA
lspci | grep HBA
lsdev -Cc adapter | grep "FC Adapter"

A simple PERL script that will give system and OS information on a Linux box

#!/usr/bin/perl -w

#
# A simple PERL script that gives system information like Linux kernel version
# CPU, Memory, IP address, and HBA
#

#
# Function that returns Linux kernel information
#

sub os {
    open (MYOS, "uname -a|");

    while (<MYOS>) {
        $my_os = $_;
        chomp($my_os);
        @kernel_version = split(/\s+/, $my_os);
        print "OS Version: $kernel_version[0] $kernel_version[2]\n";
    }
    close (MYOS);
}

#
# Function that returns the RedHat release information
#

sub rhat {
    open (MYRH, "cat /etc/redhat-release|");

        while (<MYRH>) {
            $my_rh = $_;
            chomp($my_rh);
            print "RedHat Release Version: $my_rh\n\n";
        }
        close (MYRH);
}

#
# Function that returns number of CPU sockets on the machine
#

sub numcpu {
    open (MYPROC, "cat /proc/cpuinfo  | grep \"physical id\" | sort | uniq | wc -l |");

    while (<MYPROC>) {
        $num_cpu = $_;
        chomp($num_cpu);
        print "Number of CPU/Sockets: $num_cpu\n\n";
    }
    close (MYPROC);
}

#
# Function that returns the number of hardware threads in the processor
#

sub numhwthreads {
    open (MYHWTHREADS, "cat /proc/cpuinfo | grep processor | wc -l |");

    while (<MYHWTHREADS>) {
        $num_threads = $_;
        chomp($num_threads);
        print "Number of Hardware threads: $num_threads\n\n";
    }
    close (MYHWTHREADS);
}

#
# Function that returns the processor type.
#

sub proctype {
    open (MYPROCTYPE, "cat /proc/cpuinfo | grep \"model name\" | uniq |");

    while (<MYPROCTYPE>) {
        $num_cpu = $_;
        chomp($num_cpu);
        @proc_info = split(/:/,$num_cpu);
        print "Processor Type: $proc_info[1]\n\n";
    }
    close (MYPROCTYPE);
}

#
# Function that returns the Memory information
#

sub memory {
    open (MYMEMORY, "cat /proc/meminfo | grep MemTotal |");

    while (<MYMEMORY>) {
        $my_memory = $_;
        chomp($my_memory);
        @memory_kb = split(/\s+/,$my_memory);
        $memory_gb = $memory_kb[1]/1048576;
        print "Memeory: $memory_gb GB\n\n";
    }
    close (MYMEMORY);
}

#
# Function that returns the IP address of the host
#

sub ipaddress {
    open (MYIP, "hostname -i |");

    while (<MYIP>) {
        $my_ip = $_;
        chomp($my_ip);
        print "IP address: $my_ip\n\n";
    }
    close (MYMEMORY);
}

#
# Function that returns the HBA information on the host
#

sub hba {
    open (MYHBA, "lspci | grep HBA|");

    while (<MYHBA>) {
        $my_hba = $_;
        chomp($my_hba);
        print "HBA: $my_hba\n";
    }
    close (MYHBA);
}

os();
rhat();
numcpu();
numhwthreads();
proctype();
memory();
ipaddress();
hba();