Friday, March 28, 2014

Linux Shell Scripting !!

How to pause or hold a shell script at run time

You can put the script on hold with "kill -19/-STOP" and you can start it from the same point with "kill -18/-CONT". You can control the flow with a while loop which will keep on reading a character from key board.

Here I am using simple script to print 1 to 50 numbebs, which I placing in background with "&" and get its process ID "PID" with "$!". Once we have PID of the script, we are good.

[root@lemonpot ~]# cat /tmp/print-numbers.sh

#!/bin/bash

for i in {1..50}   #  for loop runs from 1 to 50 numbers
do
echo "              $i "    
sleep 1
done
[root@lemonpot ~]#



[root@lemonpot ~]# cat /tmp/vix.sh

#!/bin/bash
/tmp/print-numbers.sh &   # you put the process in background
pid=$!                              # here you get the pid of the process for script above print-numbers.sh
echo $pid
while true
do
read key

if [ "$key" == "b" ]; then           # if you press "b" from keyboard, print-numbers.sh will be stopped
kill -19 $pid                              
fi


if [ "$key" == "a" ]; then         # if you press "a" from keyboard, print-numbers.sh
kill -18 $pid                          # will be started from the same point, it was stopped from.
fi

if [ "$key" == "c" ]; then      # if you press "c" from keyboard, print-numbers.sh
kill -9 $pid                          # will be kill and while loop will be broken with exit 0
exit 0
fi

done
[root@lemonpot ~]#


It is show below where script was put on hold with key "b" return & then resumed with key "a" return and finally stopped with key "c".


Checking a list of servers for ping:

 

This script is helpful when you a list of servers rebooted under a maintenance windows and you quickly want to know which one is still not pinging. so that you can check console and start it up.

[root@lemonpot ~]# cat ./server-check-ping.sh
#!/bin/bash

for host in $( cat host-list.txt )
do
/bin/ping -q -c2 $host > /dev/null
if [ $? -eq 0 ]; then
echo "Host $host is up"
sleep 1
else
echo "Host $host is down"
fi
done

[root@lemonpot ~]#

 

Progress / Status function:


This function can be called up multiple times within a script to check the progress / status.

