pve/scripts/create_ct.sh

154 lines
4.6 KiB
Bash
Raw Normal View History

2023-04-23 13:32:16 +08:00
#!/bin/bash
# from
# https://github.com/spiritLHLS/pve
2023-04-24 08:55:48 +08:00
# 2023.04.24
2023-04-23 13:32:16 +08:00
# cd /root
2023-04-24 08:55:48 +08:00
_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"; }
2023-04-24 09:19:04 +08:00
reading(){ read -rp "$(_green "$1")" "$2"; }
2023-04-24 08:55:48 +08:00
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 utf8)
if [[ -z "$utf8_locale" ]]; then
_yellow "No UTF-8 locale found"
else
export LC_ALL="$utf8_locale"
export LANG="$utf8_locale"
_green "Locale set to $utf8_locale"
fi
2023-04-24 08:35:46 +08:00
2023-04-24 09:55:04 +08:00
check_cdn() {
local o_url=$1
for cdn_url in "${cdn_urls[@]}"; do
if curl -sL -k "$cdn_url$o_url" --max-time 6 | grep -q "success" > /dev/null 2>&1; then
export cdn_success_url="$cdn_url"
return
fi
sleep 0.5
done
export cdn_success_url=""
}
check_cdn_file() {
check_cdn "https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test"
if [ -n "$cdn_success_url" ]; then
_yellow "CDN available, using CDN"
else
_yellow "No CDN available, no use CDN"
fi
}
cdn_urls=("https://cdn.spiritlhl.workers.dev/" "https://shrill-pond-3e81.hunsh.workers.dev/" "https://ghproxy.com/" "http://104.168.128.181:7823/" "https://gh.api.99988866.xyz/")
check_cdn_file
2023-04-23 13:32:16 +08:00
pre_check(){
home_dir=$(eval echo "~$(whoami)")
if [ "$home_dir" != "/root" ]; then
2023-04-24 08:55:48 +08:00
_red "当前路径不是/root脚本将退出。"
2023-04-23 13:32:16 +08:00
exit 1
fi
if ! command -v dos2unix > /dev/null 2>&1; then
apt-get install dos2unix -y
fi
2023-04-23 13:45:38 +08:00
if [ ! -f "buildct.sh" ]; then
2023-04-24 09:55:04 +08:00
curl -L ${cdn_success_url}https://raw.githubusercontent.com/spiritLHLS/pve/main/scripts/buildct.sh -o buildct.sh && chmod +x buildct.sh
2023-04-23 13:45:38 +08:00
dos2unix buildct.sh
2023-04-23 13:32:16 +08:00
fi
}
# files=$(find . -maxdepth 1 -name "ct*" | sort)
# if [ -n "$files" ]; then
# for file in $files
# do
2023-04-23 14:01:01 +08:00
# cat "$file" >> ctlog
2023-04-23 13:32:16 +08:00
# done
# fi
check_info(){
2023-04-23 13:44:31 +08:00
log_file="ctlog"
if [ ! -f "ctlog" ]; then
2023-04-24 08:55:48 +08:00
_yellow "当前目录下不存在ctlog文件"
2023-04-23 13:32:16 +08:00
ct_num=302
web2_port=20003
port_end=30025
else
while read line; do
last_line="$line"
done < "$log_file"
last_line_array=($last_line)
ct_num="${last_line_array[0]}"
password="${last_line_array[1]}"
ssh_port="${last_line_array[2]}"
web1_port="${last_line_array[3]}"
web2_port="${last_line_array[4]}"
port_start="${last_line_array[5]}"
port_end="${last_line_array[6]}"
system="${last_line_array[7]}"
2023-04-24 08:55:48 +08:00
_green "当前最后一个NAT服务器对应的信息"
2023-04-23 14:00:37 +08:00
echo "NAT服务器: $ct_num"
2023-04-23 13:32:16 +08:00
# echo "用户名: $user"
# echo "密码: $password"
echo "外网SSH端口: $ssh_port"
echo "外网80端口: $web1_port"
echo "外网443端口: $web2_port"
echo "外网其他端口范围: $port_start-$port_end"
echo "系统:$system"
fi
}
2023-04-23 14:01:01 +08:00
build_new_cts(){
2023-04-23 13:32:16 +08:00
while true; do
reading "还需要生成几个NAT服务器(输入新增几个NAT服务器)" new_nums
if [[ "$new_nums" =~ ^[1-9][0-9]*$ ]]; then
break
else
2023-04-24 08:55:48 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-23 13:32:16 +08:00
fi
done
while true; do
reading "每个虚拟机分配几个CPU(若每个虚拟机分配1核则输入1)" cpu_nums
if [[ "$cpu_nums" =~ ^[1-9][0-9]*$ ]]; then
break
else
2023-04-24 08:55:48 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-23 13:32:16 +08:00
fi
done
while true; do
reading "每个虚拟机分配多少内存?(若每个虚拟机分配512MB内存则输入512)" memory_nums
if [[ "$memory_nums" =~ ^[1-9][0-9]*$ ]]; then
break
else
2023-04-24 08:55:48 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-23 13:32:16 +08:00
fi
done
while true; do
reading "每个虚拟机分配多少硬盘?(若每个虚拟机分配5G硬盘则输入5)" disk_nums
if [[ "$disk_nums" =~ ^[1-9][0-9]*$ ]]; then
break
else
2023-04-24 08:55:48 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-23 13:32:16 +08:00
fi
done
for ((i=1; i<=$new_nums; i++)); do
ct_num=$(($ct_num + 1))
ori=$(date | md5sum)
password=${ori: 2: 9}
ssh_port=$(($web2_port + 1))
web1_port=$(($web2_port + 2))
2023-04-23 14:15:46 +08:00
web2_port=$(($web1_port + 1))
2023-04-23 13:32:16 +08:00
port_start=$(($port_end + 1))
port_end=$(($port_start + 25))
./buildct.sh $ct_num $password $cpu_nums $memory_nums $disk_nums $ssh_port $web1_port $web2_port $port_start $port_end debian10
2023-04-23 13:44:31 +08:00
cat "ct$ct_num" >> ctlog
2023-04-23 13:32:16 +08:00
rm -rf "ct$ct_num"
sleep 60
done
}
pre_check
check_info
2023-04-23 14:01:01 +08:00
build_new_cts
2023-04-23 13:32:16 +08:00
check_info