需求分析:
1.之前手动去生成frp的配置文件,需要改的地方多,繁琐容易出错
2.通过shell脚本实现自动化批量生成所需要的配置文件
操作步骤:
1.在相应的relay01与02节点分别安装frp (对应节点服务器)
2.将此脚本copy到自己的家目录下 (堡垒机)
3.执行脚本

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
#Author:teng

read -p "请输入英文节点名:>>>" node
cd
home_path=`pwd`
kscinfo_path=$home_path/kscinfo/
relay_out_ip=`$kscinfo_path/./kscinfo.py -l -e node=$node |grep relay |awk -F "|" '{ip[x++]=$6};END{for(i=0;i<NR;i++)print ip[i] }'` #获取relay IP
cache_out_ip=`$kscinfo_path/./kscinfo.py -l -e node=$node |grep cache |awk -F "|" '{ip[x++]=$6};END{for(i=0;i<NR;i++)print ip[i] }'` #获取cache IP
cache_ilo_ip=`$kscinfo_path/./kscinfo.py -l -e node=$node |grep cache |awk -F "|" '{ip[x++]=$7};END{for(i=0;i<NR;i++)print ip[i] }'`
cache_in_ip=`$kscinfo_path/./kscinfo.py -l -e node=$node |grep cache |awk -F "|" '{ip[x++]=$8};END{for(i=0;i<NR;i++)print ip[i] }'`
cache_num=`$kscinfo_path/./kscinfo.py -l -e node=$node |grep cache |wc -l`
gw=`$kscinfo_path/./kscinfo.py -l -e node=$node |grep relay01 |awk -F "|" '{print$8}'|cut -d . -f 1,2,3`
frp_server_path=/root/frp_0.33.0_linux_amd64 #frp安装包位置
back_file_dir=$home_path/frp_test #frp文件生成位置
frp_package=$home_path/package/frp_0.33.0_linux_amd64.tar.gz
password='k*******cdn'
source /etc/init.d/functions

[ -d $back_file_dir ] || mkdir -p $back_file_dir #判断目录是否存在,不存在就创建

#relay IP
for i in $relay_out_ip
do
echo $i>>$back_file_dir/relay_out_ip.txt
done
array2=($(cat $back_file_dir/relay_out_ip.txt)) #将获取到的值存入到数组中
echo "" > $back_file_dir/relay_out_ip.txt #清理

#out_ip数组
for i in $cache_out_ip
do
echo $i>>$back_file_dir/out_ip.txt
done
array=($(cat $back_file_dir/out_ip.txt)) #将获取到的值存入到数组中
echo "" > $back_file_dir/out_ip.txt

#ip_ip数组
for i in $cache_in_ip
do
echo $i>>$back_file_dir/in_ip.txt
done
array1=($(cat $back_file_dir/in_ip.txt)) #将获取到的值存入到数组中
echo "" > $back_file_dir/in_ip.txt



while true
do
#格式化输出以下菜单
cat <<EOF
-----------------------------菜单----------------------------------
- 1.生成frp服务端frps.ini文件 -
- 2.生成frp客户端frpc.ini文件 -
- 3.安装frp -
- 4.发送frp文件到对应服务器 -
- 5.启动frp服务并生成/etc/rc.local文件 -
- 6.退出 -
------------------------------------------------------------------
EOF
#接受用户输入的一个值
read -p "请输入你要执行的操作:>>>" num
#服务端frp配置
Server(){

echo -e "[common] \nbind_addr = ${array2[0]} \nbind_port = 7000 \n# auth \nauthentication_method = token \ntoken = kingsoft"> $back_file_dir/frps.ini
#echo `sudo chown 1000:dorguard $home_path/test_sh/frps.ini `

#sudo chown root:root $home_path/test_sh/frps.ini
echo "配置文件权限已修改"
#echo "服务端frps.ini文件已生成($back_file_dir)"
action "服务端frps.ini文件已生成($back_file_dir)" /bin/true
}

