Wednesday, 4 December 2013

Perl module installation script

#!/bin/bash

#####Checking shell########

if [ "$0" = "bash" ]
then
echo "Usage: bash scriptname  package.tar.gz"
exit
fi


###Checking error#########

error()
{
if [ $st -eq 0 ]
then
echo $succ
else
echo $fail
exit
fi
unset succ fail
}

###Untaring tar files#####

if [ ! $1 ]
then
echo "Please mention the tar filename"
stop
else
echo $1 | grep ".tar.gz"
st=$?
succ="File is in tar.gz format"
fail="File is not in tar.gz format"
error
gunzip -dc $1 | tar -xvf -
st=$?
succ="file untarer successfully"
fail="Unable to untar the file"
error
cd `basename $1 .tar.gz` && ls
fi


###Checking perl ##########

perl_e=`which perl | grep -i mpde`
if [ ! $PERL_ROOT ] && [ ! $perl_e ]
then
echo "You are using default perl version , do you want to continue ? y/n"
read per
if [ "$per" = "y" ] || [ "$per" = "Y" ]
then
echo "You are about to compile perl modules using default perl version"
else
break
fi
fi

#######Checking Oracle######

if [ ! $ORACLE_HOME ]
then
echo "Oracle is not set , Do you want to continue Y/N"
read ora
if [ $ora = "y" ] || [ $ora = "Y" ]
then
echo "You might not able to install compile some oracle based modules "
stop1
else
break
fi
fi

#####Perl compilation#########

perl Makefile.PL
st=$?
succ="Makefile created"
fail="Error in creating makefile"
gmake
st=$?
succ="Compiled successfully"
fail="Error during compilation"
error
gmake install
st=$?
succ="Perl modules compiled successfully"
fail="Error in compiling perl modules"
error

Thursday, 14 November 2013

grepping line inbetween triggers , using awk

sam() { cat sam | awk 'BEGIN{a="'$1'";s=0} {if($0~a) {s=s+1} {if(s==1) {print $0}} {if(s==2) {s=0}}}';  }


cat sam
trigger
hi
this
is
sam
trigger
this
line
will
not
display
trigger
this
line
will
be
on
display
trigger
bye

usage:
sam trigger

eptuwya@JDQ08R1:~$ sam() { cat sam | awk 'BEGIN{a="'$1'";s=0} {if($0~a) {s=s+1} {if(s==1) {print $0}} {if(s==2) {s=0}}}';  }
eptuwya@JDQ08R1:~$ vim sam
eptuwya@JDQ08R1:~$ sam trigger
trigger

hi

this

is

sam

trigger

this

line

will

be

on

display


Monday, 7 October 2013

python preventing NONE return function

class sam:

        def sam1(self,d):
                if ("bash" in d or "jenkins" in d):
                        return d

s=sam()
f=open("/etc/passwd","r")
for i in f.readlines():
        q=s.sam1(i)
        if q is not None:
                print q                   

Monday, 30 September 2013

Importing our predefined function/class in any python script

File name : modules.py

def add(m):
    y=m+2
    return y

Main file: adding

import modules  ---> importing function from modules.py file
s=modules.add(4) --> Calling functions form modules.py file
print s




Result
python adding
6

python class function with self initialization

class sam:
   
    def __init__(self,a,b):
        self.a=a
        self.b=b

    def sam1(self,d):
        return self.a+self.b+d
       

s=sam(1,2)
z=s.sam1(3)
print z

python search for ipaddress

import os
import re
s=os.popen("ifconfig")
for i in s.readlines():
    switch=re.search( r"(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})", i, re.M|re.I)
    if switch:
        print (switch.group())

Sunday, 29 September 2013

Finding match group in awk

ifconfig | awk --re-interval 'match($0,/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/,ar) {print ar[0]}'

Friday, 20 September 2013

Grep function in python

import os
import sys
import re

grep=sys.argv[1]
file=sys.argv[2]

def search(m):
        switch=re.search(r'%s' %grep, m)
        if switch:
                return m



f=open("%s" %file,"r")

for i in f.readlines():
        s=search(i)
        if s is not None:
                print s

