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