-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycentos
executable file
·148 lines (132 loc) · 3.32 KB
/
mycentos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# Program:
# This program shows
# History:
# 2017-05-19 zmqc First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 支持 vbox
################################
# 变量定义区
################################
vmsUUID="f11cedbd-cdc8-4b23-822e-b7eaec14d693" # vbox
vms=$(VBoxManage list runningvms)
existvms=$(VBoxManage list runningvms|sed 's/.*{//g'|sed 's/}//g')
statvms=$(VBoxManage showvminfo centos|grep -i state|cut -d':' -f2|awk '{print $1}')
################################
# 函数定义区
################################
# 开机
function boot() {
if [ "${existvms}" != "${vmsUUID}" ]; then
echo "boot..."
VBoxManage startvm centos -type headless &> /dev/null
while [ "${existvms}" != "${vmsUUID}" ]
do
sleep 1
existvms=$(VBoxManage list runningvms|sed 's/.*{//g'|sed 's/}//g')
done
fi
}
# 虚拟机信息
function info() {
echo "vms info: "${vms:-"没有虚拟机"}
}
# 远程链接
function connect() {
echo "connect..."
ssh 192.168.56.4
}
# 测试是否可以连接
function testip() {
if [ "${existvms}" != "${vmsUUID}" ]; then
echo "虚拟机未启动"
exit 0
fi
result="1"
while [ "${result}" != "0" ]
do
ping -c 1 -w 1 192.168.56.4 &> /dev/null && result=0 || result=1
sleep 0.3
done
success="虚拟机已经启动"
}
# 帮助信息
function help() {
echo -e "\
help ( 查看 mycentos 的参数介绍 )\n\
ls ( 查看有哪些虚拟机 ) \n\
boot ( 启动虚拟机 )\n\
ssh ( 用ssh进行远程连接 )\n\
status ( 查看虚拟机是否启动 )\n\
ping ( ping 一下虚拟机的网络 )\n\
pause ( 暂停虚拟机 )\n\
resume ( 恢复暂停虚拟机 )\n\
ss ( savestate 保存状态然后关闭 )\n\
shutdown ( 关机 )\n\
poweroff ( 强制关机 )"
}
################################
# 主程序区
################################
# 不带任何参数则直接启动
if [ $# == 0 ]; then
boot
# 虚拟机已经启动了,但是被暂停了
if [ "${statvms}" == "paused" ]; then
VBoxManage controlvm centos resume
fi
testip
# 网络可以连通后,ssh 服务还没有完全启动
if [ "${statvms}" == "powered" ]; then
sleep 1
fi
connect
exit
elif [ $# != 1 ]; then
echo " 本程序不接受参数或者只接收一个参数, 参数列表如下:"
help
exit
fi
# 针对不同参数进行处理
case $1 in
"help")
help
;;
"ls")
info
;;
"boot")
boot
;;
"ssh")
connect
;;
"ping")
ping -c 1 -w 1 192.168.56.4
;;
"status")
testip
echo $success
;;
"pause")
VBoxManage controlvm centos pause
;;
"resume")
VBoxManage controlvm centos resume
;;
"ss")
VBoxManage controlvm centos savestate
;;
"shutdown")
VBoxManage controlvm centos acpipowerbutton
;;
"poweroff")
VBoxManage controlvm centos poweroff
;;
*)
echo " 参数错误"
echo " 本程序不接受参数或者只接收一个参数, 参数列表如下:"
help
;;
esac