Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Python script to check if file is locked also display last modified time

To lock file for testing use this command 

The below command will lock /home/ubuntu/fileloc

flock -x -w 5 /home/ubuntu/fileloc echo "4" >  /home/ubuntu/fileloc && sleep 5

------------------------
test.py
------------------------
import os, time
import datetime as dt

def is_locked(filepath):
   
    locked = None
    file_object = None
    if os.path.exists(filepath):
        try:
            print "Trying to open %s." % filepath
            buffer_size = 8
            # Opening file in append mode and read the first 8 characters.
            file_object = open(filepath, 'a', buffer_size)
            if file_object:
                print "%s is not locked." % filepath
                locked = False
        except IOError, message:
            print "File is locked (unable to open in append mode). %s." % \
                  message
            locked = True
        finally:
            if file_object:
                file_object.close()
                print "%s closed." % filepath
    else:
        print "%s not found." % filepath
    return locked

def wait_for_files(filepaths):
   

    for filepath in filepaths:
      
       if is_locked(filepath):
            print "%s is currently in use." % \
                  (filepath)
            st = os.stat(filepath)    
            mtime = dt.datetime.fromtimestamp(st.st_mtime)
            print('%s modified %s'%(filepath, mtime))

                

# Test
if __name__ == '__main__':
    files = [r"/home/ubuntu/filelock"]
    print wait_for_files(files)
-------------------------

python test.py

How to parse xml tags in bash or retrieve xml tag values in linux


Consider the following example file test.xml





<header>
        <tag1>1</tag1>
        <system>test123</system>
        <cdc>230</cdc>
       
</header>

   
 


 grep -E -m 1 -o "(.*)" test.xml

This will return "test123"


if you want only the value we need to pipe it further

grep -E -m 1 -o "(.*)" test.xml | sed -e 's,.*\([^<]*\).*,\1,g'

This will return "test123"

Script to import csv into dhcpd.conf import multiple mac bindings

make sure your csv file is in this order MAC,IP,NAME and file name of csv is ips.csv
Once that is done just run the script and check mac_out.txt which is file to be imported in dhcpd.conf

!/bin/bash

INPUT=ips.csv
OUTPUT=mac_out.txt
OLDIFS=$IFS
IFS=","
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
[ -f $OUTPUT ] && { rm $OUTPUT; }
[ ! -f $OUTPUT ] && { touch $OUTPUT; }
while read mac ip uname
do
echo "host $uname {" >> $OUTPUT
echo -e "\thardware ethernet $mac;" >> $OUTPUT
echo -e "\tfixed-address $ip;" >> $OUTPUT
echo "}" >> $OUTPUT
done < $INPUT

IFS=$OLDIFS


Linux bash script for checking and sending email alerts on disk space issues

#!/bin/sh
FILE=/tmp/mailcontent.txt

> /tmp/mailcontent.txt

df -H | awk '{ print $5 " " $6 }'|head -6|tail -5|while read output;

do
#  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
echo $usep
  if [ $usep -ge 90 ]; then
   echo -e "Running out of space \n\nPartition:\"$partition ($usep%)\" \nHost:$(hostname) \nDate:$(date)\n" >> /tmp/mailcontent.txt

 fi

done

if [[ -s $FILE ]] ; then

mail -s "CRITICAL ALERT:Disk space of $hostname more that 90%" disk-alert@yourcompany.com
else
echo "$FILE is empty."
fi

exit

Bash Script to get public ip

#!/bin/bash




