需求分析: 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 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] }' ` 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_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 back_file_dir=$home_path /frp_test 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 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 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.txtfor 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.txtwhile truedo cat <<EOF ----------------------------- 菜单---------------------------------- - 1 .生成frp服务端frps.ini文件 - - 2 .生成frp客户端frpc.ini文件 - - 3 .安装frp - - 4 .发送frp文件到对应服务器 - - 5 .启动frp服务并生成/etc/rc.local文件 - - 6 .退出 - ------------------------------------------------------------------ EOF read -p "请输入你要执行的操作:>>>" num Server(){ echo -e "[common] \nbind_addr = $ {array2[0]} \nbind_port = 7000 \n# auth \nauthentication_method = token \ntoken = kingsoft" > $back_file_dir /frps.iniecho "配置文件权限已修改" action "服务端frps.ini文件已生成($back_file_dir )" /bin/true } 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.ininew=`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 ] 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 break break elif [ $m -gt 0 ] && [ $teng -eq 8 ] 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 ] then break fi else 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 ] then action "客户端配置文件已生成($back_file_dir )" /bin/true exit fi done done fi done teng=4 let x+=1 done action "客户端配置文件已生成($back_file_dir )" /bin/true } 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 if [ $ ? -ne 0 ] then yum install sshpass -y sshpass -p $password scp -p -o StrictHostKeyChecking=no $back_file_dir /frps.ini root@ $ {array2[0 ]}:$frp_server_path if [ $ ? -eq 0 ]then 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 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 if [ $ ? -eq 0 ]then 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 action "frpc.ini文件以发送至客户端:$ {array2[1]}" /bin/true fi fi } Send(){ 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.txtsshpass -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.txtsshpass -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 $num in 1 ) Server ;; 2 ) Client ;; 3 ) Package ;; 4 ) MEM ;; 5 ) Send ;; 6 |q) exit 0 ;; *) echo "请输入正确的参数..." esac done