自动检测公网ip可用性并替换不可用的

#!/bin/sh

log_file=/home/infra_123/eip_vpn/EIP_replace.log
tfstate=/home/infra_123/eip_vpn/terraform.tfstate
ipfile=/home/infra_123/eip_vpn/iplist
mailfile=/home/infra_123/eip_vpn/mailfile.txt

echo "" >> $log_file
echo `date` >> $log_file
rm $mailfile

#sit and uat, tcp 443 port
while read line; do
  attach=`echo $line|awk -F ' ' '{print $1}'`
  eip=`echo $line|awk -F ' ' '{print $2}'`
  clbins=`echo $attach|sed -e 's/_lb//g'`
  echo -e '\x1dclose\x0d' | telnet $eip 443
  if [ $? == 1 ] ; then
    cd /home/infra_li.jun/eip_vpn/
    ./terraform12 destroy -target=module.modules.tencentcloud_clb_instance.$clbins -auto-approve 
    ./terraform12 apply -target=module.modules.tencentcloud_clb_attachment.$attach -auto-approve
    newip=`./terraform12 state show module.modules.tencentcloud_clb_instance.$clbins|egrep -o "\b(12[89]|1[3-8][0-9]|19[01])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}\b"`
    sed -i "s/$eip/$newip/g" $ipfile
    sleep 5
#check if the new ip is ok
    echo -e '\x1dclose\x0d' | telnet $newip 443
    if [ $? == 1 ] ; then
      echo "$line is NG! Replace with new ip $newip(NG, please check the server!)" >> $log_file
      echo "$line is NG! Replace with new ip $newip(NG, please check the server!)" >> $mailfile
    else
      echo "$line is NG! Replace with new ip $newip(OK)" >> $log_file
      echo "$line is NG! Replace with new ip $newip(OK)" >> $mailfile
    fi
  else
    echo "$line is OK" >> $log_file
    echo "$line is OK" >> $mailfile
  fi
done < $ipfile

#prod, udp 1194 port
cd /home/infra_123/eip_vpn/
prodipfile=/home/infra_123/eip_vpn/prodiplist
line_p=`cat $prodipfile`
attach_p=`echo $line_p|awk -F ' ' '{print $1}'`
eip_p=`echo $line_p|awk -F ' ' '{print $2}'`
clbins_p=`echo $attach_p|sed -e 's/_lb//g'`
result_p=`python3 prod_vpn.py -p 1194 --tls-auth ta.key $eip_p`
echo -e $result_p|grep OK
if [ $? == 1 ] ; then
  ./terraform12 destroy -target=module.modules.tencentcloud_clb_instance.$clbins_p -auto-approve
  ./terraform12 apply -target=module.modules.tencentcloud_clb_attachment.$attach_p -auto-approve
  newip_p=`./terraform12 state show module.modules.tencentcloud_clb_instance.$clbins_p|egrep -o "\b(12[89]|1[3-8][0-9]|19[01])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}\b"`
  sed -i "s/$eip_p/$newip_p/g" $prodipfile
  result_p=`python3 prod_vpn.py -p 1194 --tls-auth ta.key $newip_p`
  echo -e $result_p|grep OK
  if [ $? == 1 ] ; then
    echo "$line_p is NG! Replace with new ip $newip_p(NG, please check the server!)" >> $log_file
    echo "$line_p is NG! Replace with new ip $newip_p(NG, please check the server!)" >> $mailfile
  else
    echo "$line_p is NG! Replace with new ip $newip_p(OK)" >> $log_file
    echo "$line_p is NG! Replace with new ip $newip_p(OK, udp 1194)" >> $mailfile
  fi

else
  echo "$line_p is OK" >> $log_file
  echo "$line_p is OK(udp 1194)" >> $mailfile
fi

#mail addresses list
mail -s 'VPN ipaddress update' mailaddress@123.com < $mailfile

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值