Shell Programming Examples: Difference between revisions

From RoggeWiki
Jump to navigation Jump to search
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 200: Line 200:
  # Cleanup vgdata files older than 3 months
  # Cleanup vgdata files older than 3 months
  find /var/log/vgdata/ -mtime +91 -exec rm {} \;
  find /var/log/vgdata/ -mtime +91 -exec rm {} \;
===mci.sh===
#!/bin/ksh
# Make Config Inventory
#cmd ifconfig -a
#cmd no -a
#cmd netstat -rn
#cmd netstat -an
#cmd netstat -an|wc -l
#cmd netstat -an|grep LIST
#cmd netstat -an|grep LIST|wc -l
#cmd cat /etc/resolv.conf
#cmd cat /etc/hosts
#cmd cat /etc/netsvc.conf
#cmd lsattr -Elinet0
#cmd lsattr -Elsys0
#cmd lsattr -Elpty0
#cmd lspv
#cmd lsvg
#cmd lsvg|lsvg -i
#cmd lsvg|lsvg -il
#cmd df -k
#cmd lsfs -q
#cmd lsdev -Ccdisk
#cmd lsdev -Ccadapter
#cmd smtctl
#cmd ps -ef
#cmd ps -ef|wc -l
#cmd ps -ef|grep ora
#cmd ps -ef|grep ora|wc -l
#cmd ps -ef|grep pmon
#cmd ps -ef|grep pmon|wc -l
#cmd cat /etc/exclude.rootvg
#cmd lscfg -vp
#cmd prtconf
#cmd last -300
printline()
{
    let i=1
    while [ i -lt 80 ]
    do
        printf "*"
        let i=$i+1
    done
    printf "\n"
}
printline
grep "^#cmd" $0
grep "^#cmd" $0 |
while read f c
do
    printline
    echo Command $c $(date)
    printline
    eval $c
done
===lff.sh===
#!/bin/ksh
#
#      Looking for files
#
#      - Exit if an instance of this script is already running.
#      - Find sapevt user in /etc/lff.conf
#      - Directories to check and SAP events are defined in /etc/lff.conf
Message ()
{
        echo $(date +"%Y%m%d-%H%M%S") $1
}
#
#      Check if this instance is already running
#
Scriptname=$(basename $0)
Message "This $Scriptname script is running with PID $$"
sleep 3
ps -ef|grep $Scriptname |grep -v $$ >/tmp/${Scriptname}.ps.out
Message "$Scriptname processes found:"
Message "$(cat /tmp/${Scriptname}.ps.out)"
grep $Scriptname /tmp/${Scriptname}.ps.out
RC=$?
rm /tmp/${Scriptname}.ps.out
if [ $RC = 0 ]
then
        Message "An instance of this script is already running"
        exit 1
else
        Message "No lff.sh instance found, continue"
