Shell Programming Examples: Difference between revisions

From RoggeWiki
Jump to navigation Jump to search
No edit summary
Line 60: Line 60:
  $
  $


===mksysb===
===mksysb===
Create a mksysb and remove older versions. The script runs on the NIM master and an NIM machine resource for the host to be backed up is needed.
The mksysb is pulled from the NIM client.
 
  #/usr/bin/ksh
  #/usr/bin/ksh
  #
  #
  # Make mksysb
  #       Make mksysb
  #
  #
  # Arguments Hostname and Suffix
  #       Arguments Hostname and Suffix
# Pick YYMMDD as suffix.
  #
  #
  # Run the mksysb and cleanup older versions
  #       Run the mksysb
#              - Make the mksysb
#              - Check for success
#                      - Ok
#                      - Incomplete
#                      - Failed
#      Cleanup older mksysb files
  #
  #
  P=/backups/mksysb/
  P=/backups/mksysb/
Line 77: Line 79:
  if [ $? = 0 ]
  if [ $? = 0 ]
  then
  then
        echo NIM backup for ${1} completed successfully
        echo "NIM backup for ${1} completed successfully"
        gzip $P${1}-${2}.mksysb &
        gzip $P${1}-${2}.mksysb &
  else
  else
        echo NIM backup for ${1} failed, rc=$?
        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
  fi
  #
  #
Line 92: Line 103:
  do
  do
  #      nim -o remove -a rm_image=yes ${i}      # Image renamed by gzip to .gz
  #      nim -o remove -a rm_image=yes ${i}      # Image renamed by gzip to .gz
        nim -o remove ${i}
        nim -o remove ${i}
        if [ $? = 0 ]
        if [ $? = 0 ]
        then
        then
                echo NIM resource ${i} deleted
                echo NIM resource ${i} deleted
                echo Available files:
                echo Available files:
                ls -l $P${1}*
                ls -l $P${1}*
                echo Removing files:
                echo Removing files:
                ls -l $P${i}*
                ls -l $P${i}*
                echo rm    $P${i}*
                echo rm    $P${i}*
        else
        else
                echo "Error during remove NIM resource ${i}, rc=$?"
                echo "Error during remove NIM resource ${i}, rc=$?"
        fi
        fi
  done
  done


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

Revision as of 12:30, 29 September 2015

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