pve/create_vm.sh

96 lines
2.6 KiB
Bash
Raw Normal View History

2023-04-09 21:30:17 +08:00
#!/bin/bash
# from
# https://github.com/spiritLHLS/pve
2023-04-11 12:03:57 +08:00
# 2023.04.11
2023-04-09 21:30:17 +08:00
# cd /root
red() { echo -e "\033[31m\033[01m$@\033[0m"; }
green() { echo -e "\033[32m\033[01m$@\033[0m"; }
yellow() { echo -e "\033[33m\033[01m$@\033[0m"; }
blue() { echo -e "\033[36m\033[01m$@\033[0m"; }
reading(){ read -rp "$(green "$1")" "$2"; }
pre_check(){
home_dir=$(eval echo "~$(whoami)")
if [ "$home_dir" != "/root" ]; then
red "当前路径不是/root脚本将退出。"
exit 1
fi
if ! command -v dos2unix > /dev/null 2>&1; then
apt-get install dos2unix -y
fi
2023-04-11 12:00:05 +08:00
if [ ! -f "buildvm.sh" ]; then
curl -L https://raw.githubusercontent.com/spiritLHLS/pve/main/buildvm.sh -o buildvm.sh && chmod +x buildvm.sh
2023-04-11 12:04:41 +08:00
dos2unix buildvm.sh
2023-04-09 21:30:17 +08:00
fi
}
2023-04-11 12:18:05 +08:00
# files=$(find . -maxdepth 1 -name "vm*" | sort)
# if [ -n "$files" ]; then
# for file in $files
# do
# cat "$file" >> vmlog
# done
# fi
2023-04-11 12:00:05 +08:00
2023-04-11 16:53:34 +08:00
log_file="vmlog"
2023-04-09 21:30:17 +08:00
if [ ! -f "vmlog" ]; then
yellow "当前目录下不存在vmlog文件"
2023-04-11 12:06:38 +08:00
vm_num=202
2023-04-09 23:23:55 +08:00
web2_port=40003
port_end=50025
2023-04-09 21:30:17 +08:00
else
2023-04-11 16:53:34 +08:00
while read line; do
last_line="$line"
done < "$log_file"
last_line_array=($last_line)
vm_num="${last_line_array[0]}"
user="${last_line_array[1]}"
password="${last_line_array[2]}"
ssh_port="${last_line_array[6]}"
web1_port="${last_line_array[7]}"
web2_port="${last_line_array[8]}"
port_start="${last_line_array[9]}"
port_end="${last_line_array[10]}"
system="${last_line_array[11]}"
2023-04-09 21:30:17 +08:00
green "最后一个NAT服务器对应的信息"
2023-04-11 16:54:08 +08:00
echo "NAT服务器: $vm_num"
2023-04-09 21:30:17 +08:00
# echo "用户名: $user"
# echo "密码: $password"
2023-04-09 21:58:30 +08:00
echo "外网SSH端口: $ssh_port"
echo "外网80端口: $web1_port"
echo "外网443端口: $web2_port"
echo "外网其他端口范围: $port_start-$port_end"
2023-04-11 16:53:34 +08:00
echo "系统:$system"
2023-04-09 21:30:17 +08:00
fi
2023-04-09 22:45:39 +08:00
build_new_vms(){
2023-04-09 21:30:17 +08:00
while true; do
reading "还需要生成几个NAT服务器(输入新增几个NAT服务器)" new_nums
if [[ "$new_nums" =~ ^[1-9][0-9]*$ ]]; then
break
else
yellow "输入无效,请输入一个正整数。"
fi
done
for ((i=1; i<=$new_nums; i++)); do
2023-04-11 12:06:38 +08:00
vm_num=$(($vm_num + 1))
2023-04-09 21:30:17 +08:00
ori=$(date | md5sum)
user=${ori: 2: 4}
ori=$(date | md5sum)
password=${ori: 2: 9}
2023-04-09 22:20:13 +08:00
ssh_port=$(($web2_port + 1))
web1_port=$(($web2_port + 2))
web2_port=$(($web1_port + 3))
2023-04-09 21:30:17 +08:00
port_start=$(($port_end + 1))
port_end=$(($port_start + 25))
2023-04-11 16:53:34 +08:00
./buildvm.sh $vm_num $user $password 1 512 5 $ssh_port $web1_port $web2_port $port_start $port_end debian10
2023-04-09 21:30:17 +08:00
cat "vm$vm_num" >> vmlog
rm -rf "vm$vm_num"
done
}
2023-04-11 12:04:41 +08:00
pre_check
2023-04-09 22:45:39 +08:00
build_new_vms