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)