pubip=$( curl http://ip4.me 2>/dev/null | sed -e 's#<[^>]*>##g' | grep '^[0-9]' )



echo $pubip



you can replace curl command with



curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' 

Bash script to add some line to a file at specific line number

I wrote this example to show how to append JAVA_OPTS parameters to startup.sh of tomcat



#!/bin/bash

NLINE=2
s=" export JAVA_OPTS=\"$JAVA_OPTS -Xms1024m -Xmx1024m -XX:MaxPermSize=256m\""
sp=" "
awk -vn="$NLINE" -vs="$s" -vsp="$sp" 'NR==n{$0=$0 sp s}1' startup.sh >temp
mv temp startup.sh



Sp is space given so that if something exists in that line it will give some space and
append our command or string

Bash script to check command-line arguments and set default if not present

if [ -n "$1" ]
# Test whether command-line argument is present (non-empty).
then
lines=$1
else
lines=$LINES # Default, if not specified on command-line.
fi

how to encrypt your scripts using sch

Dont want to give your scripts to any one but want to give someone to execute for specific period of time then this is what you are looking for .
We can also give timeline in which the script can be used.

Also you will not be revealing you passwords in the script.

SCH will encrypt shell scripts using RC4 and make executable binary.

http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz

shc [ -e date ] [ -m addr ] [ -i iopt ] [ -x cmnd ]
[ -l lopt ] [ -ACDhTv ] -f script

shc -e date ( script will not run afterdate specified )
-m "Please contact support " (message to display after we run the expired scirpt)

shc -e 01/06/2010 -m "You are late to run this script." -f script.sh

./script.sh.x



http://www.datsi.fi.upm.es/~frosal/sources/shc.html

I have tried to hack the encrypted binary that came as output, but was not successful
I used bashdb and GDB

Per user mac based bandwidth monitor for routers


Want to calculate how much bandwidth each user (mac address) is using


    ./wrtbwmon setup
   ./wrtbwmon update /tmp/usage.db offpeak
   ./wrtbwmon publish /tmp/usage.db /www/user/usage.htm /jffs/users.txt

Here is open source solution ClickHere

Below is the script  (wrtbwmon )
---------------------------

LAN_IFACE=$(nvram get lan_ifname)

lock()
{
while [ -f /tmp/wrtbwmon.lock ]; do
if [ ! -d /proc/$(cat /tmp/wrtbwmon.lock) ]; then
echo "WARNING : Lockfile detected but process $(cat /tmp/wrtbwmon.lock) does not exist !"
rm -f /tmp/wrtbwmon.lock
fi
sleep 1
done
echo $$ > /tmp/wrtbwmon.lock
}

unlock()
{
rm -f /tmp/wrtbwmon.lock
}

case ${1} in

"setup" )

#Create the RRDIPT CHAIN (it doesn't matter if it already exists).
iptables -N RRDIPT 2> /dev/null

#Add the RRDIPT CHAIN to the FORWARD chain (if non existing).
iptables -L FORWARD --line-numbers -n | grep "RRDIPT" | grep "1" > /dev/null
if [ $? -ne 0 ]; then
iptables -L FORWARD -n | grep "RRDIPT" > /dev/null
if [ $? -eq 0 ]; then
echo "DEBUG : iptables chain misplaced, recreating it..."
iptables -D FORWARD -j RRDIPT
fi
iptables -I FORWARD -j RRDIPT
fi

#For each host in the ARP table
grep ${LAN_IFACE} /proc/net/arp | while read IP TYPE FLAGS MAC MASK IFACE
do
#Add iptable rules (if non existing).
iptables -nL RRDIPT | grep "${IP} " > /dev/null
if [ $? -ne 0 ]; then
iptables -I RRDIPT -d ${IP} -j RETURN
iptables -I RRDIPT -s ${IP} -j RETURN
fi
done
;;
"update" )
[ -z "${2}" ] && echo "ERROR : Missing argument 2" && exit 1
# Uncomment this line if you want to abort if database not found
# [ -f "${2}" ] || exit 1

lock

#Read and reset counters
iptables -L RRDIPT -vnxZ -t filter > /tmp/traffic_$$.tmp

grep -v "0x0" /proc/net/arp  | while read IP TYPE FLAGS MAC MASK IFACE
do
#Add new data to the graph. Count in Kbs to deal with 16 bits signed values (up to 2G only)
#Have to use temporary files because of crappy busybox shell
grep ${IP} /tmp/traffic_$$.tmp | while read PKTS BYTES TARGET PROT OPT IFIN IFOUT SRC DST
do
[ "${DST}" = "${IP}" ] && echo $((${BYTES}/1000)) > /tmp/in_$$.tmp
[ "${SRC}" = "${IP}" ] && echo $((${BYTES}/1000)) > /tmp/out_$$.tmp
done
IN=$(cat /tmp/in_$$.tmp)
OUT=$(cat /tmp/out_$$.tmp)
rm -f /tmp/in_$$.tmp
rm -f /tmp/out_$$.tmp
if [ ${IN} -gt 0 -o ${OUT} -gt 0 ];  then
echo "DEBUG : New traffic for ${MAC} since last update : ${IN}k:${OUT}k"
LINE=$(grep ${MAC} ${2})
if [ -z "${LINE}" ]; then
echo "DEBUG : ${MAC} is a new host !"
PEAKUSAGE_IN=0
PEAKUSAGE_OUT=0
OFFPEAKUSAGE_IN=0
OFFPEAKUSAGE_OUT=0
else
PEAKUSAGE_IN=$(echo ${LINE} | cut -f2 -s -d, )
PEAKUSAGE_OUT=$(echo ${LINE} | cut -f3 -s -d, )
OFFPEAKUSAGE_IN=$(echo ${LINE} | cut -f4 -s -d, )
OFFPEAKUSAGE_OUT=$(echo ${LINE} | cut -f5 -s -d, )
fi
if [ "${3}" = "offpeak" ]; then
OFFPEAKUSAGE_IN=$((${OFFPEAKUSAGE_IN}+${IN}))
OFFPEAKUSAGE_OUT=$((${OFFPEAKUSAGE_OUT}+${OUT}))
else
PEAKUSAGE_IN=$((${PEAKUSAGE_IN}+${IN}))
PEAKUSAGE_OUT=$((${PEAKUSAGE_OUT}+${OUT}))
fi

