This issue affects all products which use the Bash shell and parse values of environment variables. This issue is especially dangerous as there are many possible ways Bash can be called by an application. Quite often if an application executes another binary, Bash is invoked to accomplish this. Because of the pervasive use of the Bash shell, this issue is quite serious and should be treated as such.

All versions prior to those listed as updates for this issue are vulnerable to some degree.

My infected OS version is CentOS-6 and bash version 4.1.2

[root@host75 ~]# lsb_release -a
lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.4 (Final)
Release: 6.4
Codename: Final

[root@host75 ~]# bash --version
bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Diagnostic Steps:

To test if your version of Bash is vulnerable to this issue, run the following command:

$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test"

If the output of the above command looks as follows:

vulnerable
this is a test

hmm, I got infected!

[root@host75 ~]# env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
vulnerable
this is a test

You are using a vulnerable version of Bash. The patch used to fix this issue ensures that no code is allowed after the end of a Bash function. Thus, if you run the above example with the patched version of Bash, you should get an output similar to:

$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
bash: warning: x: ignoring function definition attempt
bash: error importing function definition for `x'
this is a test

If your system is vulnerable, update to the most recent version of the Bash package by running the following command:

$yum update bash

This fix my bash ??????

[root@host75 ~]# yum update bash
Loaded plugins: fastestmirror, security, tmprepo
Loading mirror speeds from cached hostfile
epel/metalink | 15 kB 00:00
* base: centos.eecs.wsu.edu
* epel: mirrors.kernel.org
* extras: centos.chi.host-engine.com
* updates: mirror.raystedman.net
base | 3.7 kB 00:00
epel | 4.4 kB 00:00
epel/primary_db | 6.3 MB 00:05
extras | 3.3 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 5.3 MB 00:04
Setting up Update Process
Resolving Dependencies
--> Running transaction check
---> Package bash.x86_64 0:4.1.2-14.el6 will be updated
---> Package bash.x86_64 0:4.1.2-15.el6_5.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================
Updating:
bash x86_64 4.1.2-15.el6_5.1 updates 905 k

Transaction Summary
================================================================================================================================================
Upgrade 1 Package(s)

Total download size: 905 k
Is this ok [y/N]: y
Downloading Packages:
bash-4.1.2-15.el6_5.1.x86_64.rpm | 905 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : bash-4.1.2-15.el6_5.1.x86_64 1/2
Cleanup : bash-4.1.2-14.el6.x86_64 2/2
Verifying : bash-4.1.2-15.el6_5.1.x86_64 1/2
Verifying : bash-4.1.2-14.el6.x86_64 2/2

Updated:
bash.x86_64 0:4.1.2-15.el6_5.1

Complete!

Test if update fixed to patch your bash

[root@host75 ~]# env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
bash: warning: x: ignoring function definition attempt
bash: error importing function definition for `x'
this is a test

How does this impact systems:
This issue affects all products which use the Bash shell and parse values of environment variables. This issue is especially dangerous as there are many possible ways Bash can be called by an application. Quite often if an application executes another binary, Bash is invoked to accomplish this. Because of the pervasive use of the Bash shell, this issue is quite serious and should be treated as such.

All versions prior to those listed as updates for this issue are vulnerable to some degree.

See the appropriate remediation article for specifics.

Functions written in Bash itself do not need to be changed, even if they are exported with “export -f”. Bash will transparently apply the appropriate naming when exporting, and reverse the process when importing function definitions.

Ref:
http://www.bbc.com/news/technology-29361794
https://www.us-cert.gov/ncas/current-activity/2014/09/24/Bourne-Again-Shell-Bash-Remote-Code-Execution-Vulnerability
https://access.redhat.com/articles/1200223
https://securityblog.redhat.com/2014/09/24/bash-specially-crafted-environment-variables-code-injection-attack/
http://lists.gnu.org/archive/html/bug-bash/2014-09/threads.html
https://rhn.redhat.com/errata/RHSA-2014-1306.html

(0) Comments    Read More   