status() {

for i in {0...5}
do
echo -ne " Status : $HOSTNAME  # \\ .         \r"
usleep 50000
echo -ne " Status : $HOSTNAME  # | ..        \r"
usleep 50000
echo -ne " Status : $HOSTNAME  # - ...       \r"
usleep 50000
echo -ne " Status : $HOSTNAME  # / ....      \r"
usleep 50000
echo -ne " Status : $HOSTNAME  # \\ .....     \r"
usleep 50000
echo -ne " Status : $HOSTNAME  # | ......    \r"
usleep 50000
echo -ne " Status : $HOSTNAME  # - .......   \r"
usleep 50000
echo -ne " Status : $HOSTNAME  # / ........  \r"
usleep 50000
done

 

 

Checking autofs mounts for all NFS client in your Environment :

This script can be useful for a Sysadmin who has a lot NFS clients in his environment, basically you can run this script to check all of the autofs mount points, if in case any of the mount point is not mounted you get a script_name.out file and later can be fixed. There could a stale error, simply restart autofs service if NFS server is fine after any maintenance activity or rebooted etc.

Explanation : In this script I have simply took list of hosts and ssh them from a gateway server. and then list all auto.direct mount points from /proc/mounts. If one is able to list them its working, else its not working. you can test it putting a wrong mount point in /etc/auto.direct file and service autofs reload.

[root@lemonpot ~]# cat nfs-client-mount-check.sh

#!/bin/bash

for host in $( cat /tmp/host-list.txt )
do

echo "$host NFS Client __"

    for mount in ` ssh -q $host " cat /proc/mounts " | grep auto.direct | cut -d' ' -f2 `

    do
    echo "                     |__Autofs_Mount__$mount"
        if  ssh -q $host ls -l $mount 2> /dev/null > /dev/null ; then
        echo "                     |                     |_________________GOOD"
        else
        echo "                     |                     |_________________Not mounted, please CHECK <==="
        echo "$host : Mount $mount " >> $0.out    # This will make your $script_name.out file
       fi                                                                 # to check later for errors
    usleep 2500

    done

done
[root@lemonpot ~]#


 

A Script for DISK Monitoring:

[root@lemonpot ~]#  
root@lemonpot:~# cat ./disk-monitoring.sh
#!/bin/bash
IFS='
'
MYNAME=$0
df -hP | grep -v Filesystem > /tmp/$MYNAME.out

for DISK in $( cat /tmp/$MYNAME.out )
do
DISK_USAGE=` echo $DISK | awk '{ print $5 }' | awk -F% '{ print $1 }' `
DISK_INFO=` echo $DISK | awk '{ print "File system " $1 " " USAGE  $5 }' `
if [ $DISK_USAGE -ge 70 ]; then   # I kept threshold as 70% <===
echo $DISK_INFO >> /tmp/$MYNAME.mail
fi
done

cat /tmp/$MYNAME.mail | mail -s "DISK INFO for $HOSTNAME" kanvi@lemonpot
rm -rf /tmp/$MYNAME.out
rm -rf /tmp/$MYNAME.mail

root@lemonpot:~#

  How to place a shell script in crontab (scheduler):

Five fields of crontab
minute (m), hour (h), day of month (dom), month (mon), and day of week (dow)

root@lemonpot:~# crontab -e
#place your entry as given below
*/10  *  *  *   * /root/disk-monitoring.sh
 ^  ^ ^ ^ ^ 
 |  | | | | 
(m) (h)  |   |   | 
         dom |   | 
              mon| 
                  dow


 

Rescan SCSI Bus on Linux

Manual:

echo "- - -" > /sys/class/scsi_host/hostX/scan   # X could be number of port on your HBA card

Automatic by Script:

[root@lemonpot ~]# for i in $( ls /sys/class/scsi_host/host*/scan  ); do echo $i; echo "- - -" > $i; done
/sys/class/scsi_host/host0/scan
/sys/class/scsi_host/host1/scan
/sys/class/scsi_host/host2/scan
/sys/class/scsi_host/host3/scan
[root@lemonpot ~]#

Delete a disk from device tree

# echo "1" > /sys/block/sdXX/device/delete

Expect script, prompt for password once, run the command on list of hosts. 

x1vcmk_bs.sh
x1vcmk_mn.sh
[root@lemonpot scripts]# cat x1vcmk_bs.sh
#!/usr/bin/expect -f
set timeout 60
set host [lindex $argv 0];
set login [lindex $argv 1];
set pw [lindex $argv 2];
spawn -noecho ssh -q -o StrictHostKeyChecking=no $login@$host
expect "*?assword:*"
send -- "$pw\r"
expect "*$*"
send -- "sudo su -\r"
expect "*?assword:*"
send -- "$pw\r"
expect "*#*"
send -- "cat /var/log/messages* | grep \"Abort command issued\" | tail -5 \r" # Please modify the commands you want to run
#send -- "\r"
#send -- "\r"
send -- "exit\r"
send -- "exit\r"
send -- "exit\r"
expect eof
[root@lemonpot scripts]# cat x1vcmk_mn.sh
#!/bin/bash
echo -n "Your Login : "
read login
echo -n "Your Password : "
read -s pw
echo ""
if [ -f "$1" ]; then
for i in $( cat $1 )
do
echo " HOST $i ============================= Start "
echo ""
echo ""
./x1vcmk_bs.sh $i $login $pw
echo ""
echo ""
echo " Host $i ============================= End "
echo ""
echo ""
echo ""
done
else
echo "Please provide a host list as an argument"
fi
[root@lemonpot scripts]#

How to rebuild initial ramdisk in RHEL 3/4/5

#mkinitrd -f -v /boot/initrd-$(uname -r).img $(uname -r)
       -f, --force
              overwrite existing initramfs file.
       -v, --verbose
              verbose output during the build process

How to rebuild initial ramdisk in RHEL 6/7

#dracut -f -v /boot/initramfs-$(uname -r).img $(uname -r)
       -f, --force
              overwrite existing initramfs file.
       -v, --verbose
              verbose output during the build process

How to get WWN number in Linux (RHEL5)

Using systool:
# which systool
/usr/bin/systool
# rpm -qf /usr/bin/systool
sysfsutils-2.1.0-1.el5
# systool -c fc_host -v | grep port_name
    port_name           = "0x100000000xx00001"
    port_name           = "0x100000000yy00001"

# cat /sys/class/fc_host/host0/port_name
0x100000000xx00001
# cat /sys/class/fc_host/host*/port_name
0x100000000xx00001
0x100000000yy00001
#

Storage WWN number
using systool:
# systool -c fc_remote_ports -v | grep port_name
    port_name           = "0x500a065487078x18"
    port_name           = "0x500a065687078x18"
    port_name           = "0x500a065497078x18"
    port_name           = "0x500a065697078x18"
#
Manual:
#cat /sys/class/fc_host/host0/device/rport-0\:0-0/fc_remote_ports\:rport-0\:0-0/port_name
#cat /sys/class/fc_host/host*/device/rport-*\:*-*/fc_remote_ports\:rport-*\:*-*/port_name
# cat /sys/class/fc_host/host*/device/rport-*\:*-*/fc_remote_ports\:rport-*\:*-*/port_name
0x500a065487078x18
0x500a065497078x18
0x500a065687078x18
0x500a065697078x18
#


BASH parallel processing

#!/bin/bash

set -m # Enable Job Control
for i in `seq 100`; do
ssh -o StrictHostKeyChecking=no 172.28.2.$i "uname -n ; du -sh /var" &
done

 # start 100 jobs in parallel



Python Section:

Grub2 fix append parameters and compile using python:

Keep a file for parameters
$ cat grub_params_hana
selinux=0
console=ttyS1
ipv6.disable=1
processor.max_cstate=1
intel_idle.max_cstate=1
transparent_hugepage=never

#! /usr/bin/pythonimport sys
import os
import os.path
import re
import string

if os.path.isfile("/etc/default/grub"):
    print("grub file exists, proceeding")
else:
    print("grub file doesn't exist, exiting.. code 1")
    exit(1)

f=open("/tmp/grub_params_hana","r")

g=open("/etc/default/grub","r+")


fn=f.readlines()
gn=g.readlines()

g.close()

keyw=str("")
for j in range(len(gn)):
    if "GRUB_CMDLINE_LINUX" in gn[j]:
        gnm=gn[j].rstrip('"\n')
        old=gnm #+ '"'        gnm=gnm.lstrip('GRUB_CMDLINE_LINUX="')
        keyw="found"        break
if not keyw:
    print("Didn't find the GRUB_CMDLINE_LINUX, exiting...")
    exit(1)


tmp=str("")
for i in range(len(fn)):
    d=fn[i].rstrip('"\n')
    x =  fn[i].split("=")
    for k in x[:1]:
        print(k)
        if k not in gnm:
            print(d, "not in there, so adding in")
            tmp=tmp + " " + d
            print(tmp)
        if d not in gnm and k in gnm:
            gnm=re.sub(k+'=[0-9]*[a-z]*', d, gnm)
            print(gnm)


#if not tmp:#    print("file is good, exiting")#    exit(0)gnm='GRUB_CMDLINE_LINUX="' + gnm + tmp
new=gnm



print(old)
print(new)

if old == new:
    print("Nothing changed, exiting..")
    exit(0)
else:
    print("compling grub 2, post change in /etc/default/grub")
    os.system('grub2-mkconfig -o /boot/grub2/grub.cfg')

s = open("/etc/default/grub").read()
s = s.replace(old, new)
g = open("/etc/default/grub", 'w')
g.write(s)
g.close()

f.close()






kanvi@lemonpot:~/python$ cat nn.py
#!/usr/bin/python -tt

import sys
import os
import re

def check(fname):
 if os.path.isfile(fname):
  print fname + " is a file"
 elif os.path.isdir(fname):
  print fname + " is a directory"
 else:
  print fname + " doesn't exists"

check(sys.argv[1])
kanvi@lemonpot:~/python$

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

kanvi@lemonpot:~/python$ cat nn.py
#!/usr/bin/python -tt

import sys
import os
import re

user_input = raw_input("Please enter filename : ")

print "You entered filename:" + user_input

def check(fname):
 if os.path.isfile(fname):
  print fname + " is a file"
 elif os.path.isdir(fname):
  print fname + " is a directory"
 else:
  print fname + " doesn't exists"

check(user_input)
#check(sys.argv[1])
kanvi@lemonpot:~/python$ 

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

kanvi@lemonpot:~/python$ cat nn.py
#!/usr/bin/python -tt

import sys
import os
import re

user_input = raw_input("Please enter filename : ")

print "You entered filename:" + user_input

def check(fname):
 if os.path.isfile(fname):
  print fname + " is a file"
 elif os.path.isdir(fname):
  print fname + " is a directory"
 else:
  print fname + " doesn't exists"

check(user_input)
#check(sys.argv[1])
kanvi@lemonpot:~/python$ 


$ cat nn.py
#!/usr/bin/python -tt
import sys
import os
import re
user_input = raw_input("Please enter filename : ")
p = int(input("place : "))
print "You entered filename:" + user_input
def check(fname):
 if os.path.isfile(fname):
  print fname + " is a file"
  fo = open(fname, "ro")
  print fo.readlines()[p]
  #print len(fo.readlines())
  #for ln in fo.readlines():
  # print "hi " + ln
  fo.close()
  for line in open(fname, "ro").readlines():
   print "Line : " + line
 elif os.path.isdir(fname):
  print fname + " is a directory"
 else:
  print fname + " doesn't exists"
check(user_input)
$


]$ cat nn.py
#!/usr/bin/python -tt
import sys
import os
import re
user_input = raw_input("Please enter filename : ")
p = int(input("place : "))
print "You entered filename:" + user_input
def check(fname):
 if os.path.isfile(fname):
  print fname + " is a file"
  fo = open(fname, "ro")
  print fo.readlines()[p]
  #print len(fo.readlines())
  #for ln in fo.readlines():
  # print "hi " + ln
  fo.close()
  for line in open(fname, "ro").readlines():
   print "Line : " + line
   response = os.system("ping -c 1 " + line)
   if response == 0:
    print line + 'is up!'
   else:
    print line + 'is down'
 elif os.path.isdir(fname):
  print fname + " is a directory"
 else:
  print fname + " doesn't exists"
check(user_input)
$