Wednesday, 18 September 2013

Difference and Patch

Difference and Patch

Taking difference between test and test1 folder 


diff -u --recursive --new-file test test1



Patching difference to test folder

patch -p1 -s < ../sam.diff



Tuesday, 17 September 2013

python find and replace with return type

import re

def sam(file):
    return re.sub(":"," ", file)


f=open("/etc/passwd","r")
for i in f.readlines():
    s=sam(i)
    print s

Creating pkg build in solaris

PKG creation in solaris

FILE NEEDED:
       
       pkginfo       ---> Information about the package     
       checkinstall --> operation that you are gonna do while installing
       Postinstall   ---> Operation that  you need to do after installation
       prototype    ----> configuration file for installing pkgs in solaris


  Example:  We are gonna create whole file under /solaris

 pkginfo -->

PKG=”pkgname″
NAME=”test tool”
VERSION=”1.0″
ARCH=”x86″
CLASSES=”none”
CATEGORY=”utility”
VENDOR=”company name”
PSTAMP=”18 Sept″
ISTATES=”S s 1 2 3″
RSTATES=”S s 1 2 3″
BASEDIR=”/solaris”



   


 
   Checkinstall ->

    echo "sam" > /tmp/log



   Postinstall ->

   echo "finished" >> /tmp/log



 ## File that you need to include while installaling


   mkdir sam
  
   cd sam
    echo "include files" > includes


   pkgproto sam/ > ../prototype

   #### The content of file will be look like below##########

 d none sam 0755 root root
 f none sam/includes 0644 root root


#####Ready for installation########################

Append the following line on the top of the file


i pkginfo
i checkinstall
i postinstalld none sam 0755 root root
f none sam/includes 0644 root root


############################################

Creating package into directory format


pkgmk -r -o /solaris -d /solaris -f /solaris/prototype

A compiled file will be created in the directory with packagename --> The which you given in pkginfo file


############################################

Converting package directory into formatted data stream

pkgtrans  /solaris /solaris/package_name.pkg packagename



#############################################



Solaris package will be created with the name "package_name.pkg"



installing package in solaris



pkgadd -i package_name.pkg














Monday, 16 September 2013

Strip extra space in a file using python

import re
f1=open("sam","r")
for i in f1.readlines():
    d=re.sub(' +',' ',i)
    e=d.split(" ")[3]
    if e > 0:
      print  i

Friday, 13 September 2013

Ant build.xml for building java code

<project name="AddDaysToCurrentDate" basedir="." default="main" >
    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="classes.dir"  value="${build.dir}/classes" />
    <property name="jar.dir"  value="${build.dir}/jar" />


    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile" depends="clean" >
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile" >
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="AddDaysToCurrentDate"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar" >
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>
<target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>


</project>

Thursday, 12 September 2013

file filetering in python

import re
import os
import time

def sam(m):
    f1=open("%s" %m, "r")
    for i in f1.readlines():
        if "sam" in i:
            if m:
                return m



l=os.popen("ls -l")
for i in l.readlines():
    if "-r" in i:
        d=re.sub(' +',' ', i)
        w=d.split(" ")[8].strip()
        time.sleep(1)
        j=sam(w)
        if j is not None:
                       print j

Friday, 6 September 2013

continue command in shell with n

for i in $(seq 10) ;
do
for j in $(seq 10)
do
if [ $j -eq 4 ]
then
continue 2
fi
echo $j
sleep 1
done
echo $i
sleep 1
done

Wednesday, 28 August 2013

Saturday, 24 August 2013

Regular expression with class in python

import re

class sam:

        def sam1(self,m):
                for i in file.readlines():
                        switch=re.search(r'%s' %m, i)
                        if switch:
                                print i
                        else:
                                print "bye"


        def sam2(self,m):
                comp=re.compile("m")
                for i in file.readlines():
                        switch=comp.findall(i)
                        if switch:
                                print i
                        else:
                                print "joy"




file=open("/etc/passwd","r")
s=sam()
s.sam2("ba*h")

python regular expression with return type