fi
#
#      Find sapevt user in lff.conf
#
HostName=$(uname -n)
SAPuser=$(awk '/'$HostName'/{print $3}' /etc/lff.conf)
SAPenv=$(echo $SAPuser|cut -b1-3|tr  'a-z'  'A-Z')
SAPinstance=$(basename $(ls /usr/sap/${SAPenv}/SYS/profile/${SAPenv}_DVEBMGS??_${HostName})|cut -b12-13)
Message "SAP user is $SAPuser, environment is $SAPenv and instance is $SAPinstance"
typeset -i Loopcount=0
Loopcount=$(wc -l /var/log/lff/lff.log|awk '{print $1}')
Message "Logfile found with $Loopcount messages"
#
#      Looking for files in directories perpetually
#      and move found files away to temp directory and call sapevt
while true              # Do forever
do
        for Dir in $(egrep -v "^#|filelist" /etc/lff.conf|awk '{print $1}')
        do
                ADir=$(echo $Dir|sed -e 's~/~\\/~'g)
                Sapevt=$(awk '/'$ADir'/ {print $2}' /etc/lff.conf)
                for File in $(ls ${Dir}/*DAT 2>/dev/null)
                do
                        mv $File $Dir/temp/$(basename $File)
                        if [ $? = 0 ]
                        then
                                Message "File $File moved successful, calling sapevt $Sapevt"
                                su - $SAPuser -c  sapevt "$Sapevt" -p "$(/usr/bin/basename $File)" \
                                pf=/usr/sap/${SAPenv}/SYS/profile /${SAPenv}_DVEBMGS${SAPinstance}_${HostName} &
                        else
                                Message "Move $File failed with rc=$?"
                        fi
                        (( Loopcount+=1 ))
                done
        done
        if [ $Loopcount -ge 100000 ]
        then
                Loopcount=0
                cp /var/log/lff/lff.log /var/log/lff/lff.log.old
                echo Logfile copied to lff.log.old at $(date) with rc=$? >/var/log/lff/lff.log
        else
                sleep 1
        fi
done


[[Category:Unix]]
[[Category:Unix]]

Latest revision as of 08:23, 16 April 2019

Ping all hosts in a subnet

subnet=”141.93.169
i=1
while [ $i -lt 255 ]       
do
   ping ${subnet}.${i} 2 1>/dev/null 2>&1
   if   [ $? -ne 0 ] ; then
        echo "${subnet}.${i},down"
   else echo "${subnet}.${i},up"
   fi
   i=$(expr $i + 1 )
done

sshhost

#!/bin/bash
#
# ssh-keygen -t dsa -b 8192
#
Host=`echo $0|cut -d / -f5`
echo "Logon  " `date "+%m/%d/%y %H:%M:%S"` $Host >> $HOME/logs/sshhost.log
# Use escape sequence to modify SecureCrt window title: ESC]; text CTRL-G
printf "\033];$Host\007"
Hmcs="hmc100.hmc200"
Vios="uio200.uio299.uio300.uio399"
User=""
[ ! -z $(echo "$Hmcs" | grep "$Host") ] && User="hscroot@"
[ ! -z $(echo "$Vios" | grep "$Host") ] && User="padmin@"
[ ! -z $(echo "$Host" | grep "vio")   ] && User="padmin@"
ssh $User$Host
printf "\033];`uname -n`\007"
echo "Logof  " `date "+%m/%d/%y %H:%M:%S"` $Host >> $HOME/logs/sshhost.log

copypublic.sh

#!/usr/bin/ksh
#
# Copy public key to another system. Target system is $1.
scp -pr $HOME/.ssh/id_dsa.pub $1:/tmp/
ssh $1 <<EOF
mkdir -m 700 .ssh
cat /tmp/id_dsa.pub >>.ssh/authorized_keys
chmod 600 .ssh/authorized_keys
rm /tmp/id_dsa.pub
EOF

postman

How to proces a mail

$ cat .forward
|/home/roggt/receivemail

$ cat receivemail
#!/usr/bin/ksh
echo $(date) >> aaplog
echo "0" $0  >> aaplog
echo "1" $1  >> aaplog
echo $(env)  >> aaplog
echo Extension $EXTENSION >>aaplog
cat     > aapbody
$

mksysb

#/usr/bin/ksh
#
#       Make mksysb
#
#       Arguments Hostname and Suffix
#
#       Run the mksysb
#               - Make the mksysb
#               - Check for success
#                       - Ok
#                       - Incomplete
#                       - Failed
#       Cleanup older mksysb files
#
P=/backups/mksysb/
nim -o define -t mksysb -a server=master -a source=${1} -a mk_image=yes -a mksysb_flags=ev -a location=$P${1}-${2}.mksysb ${1}-${2} >$P${1}-${2}.log 2>$P${1}-${2}.err
if [ $? = 0 ]
then
       echo "NIM backup for ${1} completed successfully"
       gzip $P${1}-${2}.mksysb &
else
       grep "0512-003 savevg may not have been able to archive some files" $P${1}-${2}.log >/dev/null 2>&1
       if [ $? = 0 ]
       then
               echo "NIM backup for ${i} completed, but not all files are archived (0512-003)"
       else
               echo "NIM backup for ${1} failed, rc=$?"
               echo "=== Error log begin ==="
               cat $P${1}-${2}.err
               echo "=== Error log end   ==="
       fi
fi
#
#       Remove older versions
#       Remove NIM resources and corresponding files
Versions=3      # Number of versions to keep on disk
let Arg=$Versions+1
echo Available NIM resources:
lsnim -t mksysb|grep ${1}|sort
for i in $(lsnim -t mksysb|grep ${1}|sort -r|tail -n +$Arg|awk '{print $1}')
do
#       nim -o remove -a rm_image=yes ${i}      # Image renamed by gzip to .gz
       nim -o remove ${i}
       if [ $? = 0 ]
       then
               echo NIM resource ${i} deleted
               echo Available files:
               ls -l $P${1}*
               echo Removing files:
               ls -l $P${i}*
               echo rm    $P${i}*
       else
               echo "Error during remove NIM resource ${i}, rc=$?"
       fi
done

mksysb.all

#/usr/bin/ksh
#
#       Maak van alle LPARs een mksysb
#       Ruim alle oude files op
#       Maak een backup van de VIO servers
#       Roep chksystems script aan
#       Collect mci van alle LPARs
#       Collect vgdata van alle LPARs
#
Date=$(date "+%Y%m%d")
Logfile=/backups/mksysb/mksysb.log
for  Host in $(cat /root/bin/lpars.list)
do
       echo $(date "+%Y-%m-%d %H:%M:%S") mksysb for $Host $Date started >>$Logfile
       /root/bin/mksysb.sh $Host $Date                                  >>$Logfile
       echo $(ls -l /backups/mksysb/${Host}-mksysb-${Date}*)            >>$Logfile
       echo $(date "+%Y-%m-%d %H:%M:%S") mksysb for $Host $Date ended   >>$Logfile
done
# Cleanup old .log and .err files
find /backups/mksysb/ -name "*.log" -mtime +90 -exec rm {} \;
find /backups/mksysb/ -name "*.err" -mtime +90 -exec rm {} \;
# Create backupios files
# Run backupios in vio and write mksysb to nim server via NFS mount 
Date=$(date "+%Y%m%d")
Logfile=/backups/backupios/backupios.log
echo $(date "+%Y-%m-%d %H:%M:%S") backupios start for the following VIOs    >>$Logfile 2>&1
cat /root/bin/vios.list                                                     >>$Logfile 2>&1
for Vios in $(cat /root/bin/vios.list)
do
       echo '============================================================' >>$Logfile 2>&1
       echo $(date "+%Y-%m-%d %H:%M:%S") backupios for $Vios $Date started >>$Logfile 2>&1
       ssh $Vios mount nl01nm001:/backups/backupios /mnt                   >>$Logfile 2>&1
       ssh $Vios /usr/ios/cli/ioscli backupios -file       \
           /mnt/$Vios-backupios-${Date} -nosvg -nomedialib -mksysb         >>/backups/backupios/$Vios-backupios-${Date}.log 2>&1
       ssh $Vios unmount /mnt                                              >>$Logfile 2>&1
       echo $(date "+%Y-%m-%d %H:%M:%S") backupios for $Vios $Date ended   >>$Logfile 2>&1
done
# Bewaar alleen de laatste drie versies van backupios files,
# gooi de rest weg
Version=3
let Arg=$Version+1
echo  $(date "+%Y-%m-%d %H:%M:%S") Delete old vio backups started           >>$Logfile 2>&1
for i in $(ls /backups/backupios|grep -v log|cut -f 1 -d '-'|sort|uniq)
do
   for v in $(ls -t /backups/backupios/${i}*|grep -v log|tail -n +$Arg)
   do
       ls -l ${v}*                                                         >>$Logfile 2>&1
       rm    ${v}*                                                         >>$Logfile 2>&1
   done
done
echo  $(date "+%Y-%m-%d %H:%M:%S") Delete old vio backups ended             >>$Logfile 2>&1
# Check systems and send mail
/root/bin/chksystems.sh $Date
# Collect machine configuration items for future reference
for Host in $(cat /root/bin/lpars.list)
do
       ssh $Host /root/bin/mci.sh >/var/log/mci/${Host}.mci.log.${Date}
done
# Cleanup mci files older than 3 years
find /var/log/mci/ -mtime +1095 -exec rm {} \;
#
#       Collect vgdata for all LPARs
#
for Host in $(cat /root/bin/lpars.list)
do
       /root/bin/mkbkpvgdata.sh $Host
done
# Cleanup vgdata files older than 3 months
find /var/log/vgdata/ -mtime +91 -exec rm {} \;

mci.sh

#!/bin/ksh
# Make Config Inventory
#cmd ifconfig -a
#cmd no -a
#cmd netstat -rn
#cmd netstat -an
#cmd netstat -an|wc -l
#cmd netstat -an|grep LIST
#cmd netstat -an|grep LIST|wc -l
#cmd cat /etc/resolv.conf
#cmd cat /etc/hosts
#cmd cat /etc/netsvc.conf
#cmd lsattr -Elinet0
#cmd lsattr -Elsys0
#cmd lsattr -Elpty0
#cmd lspv
#cmd lsvg
#cmd lsvg|lsvg -i
#cmd lsvg|lsvg -il
#cmd df -k
#cmd lsfs -q
#cmd lsdev -Ccdisk
#cmd lsdev -Ccadapter
#cmd smtctl
#cmd ps -ef
#cmd ps -ef|wc -l
#cmd ps -ef|grep ora
#cmd ps -ef|grep ora|wc -l
#cmd ps -ef|grep pmon
#cmd ps -ef|grep pmon|wc -l
#cmd cat /etc/exclude.rootvg
#cmd lscfg -vp
#cmd prtconf
#cmd last -300
printline()
{
   let i=1
   while [ i -lt 80 ]
   do
       printf "*"
       let i=$i+1
   done
   printf "\n"
}
printline
grep "^#cmd" $0
grep "^#cmd" $0 |
while read f c
do
   printline
   echo Command $c $(date)
   printline
   eval $c
done

lff.sh

#!/bin/ksh
#
#       Looking for files
#
#       - Exit if an instance of this script is already running.
#       - Find sapevt user in /etc/lff.conf
#       - Directories to check and SAP events are defined in /etc/lff.conf

Message ()
{
       echo $(date +"%Y%m%d-%H%M%S") $1
}

#
#       Check if this instance is already running
#
Scriptname=$(basename $0)
Message "This $Scriptname script is running with PID $$"
sleep 3
ps -ef|grep $Scriptname |grep -v $$ >/tmp/${Scriptname}.ps.out
Message "$Scriptname processes found:"
Message "$(cat /tmp/${Scriptname}.ps.out)"

grep $Scriptname /tmp/${Scriptname}.ps.out
RC=$?
rm /tmp/${Scriptname}.ps.out
if [ $RC = 0 ]
then
       Message "An instance of this script is already running"
       exit 1
else
       Message "No lff.sh instance found, continue"
fi

#
#       Find sapevt user in lff.conf
#
HostName=$(uname -n)
SAPuser=$(awk '/'$HostName'/{print $3}' /etc/lff.conf)
SAPenv=$(echo $SAPuser|cut -b1-3|tr  'a-z'  'A-Z')
SAPinstance=$(basename $(ls /usr/sap/${SAPenv}/SYS/profile/${SAPenv}_DVEBMGS??_${HostName})|cut -b12-13)
Message "SAP user is $SAPuser, environment is $SAPenv and instance is $SAPinstance"

typeset -i Loopcount=0
Loopcount=$(wc -l /var/log/lff/lff.log|awk '{print $1}')
Message "Logfile found with $Loopcount messages"

#
#       Looking for files in directories perpetually
#       and move found files away to temp directory and call sapevt

while true              # Do forever
do
       for Dir in $(egrep -v "^#|filelist" /etc/lff.conf|awk '{print $1}')
       do
               ADir=$(echo $Dir|sed -e 's~/~\\/~'g)
               Sapevt=$(awk '/'$ADir'/ {print $2}' /etc/lff.conf)
               for File in $(ls ${Dir}/*DAT 2>/dev/null)
               do
                       mv $File $Dir/temp/$(basename $File)
                       if [ $? = 0 ]
                       then
                               Message "File $File moved successful, calling sapevt $Sapevt"
                               su - $SAPuser -c  sapevt "$Sapevt" -p "$(/usr/bin/basename $File)" \
                               pf=/usr/sap/${SAPenv}/SYS/profile /${SAPenv}_DVEBMGS${SAPinstance}_${HostName} &
                       else
                               Message "Move $File failed with rc=$?"
                       fi
                       (( Loopcount+=1 ))
               done
       done
       if [ $Loopcount -ge 100000 ]
       then
               Loopcount=0
               cp /var/log/lff/lff.log /var/log/lff/lff.log.old
               echo Logfile copied to lff.log.old at $(date) with rc=$? >/var/log/lff/lff.log
       else
               sleep 1
       fi
done