Bean's Studio.

Centos下防火墙+ss多端口脚本

字数统计: 724阅读时长: 4 min
2019/02/10

目的

开启SS之后,添加多端口再去打开防火墙比较麻烦,做了一个一键脚本。

代码

简易,不带Shadowsocks安装检查

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
#!/bin/sh
method="aes-256-cfb"
mup="6001"
log="./add.log"

echo -e -n "add: {\"server_port\": $1, \"password\":\"$2\"}" > /dev/udp/127.0.0.1/$mup

tcp=$(firewall-cmd --zone=public --add-port=$1/tcp --permanent)
echo "TCP端口 $1 开放操作: $tcp"

udp=$(firewall-cmd --zone=public --add-port=$1/udp --permanent)
echo "udp端口 $1 开放操作: $udp"

rel=$(firewall-cmd --reload)
echo "防火墙重启操作: $rel"

echo "IP:$(curl ipinfo.io/ip) 端口:$1 密码:$2"

if [ ! -f "$log" ]; then

echo -e -n "\"$1\":\"$2\"" >> add.log

else

echo -e -n ",\n\"$1\":\"$2\"" >> add.log

fi

# base64链接
link=$( base64 <<< "$method:$2@$(get_ip):$1" )

echo "ss://$link"

其中获得IP的函数,依赖curl,需要能够访问两个网站:
‘ipinfo.io’

‘ipv4.icanhazip.com’

1
2
3
4
get_ip(){
IP = $(curl ipinfo.io/ip)
echo ${IP}
}

1
2
3
4
get_ip(){
IP = $(curl ipv4.icanhazip.com)
echo ${IP}
}

通过查看本地网络连接信息的方式也可,用正则表达式筛选

1
2
3
4
get_ip(){
local IP=$( ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1 )
echo ${IP}
}

带安装检查

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
#!/bin/sh

# 一些常量
method="aes-256-cfb"
mup="6001"
log="./add.log"

install_sth(){
source /etc/os-release
case $ID in
debian|ubuntu|devuan)
apt-get install -y $1
;;
centos|fedora|rhel)
yum install -y $1
;;
*)
exit 1
;;
esac
}

get_ip(){
local IP=$( ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1 )
[ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com )
[ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipinfo.io/ip )
echo ${IP}
}

add_port(){
echo -e -n "add: {\"server_port\": $1, \"password\":\"$2\"}" > /dev/udp/127.0.0.1/$mup
tcp=$(firewall-cmd --zone=public --add-port=$1/tcp --permanent)
echo "TCP端口 $1 开放操作: $tcp"

udp=$(firewall-cmd --zone=public --add-port=$1/udp --permanent)
echo "udp端口 $1 开放操作: $udp"

rel=$(firewall-cmd --reload)
echo "防火墙重启操作: $rel"

echo "IP:$(get_ip) 端口:$1 密码:$2"

# base64链接
link=$( base64 <<< "$method:$2@$(get_ip):$1" )

echo "ss://$link"
}

start_check(){
process=`ps aux | grep ssserver | grep -v grep`;
if [ "$process" == "" ]; then
echo "Shaodwsock is not running"
ssserver -p 443 -k bean -m aes-256-cfb --manager-address 127.0.0.1:6001 -d start
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=443/udp --permanent
firewall-cmd --reload
if [ -f "$log" ]; then
for line in `cat $log`
do
line=$(echo $line | tr -d "\"")
por=${line%:*}
pwd=${line#*:}
add_port $por $pwd
echo "port:$por,pwd:$pwd"
done
fi
else
echo "Shaodwsock is running"
fi
}


if ! [ -x "$(command -v pip)" ]; then
echo 'Python-pip is not installed.'
install_sth epel-release >&1
install_sth python-pip >&1
fi

if ! [ -x "$(command -v ssserver)" ]; then
echo 'Shadowsocks is not installed.'
pip install shadowsocks
fi

start_check

if [ -n "$1" ]; then
echo "Add new $2@$1"
add_port $1 $2
echo -e "\"$1\":\"$2\"" >> add.log
else
echo "No port to Add"

fi

一键安装

wget --no-check-certificate https://raw.githubusercontent.com/BeanYa/OneScript/master/plane.sh && bash plane.sh
CATALOG
  1. 1. 目的
    1. 1.1. 代码
      1. 1.1.1. 简易,不带Shadowsocks安装检查
      2. 1.1.2. 带安装检查
      3. 1.1.3. 一键安装