#客户端frp配置
Client(){
echo "客户端生成文件中"

echo -e "[common]
server_addr = ${array2[0]}
server_port = 7000
login_fail_exit = false
# auth
authentication_method = token
token = kingsoft">$back_file_dir/frpc.ini


new=`echo $cache_num+1 |bc`
x=1
y=0
m=`echo $cache_num-9 |bc`
teng=8 #定义一个变量,用于后面不去匹配
while [ $x -lt $new ]
do
while [ $y -lt $cache_num ]
do
if [ $m -le 0 ] #cache节点小于等于9执行
then
echo -e "[ssh-cache0$x]
type = tcp
local_ip = ${array1[$y]}
local_port = 22
remote_port = 2200$x">>$back_file_dir/frpc.ini
let y+=1
#continue
break
break

elif [ $m -gt 0 ] && [ $teng -eq 8 ] #cache节点大于9 (执行cache的前9个) 第一次声明teng变量为8 后面定义局部变量为4时,不在匹配此条
then

echo -e "[ssh-cache0$x]
type = tcp
local_ip = ${array1[$y]}
local_port = 22
remote_port = 2200$x">>$back_file_dir/frpc.ini
let y+=1
let x+=1
if [ $x -eq 10 ] #当cache等于10时 跳出循环 跳到-------》
then
break
fi

else #cache节点大于9(执行cache9之后的所有cache)
#m=$cache_num-9
a=10
b=9
while [ $a -lt $new ]
do
while [ $b -lt $cache_num ]
do
echo -e "[ssh-cache$a]
type = tcp
local_ip = ${array1[$b]}
local_port = 22
remote_port = 220$a">>$back_file_dir/frpc.ini
let b+=1
let a+=1
if [ $a -eq $new ] #匹配到直接退出程序 ps:程序死循环了,跳不出去了,后期优化
then
action "客户端配置文件已生成($back_file_dir)" /bin/true
exit
fi
done

done
fi
done

teng=4 # ----------》跳到了这里 赋予局部变量
let x+=1
done

#echo "客户端配置文件已生成($back_file_dir)"
action "客户端配置文件已生成($back_file_dir)" /bin/true
#####################################################################
}

#发送frp安装包
Package(){
sshpass -p $password scp -p -o StrictHostKeyChecking=no $frp_package root@${array2[0]}:/root
if [ $? -eq 0 ]
then
action "frp安装包已发送至服务端:${array2[0]}" /bin/true
fi

sshpass -p $password scp -p -o StrictHostKeyChecking=no $frp_package root@${array2[1]}:/root
if [ $? -eq 0 ]
then
action "frp安装包已发送至客户端:${array2[1]}" /bin/true
fi

expect=`rpm -qa |grep expect`
if [ -z $expect ];then
echo "检测到你未安装expect模块,现在进行安装。。。"
sleep 1
yum install expect -y
else
echo "您已安装expect模块,接下来进行解压安装包"
sleep 1
fi
/usr/bin/expect <<EOF
spawn ssh root@${array2[0]}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "ksyun_cdn\r"; exp_continue }
}
expect "*]#"
send "tar -zxf /root/frp_0.33.0_linux_amd64.tar.gz -C /root\r"
expect "*]#"
send "exit\r"
expect eof
EOF
sleep 3
/usr/bin/expect <<EOF
spawn ssh root@${array2[1]}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "ksyun_cdn\r"; exp_continue }
}
expect "*]#"
send "tar -zxf /root/frp_0.33.0_linux_amd64.tar.gz -C /root\r"
expect "*]#"
send "exit\r"
expect eof
EOF
}