import re
def rerun(m):
        f1=open("/etc/passwd","r")
        match1=0
        lines=0
        for i in f1.readlines():
                match=re.search(m, i)
                if match:
                        match1 += 1
                lines += 1
        return(match1, lines)

if __name__ == "__main__":
        match, lines = rerun('bash')
        print match
        print lines

python regullar expression

import re
re_obj=re.compile("b*h$")
f1=open("/etc/passwd","r")
for i in f1.readlines():
        switch=re_obj.findall(i)
        if switch:
                print i

Friday, 23 August 2013

Descending order in shell script

s=($*)
func()
{
back=${array[0]}
for((i=0;i<${#array[@]};i++))
do
if [ $back -gt ${array[$i]} ]
then
continue
else
back=${array[$i]}
back_array=$i
fi
done
}

##Array transfer#################
array=("${s[@]}")

for((j=0;j<$#;j++))
do
func
list[$j]=$back
array[$back_array]=0
done

for((k=0;k<$#;k++))
do
echo ${list[$k]}
done

Wednesday, 21 August 2013

shell script alpha encryption

letter1=( a b c d e f g h i j k l m n o p q r s t u v w x y z )
for((ie=0;ie<${#letter1[@]};ie++))
do
lett=${letter1[$ie]}
let $lett=$ie
done
letter="$*"
total_letter=`echo "$letter" | wc -c`
for  je in $(seq 0 $total_letter )
do
d=`echo $letter | cut -c $je 2> /dev/null`
if [ -z $d ]
then
echo $d
else
eval echo \${$d} > /tmp/am1
num=`cat /tmp/am1`
if [ $num -gt 22 ]
then
num1=`echo "25 - $num" |bc`
encrypt=$num1
else
encrypt=` echo "$num +3" | bc`
fi
echo ${letter1[$encrypt]}
fi
done

Sunday, 18 August 2013

grepping a word in a file using python class

f1=open("/etc/passwd","r")

class sam:

        def sam1(self,m):
                for i in f1.readlines():
                        if m in i:
                                print i


v=sam()
v.sam1("bash")

Python class declaration

class sam:

        def add(self,m,n):
                y=m+n
                return y

        def sub(self,m,n):
                y=m-n
                print y

        def mult(self,m,n):
                y=m*n
                print y

s=sam()
ad=s.add(4,5)
print ad

Sunday, 11 August 2013

python script finding remote machine ip

import paramiko
import os
import re

username='user'
port=22
password='password'
hostname='127.0.0.1'


if __name__ == "__main__":
        paramiko.util.log_to_file('paramiko.log')
        s = paramiko.SSHClient()
        s.load_system_host_keys()
        s.connect(hostname, port, username, password)
        stdin, stdout, stderr = s.exec_command('ifconfig')
        d=stdout.read()
        s.close()

for i in d.splitlines():
       if "inet" in i:
               s=re.search(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}',i)
               if s:
                print (s.group())

Sunday, 4 August 2013

grepping particular range of line in a file ( perl)

#!/usr/bin/perl
$start=$ARGV[0];
$end=$ARGV[1];
$filename=$ARGV[2];
chomp($start);
chomp($end);
open(FILE,"$filename");
@file=<FILE>;
close(FILE);
for($i=0;$i<@file;$i++)
{
if ( $file[$i] =~ /$start/ )
{
$x=3;
}
if ( $file[$i] =~ /$end/ )
{
$x=1;
}
if (( $x == 3)||( $x == 1 ))
{
print $file[$i];
if ( $x == 1)
{
$x=10;
}
}
}

USAGE:- perl  filename mysql postfix /etc/passwd

Grepping particular limit of line in a file using shell script

declare -a  x ;while read a; do echo $a | grep 99 && x=0; echo $a | grep 00 && x=1; if [ $x -eq 0 ]; then echo $a; fi; done < <(seq 10000)

Friday, 26 July 2013

fibonacci in perl script

#!/usr/bin/perl
$y=0;
@s=@ARGV;
for($i=0;$i<@s;$i++)
{
$y = $s[$i] + $y;
}
print $y."\n";


usage:  ./filename `seq 100`

fibonacci series in shell script

#!/bin/bash
y=0
s=($@)
for((i=0;i<${#s[@]};i++))
do
y=$((${s[$i]} + $y))
echo $y;
done


usage:

./filename 1 2 3
 6



find and replace in same file in perl

open(FILE,"sam");
@line=<FILE>;
close(FILE);
open(FILE1,">sam");
for($i=0;$i<@line;$i++)
{
$line[$i]=~s/4/charles/sg;
print FILE1 $line[$i];
}
close(FILE1);

Thursday, 11 July 2013

Copying the file along with folder structure

cat file_name | while read a;
 do
 tar cpof - $a | ( cd /home/praveensam.s/clamav_old_files_backup ; tar xpof - ); done

Monday, 8 July 2013

Saturday, 15 June 2013

Finding duplicate entry in a array

my %seen;
@s=qw/ a b c d e f a /;
foreach $i (@s)
{
if ( exists $seen{$i} )
{
$seen{$i}++
}
else
{
$seen{$i}=1;
}
}
foreach $i (keys %seen)
{
print "$i count $seen{$i}.\n";
}
                               

Friday, 31 May 2013

Greping particular word , along with word behind that word in perl

open(FILE,"sam");
while($line=<FILE>)
{
@s=split(":",$line);
for($i=0;$i<@s;$i++)
{
if($s[$i]=~/\(Agreement/)
{
$k=$s[$i+1];
print $s[1]."\t".$s[2]."\t".$s[$i]."\t".$k."\n";
}
}
}
close(FILE);

Thursday, 23 May 2013

storing two or more output in same variable in perl

open(FILE,"/etc/passwd");
while($line=<FILE>)
{
if($line=~/sshd|sam/)
{
$r.=$line;
}
}
close(FILE);
print $r;

Sunday, 19 May 2013

Grepping a particular range of word in a file using perl script

USAGE: perl filename $startingword $lastword $filename

use warnings;
$file=$ARGV[2];
open(FILE,"$file");
$first=$ARGV[0];
$second=$ARGV[1];
while($line=<FILE>)
{
$e++;
if($line=~/$first/)
{
$f=$e;
}
elsif($line=~/$second/)
{
$s=$e;
}
}
close(FILE);
open(FILE1,"$file");
while(<FILE1>)
{
$w++;
if(($w>$f)&&($w<$s))
{
print $_."\n";
}
}
close(FILE1);

Friday, 17 May 2013

Perl multiple pattern match

@q=
(
qr/^luci\b/,
qr/sss/,
);
open(FILE,"sam");
while($line=<FILE>)
{
for($i=0;$i<@q;$i++)
{
if($line=~/$q[$i]/)
{
print "match pattern $line.\n";
}
}
}
close(FILE);

Greping exact word in a file using perl script ( Border usage)

USAGE: perl "script file" "word to grep" "file name where you going to perform the action"

#!/usr/bin/perl
$word=$ARGV[0];
$file=$ARGV[1];
open(FILE,"$file");
while($line=<FILE>)
{
if($line=~/\b$word\b/ )
{
print $line."\n";
}
}
close(FILE);

Thursday, 16 May 2013

find and replace using perl script

USAGE: perl "scriptname" "word to find" "word to replace" "file to which you are going to perform the  action"

$find=$ARGV[0];
$replace=$ARGV[1];
$file=  $ARGV[2];
open(FILE,"$file");
while($line=<FILE>)
{
if($line=~/bash/)
{
$line=~s/$find/$replace/;
print $line."\n";
}
else
{
print $line."\n";
}
}
close(FILE);

Reading each word in a file and counting no of words using perl script

open(FILE,"/etc/passwd");
while($line=<FILE>)
{
@s=split(":",$line);
$r++;
push(@e,@s);
}
foreach $i (@e)
{
print $i."\n";
$w++;
}
print "no of words: $w.\n";
print "No of lines: $r.\n";
close(FILE);

Wednesday, 15 May 2013

Tuesday, 14 May 2013

greping the file with limit

use warnings;
$limit1=$ARGV[0];
$limit2=$ARGV[1];
$file1=$ARGV[2];
open(FILE,"$file1");
sub sam
{
$i=$_[0];
while(<$i>)
{
if(/$limit1/ .. /$limit2/)
{
print $_;
}
}
}
&sam(FILE);

Sunday, 5 May 2013

Grep command in perl

$line=$ARGV[1]||<STDIN>;
$gre=$ARGV[0];
open(FILE,"$line");
while($line1=<FILE>)
{
if ( $line1 =~ /$gre/ )
{
print $line1."\n";
}
}
close(FILE);

Monday, 22 April 2013

lock process and allow the process to run after 1 mins

current=`date +"%s"`
last=`cat /tmp/last_date`
difference=`echo "$current - $last" | bc`
if [ ! -e /tmp/lock_file ] && [ $difference -gt 70 ]
then
touch /tmp/lock_file
sleep 10
echo "your command"
rm -rvf /tmp/lock_file
date +"%s" > /tmp/last_date
fi

Tuesday, 16 April 2013

greping particular range of lines in a file

open(FILE,"/etc/passwd");
$f=$ARGV[0];
$l=$ARGV[1];
while($line=<FILE>)
{
$d++;
if(($d>$f)&&($d<$l))
{
print $line."\n";
}
}
close(FILE);

usage: perl scriptname "line start" "endline"

Sunday, 14 April 2013

single if statement



print "Enter the password";
chomp ($s=<STDIN>);
$s eq "y" ? print "hi" : print "Bye" ;

Comparing File

use File::Compare;
if(compare("sam","sam1"))
{
print "file are same";
}
else
{
print "Files are not sare";
}

Changing Directory

chdir("/etc");
@s=readpipe(ls);
for($i=0;$i<@s;$i++)
{
push(@e,$s[$i]);
}
for($i=0;$i<@e;$i++)
{
print $e[$i]."\n";
}

Friday, 12 April 2013

Tuesday, 9 April 2013

Anchor in perl

#!/usr/bin/perl
open(FILE,"/etc/passwd");
while($line=<FILE>)
{
if ( $line =~ /ser\b/ )
{
print $line."\n";
}
}

Monday, 8 April 2013

Multithreading shell script

rm -rvf /tmp/lock
ls test/ | grep "sam[0-9]*" | tee /tmp/sam
proc()
{
if [ `ps aux | egrep -v 'watch|top|grep' | grep -i $0 | wc -l` -le 5 ]
then
$1 &
fi
}
funct()
{
until [ -e /tmp/lock ]
do
touch /tmp/lock
for i in `cat /tmp/sam`
do
cp -rvf test/$i test2/
sed -i "/$i/d" /tmp/sam
sleep 2
rm -rvf /tmp/lock
done
done
}
empt()
{
if [ `cat /tmp/sam | wc -l` -eq 0 ]
then
exit
fi
}
cat /tmp/sam | while read a
do
proc funct&
sleep 2
empt
done

Thursday, 4 April 2013

Find a file and rename th e file

use File::Find;
use File::Copy;
find(\&sam,"sam");
sub sam
{
copy("sam","samdev");
}

readpipe command

@s=readpipe(ls);
for($i=0;$i<@s;$i++)
{
if ( $s[$i] =~ /sam/ )
{
print $s[$i];
}
}

Perl redo

LINK: until ( $e eq "sam" )
{
&sa;
print Hi."\n";
redo LINK;
}

sub sa
{
chomp($e=<STDIN>);
}

Monday, 18 March 2013

making file as array

#!/usr/bin/perl
open(FILE,"/etc/passwd");
while($line=<FILE>)
{
@s=split(":",$line);
$e=(@s - 1);

print $s[(@s - 2)]."\n";
}
close(FILE);

Sunday, 17 March 2013

finding ntpdate in syncing or not ( positive or negative in perl )

#!/usr/bin/perl
@s=readpipe("ntpdate -q 0.centos.pool.ntp.org");
for($i=0;$i<@s;$i++)
{
if ( $s[$i] =~ /sec/i )
{
@e=split(" ",$s[$i]);
$el=@e;
$el1=($el -2);
if ( $e[$el1] > 0 )
{
print "positive"."\n";
}
elsif ( $e[$el1] < 0 )
{
print "Negative $e[$el1].\n";
}
}
}