Shell Script
Aus MeinWiki
Inhaltsverzeichnis
Funktionen
Allgemeine Funktionen
- Get script path
# get script path
GetScriptPath()
{
echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
}
- Write logfile
# write logfile
# $1 Description
# $2 Logfiledestination
WriteLog ()
{
echo $(date "+%y%m%d-%H%M%S")' '$1 >> $2
}
- Change from upper to lower char
# change from upper to lower char
# $1 Variable to change
# $2 Logfiledestionation
LowerChar ()
{
WriteLog 'LowerChar '$1 $2
local myResult=`echo $1 | tr 'A-Z' 'a-z'`
echo "$myResult"
}
- Read file to parameter
# read file to parameter
# $1 Filepath
# $2 Logfiledestination
ReadFile()
{
local myResult=
WriteLog 'ReadFile '$1 $2
if [ -e $1 ]; then
myResult=$(cat $1 | grep -v '#' |\
while read myResult;
do
echo $myResult
done
)
else
WriteLog 'ReadFile Error File '$1 $2
exit 1
fi
echo "$myResult"
}
- Check Prozess and wait
# Check Prozess and wait
# $1 wait prozzes
# $2 waiting time
# $3 Logfile
CheckWaitProzessSh()
{
WriteLog 'CheckWaitProzess '$1 $3
local myProzess=(`ps -ef | grep 'sh' | grep $1 | awk '{print $7}'`)
# echo ${#myProzess[*]}
while [[ ${#myProzess[*]} -gt 1 ]]
do
sleep $2
myProzess=(`ps -ef | grep 'sh' | grep $1 | awk '{print $7}'`)
echo -ne '.'
done
echo
}
- Check wait system live
# Check wait system live
# $1 Systemname / IP-Adresse
# $2 Testcyclus
# $3 waiting time
# $4 Logfile
CheckWaitSystem()
{
WriteLog 'CheckWaitSystem '$1 $4
local systemAlive=
local i=0
while $2 -gt $i
do
sleep $3
systemAlive=(`ping $1 -c 1 |grep received | awk '{print $4}'`)
if [ $systemAlive -ne 1 ]; then
i=$2
fi
i=$[$i+1];
echo -ne '.'
done
echo
}
- Check system alive
# Check system alive
# $1 Systemname / IP-Adresse
# $2 Logfile
CheckAliveSystem()
{
WriteLog 'CheckAliveSystem '$1 $2
local systemAlive=
systemAlive=(`ping $1 -c 1 |grep received | awk '{print $4}'`)
if [ $systemAlive -eq 1 ]; then
echo true
else
echo false
fi
}
VMware Funktionen
- Get all configfile from virtual machine
# get all configfile from virtual machine
# $1 VMwarehost
# $2 Logfiledestination
GetVmConfig()
{
WriteLog 'GetVmConfig '$1 $2
local myResult=
myResult=`vmware-cmd -H $1 -U root -l | grep vmx`
WriteLog 'GetVmConfig Count Virtual Machine '$1' '${#myResult[*]} $2
echo "$myResult"
}
- Check status and create new array by aktive vm
# check status and create new array by aktive vm
# $1 VMwarehost
# $2 Logfiledestination
# $3 Configparameter (array)
CheckVmStat()
{
WriteLog 'CheckVmStat '$1 $2
local myResult=
local vmwareConfig=("${@}")
unset vmwareConfig[1]
unset vmwareConfig[0]
for forLogFile in "${vmwareConfig[@]}"
do
WriteLog 'CheckVmStat '$1' '$forLogFile $2
done
WriteLog 'GetVmStat Count Virtual Machine '$1' '${#vmwareConfig[*]} $2
for vmxParameter in "${vmwareConfig[@]}"
do
varReturn=`vmware-cmd -H $1 -U root $vmxParameter getstate | awk '{print $3}'`
if [ "$varReturn" = 'on' ]; then
myResult=$myResult$vmxParameter' '
fi
done
echo "$myResult"
}
- Stop virtual machine (soft)
# stop virtual machine soft
# $1 VMwarehost
# $2 Logfiledestination
# $3 Configparameter (array)
StopVmSoft()
{
WriteLog 'StopVmSoft '$1 $2
local vmwareConfig=("${@}")
unset vmwareConfig[1]
unset vmwareConfig[0]
for vmxParameter in "${vmwareConfig[@]}"
do
WriteLog 'StopVmSoft '$1' '$vmxParameter $2
vmware-cmd -H $1 -U root $vmxParameter stop soft
done
}
- Stop virtual machine (Hard)
# stop virtual machine hard
# $1 VMwarehost
# $2 Logfiledestination
# $3 Configparameter (array)
StopVmHard()
{
WriteLog 'StoppVmHard '$1 $2
local vmwareConfig=("${@}")
unset vmwareConfig[1]
unset vmwareConfig[0]
for vmxParameter in "${vmwareConfig[@]}"
do
WriteLog 'StopVmHard '$1' '$vmxParameter $2
vmware-cmd -H $1 -U root $vmxParameter stop hard
done
}
Lösungen
Check SSH
$(ssh -o BatchMode=yes -o ConnectTimeout=5 root@hostt echo ok 2>&1)
Check Alive
$(ping -c 2 hostname | grep ttl | wc -l)
if [ $(ping -c 2 hostname | grep ttl | wc -l) -eg 0 ]; then
echo false
else
echo true
fi