我有两台服务器:Linux shell(192.168.1.11)和ESXI主机ash shell(192.168.1.12)。
所需结果:
testsuspend.sh
scp将suspendvm.sh
上传到远程ESXI服务器并在ESXI上执行。完成后,testsuspend.sh
继续并退出。testsuspend.sh
将ash脚本(suspendvm.sh
)上传到ESXi主机.12,应执行该脚本并等待其完成,然后完成testsuspend.sh
。在Linux上,我想从另一个脚本suspendvm.sh
远程执行一个脚本testsuspend.sh
,以挂起所有VM,并将ESXI主机置于维护模式。
当我在Linux上执行testsuspend.sh
时,它将停止通过SSH执行suspendvm.sh
。如果我直接在ESXI主机上执行挂起脚本sustendvm.sh
,它将起作用。
在ESXI上复制后的文件:
-rwxr-xr-x 1 root root 1260 Apr 16 07:06 suspendvm.sh
您是否对我需要做些什么才能使ssh远程脚本在ESXi主机上正常执行以及本地脚本等待远程完成有什么想法?
...所以我发现,如果我在testsuspend.sh
中运行它,它将起作用:
cat suspendVM.sh | ssh -T $user@$host
为什么这样做有效,但不能复制并执行?
本地脚本192.168.1.11,testsuspend.sh
应该远程执行suspenvm.sh
:
#!/bin/sh
user="root"
host="192.168.1.120"
scp suspendvm.sh $user@$host:/tmp
echo "copy suspend script"
ssh $user@$host 'sh -s /tmp/suspendvm.sh' &
exit
在ESXI主机.12上执行时,远程脚本192.168.1.12,suspendvm.sh
可以工作:
#!/bin/sh
RUNNING=0
VMS=`vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}'`
for VM in $VMS ; do
# echo "Probing VM with id: $VM."
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
name=`vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | awk -F'"' '{print $2}'
echo "VM with id $VM has power state $PWR (name = $name)."
if [ "$PWR" == "Powered on" ] ; then
RUNNING=1
echo "Powered on VM with id $VM and name: $name"
echo "Suspending VM with id $VM and name: $name"
vim-cmd vmsvc/power.suspend $VM > /dev/null &
fi
done
while true ; do
if [ $RUNNING -eq 0 ] ; then
echo "Gone..."
break
echo "Powered on VM with id $VM and name: $name"
echo "Suspending VM with id $VM and name: $name"
vim-cmd vmsvc/power.suspend $VM > /dev/null &
fi
done
while true ; do
if [ $RUNNING -eq 0 ] ; then
echo "Gone..."
break
fi
RUNNING=0
for VM in $VMS ; do
PWR=`vim-cmd vmsvc/power.getstate $VM | grep -v "Retrieved runtime info"`
if [ "$PWR" == "Powered on" ] ; then
name=`vim-cmd vmsvc/get.config $VM | grep -i "name =" | awk '{print $3}' | head -1 | awk -F'"' '{$
echo "Waiting for id $VM and name: $name..."
RUNNING=1
fi
done
sleep 1
esxcli system maintenanceMode set --enable true
done
应该通过esxicli
执行相同操作的远程脚本#2,当我在.12上执行但不通过.11的ssh执行时,它也可以工作:
#! /bin/ash
#test2
rm -f listid
touch listid
######## Listing the running vms################
esxcli vm process list |grep -v "^\s.*"| grep -v "^$" > list
######## If you want to reuse list for later use #######
####### put the file in a datastore, else it ##########
#######gonna be erease on next reboot ##########
####### Command look like this: ###############
## esxcli vm process list |grep -v "^\s.*"| grep -v "^$" > /vmfs/volumes/tmp/list
########## cleaning the id.s file by keeping only the id
for name in `cat list`;do
########## Dont forget to change the path if #######
########## you choose to put it in a datastore #####
####### put the file in a datastore, else it ##########
#######gonna be erease on next reboot ##########
####### Command look like this: ###############
## esxcli vm process list |grep -v "^\s.*"| grep -v "^$" > /vmfs/volumes/tmp/list
########## cleaning the id.s file by keeping only the id
for name in `cat list`;do
########## Dont forget to change the path if #######
########## you choose to put it in a datastore #####
## Example: for name in `cat /vmfs/volumes/yourDatastore/list`;do
vim-cmd vmsvc/getallvms | grep $name | grep vmx | grep -v "^$" | awk '{print $1}'>> listid
done
for id in `cat listid`;do
###### suspending vms##########
echo "Suspending the running machines"
vim-cmd vmsvc/power.suspend $id
## Example: for name in `cat /vmfs/volumes/yourDatastore/list`;do
vim-cmd vmsvc/getallvms | grep $name | grep vmx | grep -v "^$" | awk '{print $1}'>> listid
done
for id in `cat listid`;do
###### suspending vms##########
echo "Suspending the running machines"
vim-cmd vmsvc/power.suspend $id
done
echo "done."
echo "shutting down the host now.."
#/bin/poweroff
vim-cmd hostsvc/maintenance_mode_enter
#excli system maintenanceMode set --enable true
#done