Shell Programming Examples

From RoggeWiki
Revision as of 12:30, 29 September 2015 by Tom (talk | contribs) (→‎mksysb)
Jump to navigation Jump to search

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