changeport.sh

 1 #!/bin/bash
 2 #
 3 # ./changeport.sh 4443
 4 #
 5 PORT=$1
 6 if [ "$PORT" == "" ]; then
 7     echo "port empty"
 8     exit 0
 9 fi
10 if [ "$(netstat -anp  | grep ":$PORT")" != "" ]; then
11     echo "端口已经占用,尝试换一个端口"
12     exit 0
13 fi
14 
15 
16 # 备份文件
17 cp /etc/caddy/Caddyfile Caddyfile.bak
18 iptables-save > iptables-save.bak
19 
20 #
21 donmain_port=$(head -1 /etc/caddy/Caddyfile | awk '{print $1}')
22 domain=$(echo $donmain_port | cut -d ':' -f1)
23 newdonmain_port="${domain}:${PORT}"
24 
25 sed -i "s/$donmain_port/$newdonmain_port/" /etc/caddy/Caddyfile
26 
27 echo "添加 iptables"
28 iptables -A INPUT -p tcp -m tcp --dport $PORT -j ACCEPT
29 echo "重启 caddy"
30 systemctl restart caddy
31 
32 # 等待 caddy重启
33 sleep 2
34 
35 if [ "$(netstat -anp |grep caddy |grep ":$PORT")" == "" ]; then
36     ret="ERROR"
37 else
38     ret="OK"
39 fi
40 echo "检查 caddy, $ret"
41 if [ "$ret" == "ERROR" ]; then
42     echo "更改端口失败,尝试恢复 Caddyfile.bak, iptables-save.bak"
43 else
44     echo "更改端口成功, PORT:$PORT,尝试修改代理客户端端口号"
45 fi

 

posted @ 2022-10-11 13:50  maojun1998  阅读(21)  评论(0编辑  收藏  举报