Hello Everyone,
In day to day System Admin activities many times, you stuck to connect to any remote server, due to non-supporting timeout setting, here is the perfect solution for that ‘hatools’ – Thanks ‘MARKUS WINAND’, You can find this tool at http://www.fatalmind.com/software/hatools/

This tool will help you to manage your application and code with the specific timeout and lock, and you will be assure and go for sleep  😀

Download hatools from www.fatalmind.com (http://www.fatalmind.com/software/hatools/hatools-2.14.tar.bz2)

[root@testbed ~]# wget http://www.fatalmind.com/software/hatools/hatools-2.14.tar.bz2

[root@testbed ~]# tar xjvf hatools-2.14.tar.bz2 && cd hatools-2.14

The installation should be very seamless by just doing (Find the doc ‘README’)

[root@testbed hatools-2.14]# ./configure
[root@testbed hatools-2.14]#  make && make install

Now test the hatimerun command

[root@testbed ~]# hatimerun -h
usage: hatimerun [-a] [-e exitcode] [-k signame] -t secs command [args]
hatimerun [-l|-h|-?]
Options:
-a           Async mode. Starts hatimerun in the background
-e exitcode  Changes the exitcode returned by hatimerun on fail
-k signame   Specifies the signal witch will be sent to the process group
if a timeout occures
-t secs      Specifies the timeout in seconds
-l           Print list of available signals on this platform and exit.

Version:
V2.00
Copyright (c) 2001,2003,2005-2007 by Markus Winand <mws@fatalmind.com>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

[root@testbed ~]# hatimerun -k TERM -t 15 -t 1 ssh testbed140 uptime
04:36:23 up  3:24,  0 users,  load average: 0.51, 0.40, 0.31

Check with wrong or any ssh port blocked server and get timeout watch. 🙂

Here I am written small script for testing purpose, you can change and use as you want.

#!/bin/bash

# if you want to check and debug , run this script in following way
# bash -x script name <servername> <cmd>

server=$1  # server name or IP
cmd=$2   # command
sec=15      #Timeout seconds change as per your need

test -z $server && echo “server not found ” && echo “Usage: $0 {servername} {command}” && exit 1
test -z $cmd &&  echo “command not found ” && echo “Usage: $0 {servername} {command}” && exit 1
echo “connecting to $server, timewait set for connection is $sec Sec…….”

hatimerun -k TERM -t $sec -t 1 ssh $server $cmd
if [ $? != 0 ] ; then
echo “$server connection timeout …”
fi

 

 

-Ravi

(0) Comments    Read More   
Dec
31
Posted on 31-12-2009
Filed Under (Linux commands, Shell scripting) by Arun Bagul

Introduction ~

—————————————–
Program (1) ~ array.sh
—————————————–

#!/bin/bash
echo “==============”
declare -a myarr[0]=”Arun”
declare -a myarr1
myarr1=(arun bagul bangalore mumbai raju santhosh)
myarr[1]=”Bagul”

echo “my name is ${myarr[0]} ${myarr[1]}”
echo “————————–”
echo “${myarr1[*]}”
echo ${myarr1[2]}
echo ${myarr1[@]}
echo “————————–”
echo “Total no of elements in array – ${#myarr1[*]}”
echo “Total no of elements in array – ${#myarr1[@]}”
echo “Size of word ‘${myarr1[2]}’ is – ${#myarr1[2]}”
echo ${#myarr1[1]}
echo ${#myarr1[0]}

echo “————————–”

#how to delete element in array
unset myarr[1]
echo “myarr is – ${myarr[*]}”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

echo ${myarr}

————————————————————————
Program (2) ~ command_line_arguments.sh
————————————————————————

#!/bin/bash

echo “Script/command name => $0”
echo “arg1 => $1”
echo “arg2 => $2”
echo “arg3 => $3”
echo “Total No of argument = $#”

echo “Script PID is => $$”
echo “Status of previous command – $?”

name=$myname
echo “Name – $name”

read n

————————————————-
Program (3) ~ default_value.sh
————————————————-

#!/bin/bash

#start=’123′
#start=${1:-$start}

start=${1:-‘123’}

echo “Value of ‘start’ variable is ==> $start”

—————————————————
Program (4) ~ echo_example.sh
—————————————————

#!/bin/bash

name=”Arun”

echo -e “My Name is $name_arun and \n”
echo -e “My Name is ${name}_arun and \n”

echo -e ‘My Name is $name and \n’

—————————————–
Program (5) ~ elif.sh
—————————————–

#! /bin/bash

if [ $1 -eq $2 ];then
echo “good”
elif [ $2 -eq $3 ];then
echo “Fine”
elif [ $1 -eq $3 ];then
echo “OK”
else
echo “NO”
fi

————————————————————
Program (6) ~ for_loop_example-1.sh
————————————————————

#!/bin/bash

i=1
while [ $i -le 512 ]
do
temp=$i
echo “What is => $i | $temp”
i=$(expr $i + 32)
for (( j=$temp; $j<=$i; j++ ))
do
echo -n ” $j”
done
done

———————————————————–
Program (7) ~ for_loop_example-2.sh
———————————————————–

#!/bin/bash
#for val in $(ls -1 /tmp)
sum=0
#for val in {1..5}
#for val in {$1..$2}
for((val=$1;$val<=$2;val++))
do
#echo “$val”
sum=$(expr $sum + $val )
#sum=`expr $sum + $val`
done

echo “$0 # Sum of $1 to $2 => $sum”

————————————————————
Program (8) ~ for_loop_example-3.sh
————————————————————

#!/bin/bash

for i in {1..9}
do
echo $i
done

—————————————–
Program (9) ~ function.sh
—————————————–

#!/bin/bash

function my_function()
{
name=”Arun Bagul”
echo “‘my_function’ body ~ $name”
return 1;
}
##########

myfunc()
{
echo “Another way of defining the function”
}

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

echo “Starting function program”
echo “——————————”

#calling function here
my_function
##
myfunc

echo -e “\n end of program”

—————————————————————————————
Program (10) ~ how_to_pass_argument_to_function.sh
—————————————————————————————

#!/bin/bash

function my_function()
{
echo “Total number of argument ~ $#”
echo “Arg1 => $1”
echo “Arg2 => $2”
echo “Arg3 => $3”
return 0;
}
##########

echo “Starting function program”
echo “——————————”

#calling function here
my_function arun bagul 1234

————————————————————————-
Program (11) ~ how_to_take_hidden_input.sh
————————————————————————-

#!/bin/bash

echo -n “Enter User Name :”
read username
echo -n “Enter Password :”
read -s mypwd

echo -e “\nI am $username and my password is – $mypwd”

——————————————————————————
Program (12) ~ how_to_take_input_from_user.sh
—————————————————————————–

#!/bin/bash

echo -ne “Enter the Name:- ”
read name
echo -n -e “Enter the Number:- ”
read num

echo “——————————”

add=$(expr $num + 10)

echo “Name is ~ $name”
echo “Number is ~ $add”

—————————————–
Program (13) ~ ifthen.sh
—————————————–

#!/bin/bash

if [ “arun” == “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 == 2 ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ “arun” = “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 -eq 2 ];then
echo “true!”
else
echo “false”
fi

——————————————————
Program (14) ~ non-interactive.sh
——————————————————

#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “pwd\r”
interact

—————————————————————-
Program (15) ~ read_file_line_by_line.sh
—————————————————————-

#!/bin/bash

file_name=”/etc/hosts”

while read myvar
do
echo “Line => $myvar”
done < $file_name

echo “#################################################”

for myvar1 in $(cat $file_name)
do
echo “Line => $myvar1”
done

——————————————————
Program (16) ~ reverse-number.sh
——————————————————

#!/bin/bash

declare -a date_array
num=$1
i=$(expr $(echo $num | wc -c) – 1 )

while [ $num -gt 10 ]
do
temp=$( expr $num % 10 )
num=$( expr $num / 10);
echo “Digit($i) => $temp”
date_array[$i]=”${temp}”
i=$(expr $i – 1)
done
echo “Digit($i) => $num”
date_array[$i]=”${num}”

echo ${date_array[*]}

——————————————————–
Program (17) ~ string-operation.sh
——————————————————–

#! /bin/bash

echo “Arun Bagul:-”
string=”/root/arun/bagul/image.gif”
echo “string=> $string”
echo “String=> ${string##/*/}”
echo “String=> ${string#/*/}”

echo “String=> ${string%.*}”
echo “String=> ${string%%.*}”
#str=”/home/y/conf/arunbagul/daily_market_0.conf”
str=”${str##/*conf/}”
echo “String=> ${str%/*}”

#done

mystr=”keyword_summary_exact_arunsb”
echo $mystr
echo ${mystr%_*}

echo “$*”

—————————————–
Program (18) ~ switch.sh
—————————————–

#!/bin/bash

echo ” Switch program | arg1 => $1″
echo ” ——————————-”
case $1 in

123)
echo “Case is 123”
;;

arun)
echo “Case is ‘arun'”
;;

pri*)

echo “Case is ‘pri*'”
;;

*)
echo ” * Usage: $0 ”
echo ” Default case (nothing is matched)”
exit 0;
;;
esac

—————————————————————–
Program (19) ~ while_loop_example-1.sh
——————————————————————

#!/bin/bash

mywait=wait

while [ “${mywait}” = “wait” ]
do
echo “arun”
done

——————————————————————-
Program (20) ~ while_loop_example-2.sh
——————————————————————–

#! /bin/bash

## on command line -> i=0 && while [ $i -le 10 ] ; do echo $i; i=$(expr $i + 1); done

i=0
while [ $i -le 10 ]
do
echo $i
i=$(expr $i + 1)
done

——————————————————————–

* Please download PDF file http://www.slideshare.net/arunbagul/bash-learning-by-examples/

Regards,
Arun

(1) Comment    Read More   
Jun
21
Posted on 21-06-2009
Filed Under (Shell scripting) by Ravi Bhure

Hello Friends,

Many days back, I have configured this storageworks devices of HP and nowadays one of my friend is having like the same issue’s with the os’s to configuring HP storageworks, he has been doing manual driver scan with hp_ltt tool every time when system has been rebooted. Actually there some thing missing with os’s but still this scripts work for him now.

#!/usr/bin/expect  —
# This expect script is useful and working with driver Diagnostic automated tool
# I have tested this over “HP StorageWorks Library and Tape Tools”
# Please change the option number 10 for rescan as per mention in your hp_ltt script.
# Some older hp_ltt script it has option number 9, so please check once this option.
# If you have any queries about this script please send email on ravi <at> indiangnu <dot> org
set timeout -1
spawn ./hp_ltt
match_max 100000
send — “r”
# Look for prompt
expect “ommand>”
# Send option 1 for hardware scan
send — “1r”
send — “r”
# Look for  prompt
expect “ommand>”
# Send option 3 for continue
send — “3r”
send — “r”
# Look for prompt
expect “ommand>”
# send option 10 for rescan
send — “10r”
send — “r”
# Look for prompt
expect “ommand>”
# Send 1 (exit) from L & TT
send — “1r”
send — “r”
expect eof

Suggestion : You have to run this expect script under the $HOME folder of hp_ltt script.

–Ravi

(0) Comments    Read More   
Sep
05
Posted on 05-09-2008
Filed Under (Shell scripting) by Ravi Bhure

This script is very useful for system admins, it checks Cpuload and get info of which process takes the cpuload, if cpuload is or above 70%  it sends alert email to admin

==============================

#!/bin/bash
# Shell script to monitor or watch the high cpu-load
# It will send an email to $ADMIN, if the (cpu load is in %) percentage
# of cpu-load is >= 70%
# If you have any suggestion or question please email to ravi<at>indiangnu <dot> org
# set admin email so that you can get email
# set alert level 70% is default
# you can set it to string LOAD with your value

AWK=/bin/awk
SAR=/usr/bin/sar
GREP=/bin/grep
TR=/usr/bin/tr
HEAD=/usr/bin/head
PS=/bin/ps
SORT=/bin/sort
HOSTNAME=indiangnu.org
SED=/bin/sed
LOAD=70
CAT=/bin/cat
MAILFILE=/tmp/mailviews$$
MAILER=/bin/mail
mailto=”ravi@indiangnu.org”
for path in $PATHS
do
CPU_LOAD=`$SAR -P ALL 1 2 | $GREP ‘Average.*all’ | $AWK -F” ” ‘{ print 100.0 -$NF}’`
echo $CPU_LOAD
if [[ $CPU_LOAD > $LOAD ]];
then
PROC=`$PS -eo pcpu,pid -o comm= | $SORT -k1 -n -r | $HEAD -1`
echo “Please check your processess on ${HOSTNAME} the value of cpu load is $CPU_LOAD % & $PROC” > $MAILFILE
$CAT $MAILFILE | $MAILER -s “CPU Load is $CPU_LOAD % on ${HOSTNAME}” $mailto
fi
done

=============================

After  end of  schedule cron job for this script like below

*/30 * * * * /bin/sh /root/cpuload.sh >/dev/null 2>&1

Thanks

Ravi

(0) Comments    Read More   
Jul
14
Posted on 14-07-2008
Filed Under (Shell scripting) by Arun Bagul

Introduction – Array is one of the best feature of Bash shell and  I am fan of this feature!!. Bash shell provides one-dimensional array. Any variable may be used as an array. There are many ways to declare array. Builtin “declare” command is used to explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based. A array is created automatically if any variable is assigned to using the syntax array_name[subscript]=value. The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero.  To explicitly declare an array, use “declare  -a  name“. “declare -a name[index]” is also accepted and the “index” is ignored here.

Arrays are assigned to using compound assignments of the form array_name=(value1,value2, … valueN), where each value is of the form [index]=string. Only string  is required. In bash array indexing is same as C language which starts at zero. This syntax is also accepted by the declare builtin.

* How to assign/declare array variable in Bash shell –

declare -a myarr[0]=”Arun”
declare -a  my_array
my_array=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)
apna_array=([0]=arun [1]=bagul [4]=Santhosh [3]=ravi [5]=1024)
array[1]=”Bagul”

* How to refer the array and it’s element –

Suppose I want to print the above array “apna_array”, the syntax as shown below….

echo “${apna_array[*]}”
and
echo “${apna_array[@]}”

This syntax is very common in bash shell, ${variable_name} is same as $variable_name. While in array the “[*]” and “[@]” indicate the all element of array.
NOTE – Remember above two  syntax are not same there is one difference. It’s an assignment for you guys/girls!

* How to access particular element of array –

name=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)

here “name” is an array and I want to access the first and second element of the array “name”.

echo “My name is – ${name[0]}  ${name[1]}”

* How to find the length of array or element of array –

${#my_array[index]} expands to the length of (element of array) ${my_array[index]} . If “index” is “*” or “#” then expansion is the number of elements in the array. Please note – referencing an array variable without a subscript/index is equivalent to referencing element zero.

Suppose “student” is array then, ${student} is same as ${student[0]}

#declare and assign elements to array “student_record” –
student_record=(10 “Ravi Bhure” “1st year of BE” “SSVPS College” Dhule)

echo ${student_record}
echo ${student_record[0]}

echo “Total no of element in array is – ${#student_record[*]}”
echo “Total no of element in array is – ${#student_record[@]}”

echo “Length of element is – ${#student_record[1]}”

* How to delete element of array –

unset”  builtin command is used to delete element of array.

unset array_name[index]  – delete the array element at given “index”. If “index” is “*” or “@” then, it will removes the entire array.

big_city=(Bangalore Pune Nasik Hydrabad Surat)

Here “big_city” is the array of big cities in India. Suppose I want to delete “Nasik” from above array –

unset big_city[2]

* Example – Look below script and it’s output

root@arun:~# cat  how_to_array.sh
#!/bin/bash

#declare and assign element
declare -a myarr[0]=”Arun”
declare -a metro_array=(Mumbai Delhi Kolkata Chennai)
declare -a  my_array
my_array=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)
name_arr[1]=”Arun Bagul”

echo “————————–”
echo “myarr is – ${myarr[0]}”
echo “Metro cities in India – ${metro_array[*]}”
echo “My name is – ${my_array[0]}  ${my_array[1]}”

echo “Name – ${name_arr[1]}”
echo “Name – ${name_arr[*]}”

echo “————————–”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

apna_array=([0]=arun [1]=bagul [3]=Santhosh [4]=ravi [5]=1024)

echo “${apna_array[*]}”
echo “${apna_array[@]}”

student_record=(10 “Ravi Bhure” “1st year of BE” “SSVPS College” Dhule)

echo ${student_record}
echo ${student_record[0]}

echo “Total no of element in array is – ${#student_record[*]}”
echo “Total no of element in array is – ${#student_record[@]}”
echo “Length of element is – ${#student_record[1]}”

#how to delete array –
del_arr=(Arun Bagul)
echo “del_arr before ‘delete’ is – ${del_arr[*]}”
#delete
unset del_arr

echo “del_arr after ‘delete’ is – ${del_arr[*]}”

echo “————————–”

#how to delete one element in array –

big_city=(Bangalore Pune Nasik Hydrabad Surat)

#suppose – want to delete “Nasik” from above array –

echo “Big City before ‘delete’ is – ${big_city[*]}”

#delete
unset big_city[2]

echo “Big city after ‘delete’ is – ${big_city[*]}”

root@arun:~#

*** Run above script –

root@arun:~# ./how_to_array.sh
————————–
myarr is – Arun
Metro cities in India – Mumbai Delhi Kolkata Chennai
My name is – Arun  Bagul
Name – Arun Bagul
Name – Arun Bagul
————————–
myarr is – Arun – System Engineer!
arun bagul Santhosh ravi 1024
arun bagul Santhosh ravi 1024
10
10
Total no of element in array is – 5
Total no of element in array is – 5
Length of element is – 10
del_arr before ‘delete’ is – Arun Bagul
del_arr after ‘delete’ is –
————————–
Big City before ‘delete’ is – Bangalore Pune Nasik Hydrabad Surat
Big city after ‘delete’ is – Bangalore Pune Hydrabad Surat
root@arun:~#

Thank you,
Arun Bagul

(0) Comments    Read More   
Mar
25
Posted on 25-03-2008
Filed Under (Shell scripting) by Ravi Bhure

Few months ago I wrote a script that mail to admin for low disk space Shell script to monitor or watch the disk space but now here we improved the script in few better steps.

#!/bin/bash

# Shell script to monitor or watch the low-disk space
# It will send an email to $ADMIN, if the (free avilable) percentage
# of space is >= 90%
# ————————————————————————-
# Linux shell script to watch disk space (should work on other UNIX oses )
# SEE URL: http://www.indiangnu.org
# set admin email so that you can get email
# set alert level 90% is default

PATHS=”/”

AWK=/bin/awk

DU=”/usr/bin/du -ks”

GREP=/bin/grep

DF=”/bin/df -k”

TR=/usr/bin/tr

SED=/bin/sed

CAT=/bin/cat

MAILFILE=/tmp/mailviews$$

MAILER=/bin/mail

mailto=”ravi@indianGNU.org”

for path in $PATHS

do

DISK_AVAIL=`$DF $path | $GREP -v “Filesystem” | $AWK ‘{print $5}’|$SED ‘s/%//g’`

if [ $DISK_AVAIL -gt 90 ];then

echo “Please clean up your stuff \n\n” > $MAILFILE

$CAT $MAILFILE | $MAILER -s “Clean up stuff” $mailto

fi

done

Thanks

RAvi

(2) Comments    Read More   
Mar
14
Posted on 14-03-2008
Filed Under (Shell scripting) by Arun Bagul

Introduction-

The UNIX/Linux Administrator or programmers have difficulties to solve this kind of quiries. Many times we need to rename perticular files within the given directories for example
we have to rename all the text file to conf file ie *.txt file to *.conf file….

We can use Pattern matching with String Operators in Shell scripting —

root@arunbagul:/home/arun/templates# touch main.txt data.txt readme.txt sample.txt demo.txt
root@arunbagul:/home/arun/templates# ls
data.txt demo.txt main.txt readme.txt sample.txt
root@arunbagul:/home/arun/templates#
Suppose you have thousands of *.txt files in given directory –

It is very difficult to rename this files to some other extension like *.conf
Take the above case and we will try to rename *.txt file by using shell script

root@arunbagul:/home/arun# ls -l /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.txt
root@arunbagul:/home/arun#

** In above “/home/arun/templates/” directory there are few *.txt files and
we will rename this files as *.conf files. There is apache.conf file, which will not rename.

root@arunbagul:/home/arun# ll /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.txt
root@arunbagul:/home/arun#

Now run our command as given below..

root@arunbagul:/home/arun# ./rename.sh /home/arun/templates txt conf
Rename all the *.txt files as .conf files…
Directory is = /home/arun/templates
renaminng /home/arun/templates/main.txt to /home/arun/templates/main.conf
renaminng /home/arun/templates/apache.txt to /home/arun/templates/apache.conf
renaminng /home/arun/templates/sample.txt to /home/arun/templates/sample.conf
renaminng /home/arun/templates/demo.txt to /home/arun/templates/demo.conf
renaminng /home/arun/templates/data.txt to /home/arun/templates/data.conf
renaminng /home/arun/templates/readme.txt to /home/arun/templates/readme.conf
root@arunbagul:/home/arun#

root@arunbagul:/home/arun# ls -l /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.conf
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.conf
root@arunbagul:/home/arun#

Supose After some time we want to revert back!! ie want to rename *.conf file as *.
Run the above command and change the extension on command line as shown below

root@arunbagul:/home/arun# ./rename.sh /home/arun/templates/ conf txt
Rename all the *.conf files as .txt files…
Directory is = /home/arun/templates
renaminng /home/arun/templates/demo.conf to /home/arun/templates/demo.txt
renaminng /home/arun/templates/apache.conf to /home/arun/templates/apache.txt
renaminng /home/arun/templates/readme.conf to /home/arun/templates/readme.txt
renaminng /home/arun/templates/data.conf to /home/arun/templates/data.txt
renaminng /home/arun/templates/main.conf to /home/arun/templates/main.txt
renaminng /home/arun/templates/sample.conf to /home/arun/templates/sample.txt
root@arunbagul:/home/arun#

root@arunbagul:/home/arun# ll /home/arun/templates/
total 0
-rw-r–r– 1 root root 0 2008-03-13 16:29 apache.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 data.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 demo.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 main.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 readme.txt
-rw-r–r– 1 root root 0 2008-03-14 02:50 sample.txt
root@arunbagul:/home/arun#

Want to see the code ?

root@arunbagul:/home/arun# cat /home/arun/rename.sh
#! /bin/bash

path=$1
old_extension=$2
new_extension=$3
echo “Rename all the *.$old_extension files as .$new_extension files…”

#remove the trailing ‘/’ from end of the path
path=”${path%/}”
echo “Directory is = $path”

#Now, move/rename the files with given condition
#use ‘find’ command to find file path with given old extensions
for file_name in $(find $path -type f -name “*.${old_extension}”)
do
echo “renaminng $file_name to ${file_name%.${old_extension}}.${new_extension}”
mv -f $file_name ${file_name%.${old_extension}}.${new_extension}
done

#done
root@arunbagul:/home/arun#

NOTE :- Remember that this script is using ‘find’ command to find the files with given extensions
before renaming the files.. As ‘find’ command has no options to limit searching of files non recursively..
But still we can achieve non recursive renaming of files with same pattern matching and
string operation funcationality of shell script… please visit again!!

Thank you,

Arun Bagul

(0) Comments    Read More   
Feb
15
Posted on 15-02-2008
Filed Under (Shell scripting) by Arun Bagul

Introduction –

Some time you want your shell script to be binary executable. You can do this by using the tool called Shell script compiler (shc).
shc
creates a stripped binary executable version of the shell script specified with -f on the command line. There is no speed increase from using shc.
Its main purpose is to prevent your shell scripts from being easily modified or inspected. shc can wrap scripts written for any shell.

1] How to install shc on Ubuntu/Debian system –

root@arunbagul:~# apt-get install shc
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
shc
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 19.7kB of archives.
After unpacking 90.1kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com gutsy/universe shc 3.8.6-2 [19.7kB]
Fetched 19.7kB in 1s (14.8kB/s)
Selecting previously deselected package shc.
(Reading database … 88932 files and directories currently installed.)
Unpacking shc (from …/archives/shc_3.8.6-2_i386.deb) …
Setting up shc (3.8.6-2) …

root@arunbagul:~#

2] Sample shell script –

root@arunbagul:~# vim myscript.sh
root@arunbagul:~# cat myscript.sh
#! /bin/bash

echo -n “Welcome to shell script…”
echo -e “\n——————————–”
echo -n “Enter the number: ”
read num1

echo -n “Enter the number: ”
read num2

sum=$(expr $num1 + $num2)

echo -n “Sum to $num1 and $num2 = $sum”

echo -e “\ndone.”
root@arunbagul:~# chmod 755 myscript.sh
root@arunbagul:~#

root@arunbagul:~# ./myscript.sh
Welcome to shell script…
——————————–
Enter the number: 40
Enter the number: 50
Sum to 40 and 50 = 90
done.

root@arunbagul:~#

3] Steps to convert shell script to binary executable-

root@arunbagul:~# pwd
/root
root@arunbagul:~# ls
myscript.sh
root@arunbagul:~# shc -r -f myscript.sh

root@arunbagul:~# ls

** check in current working directory the binary file of you shell is created. You can deploy this script on any system (linux/Unix) for that you need to mention the options “-r” as shown in above command. please read shc man pages for more details.

Thank you,
Arun Bagul

(0) Comments    Read More   
Feb
06
Posted on 06-02-2008
Filed Under (Shell scripting) by Ravi Bhure

#!/bin/bash -x
# This script is useful for taking backup on Tape
# Tested on CentOS 3.8, CentOS 4.4
# For further info please login http://indianGNU.org or
# mail ravi <at> indianGNU.org

##### DECLARE AND INITIALIZE VARIABLES #####
### File with the full path names ###
DATE=`date +%m-%d-%g`
LOGDIR=/usr/local/admin/backup
BKDIR=/root/backup
TAPE1=$BKDIR/tape1
TAPE2=$BKDIR/tape2
TOC1=$LOGDIR/backup.$DATE/toc1
TOC2=$LOGDIR/backup.$DATE/toc2
ERR=$LOGDIR/backup.$DATE/errorlogs
GTAR=/bin/gtar
TAPE=/dev/st0
MT=/bin/mt
CP=/bin/cp
mkdir /usr/local/admin/backup/backup.$DATE

### Loop the the backup directory files ###
sleep 60
#$TIME > $TOC1
if [ -f “$TAPE1” ]; then
$MT -f $TAPE rewind;
$GTAR -cvf $TAPE -V “Tape1-Full.$DATE” $TAPE1
$CP $TAPE1 $TAPE2 $LOGDIR
for DIR1 in `cat $TAPE1`
do
$GTAR –exclude “simulation” -cvf $TAPE $DIR1 >> $TOC1 2>> $ERR
sleep 60;
done
$GTAR -cvf $TAPE $TOC1
$MT -f $TAPE rewoff
fi

### Loop the the backup directory files ###
sleep 180
#$TIME > $TOC2
if [ -f “$TAPE2” ]; then
$MT -f $TAPE rewind;
$GTAR -cvf $TAPE -V “Tape2-Full.$DATE” $TAPE2
for DIR2 in `cat $TAPE2`
do
$GTAR –exclude “simulation” -cvf $TAPE $DIR2 >> $TOC2 2>> $ERR
sleep 60;
done
$GTAR -cvf $TAPE $TOC2
$MT -f $TAPE rewoff
fi

#################################################
# Needed Additions to the backup script are: #
# Put table of contents on the end of the tape #
# Rotate through four sets of table of contents #
# in /usr/local/admin/backups #
#################################################

Thanks & Regards

Ravi Bhure 

(0) Comments    Read More   

www.flickr.com
arunbagul's photos More of arunbagul's photos
Get Adobe Flash player
-->