MEM(){

which sshpass &>/dev/null #确定sshpass是否安装,服务器可能没有安装
if [ $? -ne 0 ] #通过which命令判断sshpass是否安装,安装了echo $?则返回0
then #$?不为0时,则说明没有安装
yum install sshpass -y

sshpass -p $password scp -p -o StrictHostKeyChecking=no $back_file_dir/frps.ini root@${array2[0]}:$frp_server_path
#使用sshpass工具和-o参数实现避免交互输入yes和密码
if [ $? -eq 0 ]
then
# echo "frps.ini文件以发送至服务端:${array2[0]}"
action "frps.ini文件以发送至服务端:${array2[0]}" /bin/true
fi
sshpass -p $password scp -p -o StrictHostKeyChecking=no $back_file_dir/frpsc.ini root@${array2[1]}:$frp_server_path

if [ $? -eq 0 ]
then
#echo "frpc.ini文件以发送至客户端:${array2[1]}"
action "frpc.ini文件以发送至客户端:${array2[1]}" /bin/true
fi

else
sshpass -p $password scp -p -o StrictHostKeyChecking=no $back_file_dir/frps.ini root@${array2[0]}:$frp_server_path
#使用sshpass工具和-o参数实现避免交互输入yes和密码

if [ $? -eq 0 ]
then
#echo "frps.ini文件以发送至服务端:${array2[0]}"
action "frps.ini文件以发送至服务端:${array2[0]}" /bin/true
fi
sshpass -p $password scp -p -o StrictHostKeyChecking=no $back_file_dir/frpc.ini root@${array2[1]}:$frp_server_path

if [ $? -eq 0 ]
then
#echo "frpc.ini文件以发送至客户端:${array2[1]}"
action "frpc.ini文件以发送至客户端:${array2[1]}" /bin/true
fi
fi
}

Send(){
#执行检测并安装expect模块
echo -e "/root/frp_0.33.0_linux_amd64/frps -c /root/frp_0.33.0_linux_amd64/frps.ini &
route add -net 172.16.200.0 netmask 255.255.255.0 gw $gw.254">$back_file_dir/frps_local.txt
sshpass -p $password scp -p -o StrictHostKeyChecking=no $back_file_dir/frps_local.txt root@${array2[0]}:/tmp

if [ $? -eq 0 ]
then
echo "/frps_local.txt文件以发送至服务端"
fi

echo -e "/root/frp_0.33.0_linux_amd64/frpc -c /root/frp_0.33.0_linux_amd64/frpc.ini &
route add -net 172.16.200.0 netmask 255.255.255.0 gw $gw.254">$back_file_dir/frpc_local.txt
sshpass -p $password scp -p -o StrictHostKeyChecking=no $back_file_dir/frpc_local.txt root@${array2[1]}:/tmp

if [ $? -eq 0 ]
then
echo "/frpc_local.txt文件以发送至客户端"
fi

expect=`rpm -qa |grep expect`
if [ -z $expect ];then
echo "检测到你未安装expect模块,现在进行安装。。。"
sleep 1
yum install expect -y
else
echo "您已安装expect模块,接下来进行修改对方/etc/rc.local文件"
sleep 1
fi
/usr/bin/expect <<EOF
spawn ssh root@${array2[0]}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "ksyun_cdn\r"; exp_continue }
}
expect "*]#"
send "cat /tmp/frps_local.txt >>/etc/rc.local\r"
expect "*]#"
send "rm -rf /tmp/frps_local.txt\r"
expect "*]#"
send "/root/frp_0.33.0_linux_amd64/frps -c /root/frp_0.33.0_linux_amd64/frps.ini &\r"
expect "*]#"
send "route add -net 172.16.200.0 netmask 255.255.255.0 gw $gw.254\r"
expect "*]#"
send "date\r "
expect "*]#"
send "exit\r"
expect eof
EOF
sleep 3

/usr/bin/expect <<EOF
spawn ssh root@${array2[1]}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "ksyun_cdn\r"; exp_continue }
}
expect "*]#"
send "cat /tmp/frpc_local.txt >>/etc/rc.local\r"
expect "*]#"
send "rm -rf /tmp/frpc_local.txt\r"
expect "*]#"
send "/root/frp_0.33.0_linux_amd64/frpc -c /root/frp_0.33.0_linux_amd64/frpc.ini &\r"
expect "*]#"
send "route add -net 172.16.200.0 netmask 255.255.255.0 gw $gw.254\r"
expect "*]#"
send "date\r "
expect "*]#"
send "exit\r"
expect eof
EOF

}


#利用case语句捕获用户键入的值。
#模式匹配成功,则执行相应函数。
case $num in
1)
Server
;;
2)
Client
;;
3)
Package
;;
4)
MEM
;;
5)
Send
;;
6|q)
exit 0
;;
*)
echo "请输入正确的参数..."
esac
done