1832651 Members
2769 Online
110043 Solutions
New Discussion

Shell Scripting

 
SOLVED
Go to solution
Aggy
Frequent Advisor

Shell Scripting

# This script creates MAP files of VM and copies them to VMHOST
# Set up environment
VGEXP=/usr/sbin
MAPFILE=/home/sysadmin/vmmapfiles
MAPFILE1=/home/sysadmin/vgmaps/

#`hpvmstatus |grep HPUX|grep On|awk '{print $1}'`

# DEV1
echo "Creating DEV1 Map File"

for vg in `remsh dev1 -l root $VGEXP/vgdisplay | grep "VG N" | awk '
{print $NF}'`

do
remsh dev1 -l root $VGEXP/vgexport -v -p -m /$MAPFILE/${vg##*/}.map
${vg}

rcp dev1:$MAPFILE/vg*.map $MAPFILE1/dev1/

done

# DEV2
echo "Creating DEV2 Map File"

for vg in `remsh dev2 -l root $VGEXP/vgdisplay | grep "VG N" | awk '
{print $NF}'`

do
remsh dev2 -l root $VGEXP/vgexport -v -p -m /$MAPFILE/${vg##*/}.map
${vg}

rcp dev2:$MAPFILE/vg*.map $MAPFILE1/dev2/

done

To start with, ignore HPVMSTATUS (hashed out any way)
This script does rlogin from VMHOST server to DEV boxes (Integrity Virtual machine guests),gets the list on the Volume group name and then creates a map file using vgexport and copies the map file to VMHOST server from the dev boxes.
The script has worked fine but it is not dynamic ie. If we add or remove any DEV boxes( we have 7 at present – the script only shows 2 DEV boxes) I need to update the script so I wanted this script to be more dynamic ie.the HPVMSTATUS command above gives the output of all the active DEV boxes and I want to use this command with my script but will need some help in using this within my existing script.

3 REPLIES 3
Pete Randall
Outstanding Contributor

Re: Shell Scripting

I guess maybe you're asking how to make the script more dynamic in terms of the hosts it will query? A technique I use is to add a comment flag in the /etc/hosts file for all the unix boxes I want a particular script to run against, then use a for loop to cycle through them, like this:

HOSTLIST=`cat /etc/hosts |grep "#unixhost" |awk -F# '{ print $3 }'`
for SERVER in $HOSTLIST
do

The /etc/hosts entry looks like this:

99.99.99.250 sire sire.holstein.com #unixhost#sire


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Shell Scripting

Hi:

> HPVMSTATUS command above gives the output of all the active DEV boxes and I want to use this command with my script but will need some help in using this within my existing script.

Then your script can be reduced to something link this (untested):


#!/usr/bin/sh
VGEXP=/usr/sbin
MAPFILE=/home/sysadmin/vmmapfiles
MAPFILE1=/home/sysadmin/vgmaps/

LIST=`hpvmstatus |grep HPUX|grep On|awk '{print $1}'`

for DEV in ${LIST}
do
echo "Creating ${DEV} Map File"

for vg in `remsh ${DEV} -l root ${VGEXP}/vgdisplay | grep "VG N" | \
awk '{print $NF}'`
do
remsh ${DEV} -l root ${VGEXP}/vgexport -v -p -m /$MAPFILE/${vg##*/}.map ${vg
}
rcp ${DEV}:$MAPFILE/vg*.map ${MAPFILE}/${DEV}
done

...NOW: You can eliminate using 'grep' pipes with 'awk'. 'awk' can pattern match and extract. Re-factor your matching to be included in your 'awk' for efficiency!

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell Scripting

Hi (again):

Oops! If dropped a 'done':

#!/usr/bin/sh

VGEXP=/usr/sbin
MAPFILE=/home/sysadmin/vmmapfiles
LIST=`hpvmstatus |grep HPUX|grep On|awk '{print $1}'`

for DEV in ${LIST}
do
echo "Creating ${DEV} Map File"
for vg in `remsh ${DEV} -l root ${VGEXP}/vgdisplay \
| awk '/^VG Name/ {print $NF}'`
do
remsh ${DEV} -l root ${VGEXP}/vgexport -v -p -m /$MAPFILE/${vg##*/}.map ${vg}
rcp ${DEV}:${MAPFILE}/vg*.map ${MAPFILE}/${DEV}
done
done
exit 0

...Notice that I eliminated a 'grep|awk' pipe that captures the VG Name. Since I don't have a VM setup, I can't run 'hpvmstatus' to see its output and refactor it too.

Regards!

...JRF...