grep -v "${MAC}" ${2} > /tmp/db_$$.tmp
mv /tmp/db_$$.tmp ${2}
echo ${MAC},${PEAKUSAGE_IN},${PEAKUSAGE_OUT},${OFFPEAKUSAGE_IN},${OFFPEAKUSAGE_OUT},$(date "+%d-%m-%Y %H:%M") >> ${2}
fi
done
#Free some memory
rm -f /tmp/*_$$.tmp
unlock
;;
"publish" )

[ -z "${2}" ] && echo "ERROR : Missing argument 2" && exit 1
[ -z "${3}" ] && echo "ERROR : Missing argument 3" && exit 1
USERSFILE="/etc/dnsmasq.conf"
[ -f "${USERSFILE}" ] || USERSFILE="/tmp/dnsmasq.conf"
[ -z "${4}" ] || USERSFILE=${4}
[ -f "${USERSFILE}" ] || USERSFILE="/dev/null"

# first do some number crunching - rewrite the database so that it is sorted
lock
touch /tmp/sorted_$$.tmp
cat ${2} | while IFS=, read MAC PEAKUSAGE_IN PEAKUSAGE_OUT OFFPEAKUSAGE_IN OFFPEAKUSAGE_OUT LASTSEEN
do
echo ${PEAKUSAGE_IN},${PEAKUSAGE_OUT},${OFFPEAKUSAGE_IN},${OFFPEAKUSAGE_OUT},${MAC},${LASTSEEN} >> /tmp/sorted_$$.tmp
done
unlock

        # create HTML page
        echo "Traffic

Total Usage :

" >> ${3}
        echo "" >> ${3}
        echo "
UserPeak downloadPeak uploadOffpeak downloadOffpeak uploadLast seen
" >> ${3}
        echo "
This page was generated on `date`" 2>&1 >> ${3}
        echo "" >> ${3}

        #Free some memory
        rm -f /tmp/*_$$.tmp
        ;;

*)
echo "Usage : $0 {setup|update|publish} [options...]"
echo "Options : "
echo "   $0 setup"
echo "   $0 update database_file [offpeak]"
echo "   $0 publish database_file path_of_html_report [user_file]"
echo "Examples : "
echo "   $0 setup"
echo "   $0 update /tmp/usage.db offpeak"
echo "   $0 publish /tmp/usage.db /www/user/usage.htm /jffs/users.txt"
echo "Note : [user_file] is an optional file to match users with their MAC address"
echo "       Its format is : 00:MA:CA:DD:RE:SS,username , with one entry per line"
exit
;;
esac

Script to check weather user is root or not

if [ "$UID" -ne "$ROOT_UID" ]
then
 echo "Must be root to run this script."
 exit $E_NOTROOT
fi

Common Scripting errors for beginners

when i  started scripting i had these errors in the begenning . The most common mistakes one makes during his initial stages of scripting are.

1.Check script file permissions. A script needs execute permission


2.We should not use default values that generate errors.If you
  give those your script execution breaks in the middle.

3.We must supply parameters as required ie make parameter mandatory
  if parameter is to be provided for sure, or else it throws an error.

4. Resuable script should not be hard-coded values.Use parameters  instead.

5.Commenting the script is very important... Make it a habbit to write
  comments.Also specify in comments if script has multiple parameters.
  you can also write examples in comments for better understanding.

6. Error handling is a must for scripting

Script to recursively check a folder and delete files older



#!/bin/bash
Dir="/backup/backup/"
days="30"

find $Dir -type f -mtime +$days -exec rm {} \;




modify days value according to requirement. How old files you want to delete.

Python IP V4 validation

def validIP(address):
    parts = address.split(".")
    if len(parts) != 4:
        return False
    for item in parts:
        if not 0 <= int(item) <= 255:
            return False
    return True

Bash Script to find CPU type or model

#!/bin/sh

if [ -f /proc/cpuinfo ]; then

    echo -n "MODELNAME=\""
    grep -m 1 "model name" /proc/cpuinfo | cut -d: -f2 | sed -e 's/^ *//' | sed -e 's/$/"/'
   
fi



OUTPUT:
MODELNAME="Intel(R) Core(TM)2 Duo CPU     E8200  @ 2.66GHz"

Bash Script to find system architecture

#!/bin/sh

echo ARCH=`uname -m`



OUTPUT:


ARCH=x86_64