获取公网地址并提取ip字符串

获取公网地址

curl myip.ipip.net > ip.log

用sed命令删除所有中文和ip字符串前的多余文本内容,以及行尾空格

cat ip.log | LANG=C sed -r "s/[\x81-\xFE][\x40-\xFE]//g;s/ IP//;s/ *$//"
#!/bin/bash
#allow a dyndns name

HOSTNAME=hostname
LOGFILE=/root/ip.log

Current_IP=$(host $HOSTNAME | cut -f4 -d' ')

if [ $LOGFILE = "" ] ; then
  ufw insert 1 allow from "Current_IP"
  echo $Current_IP > $LOGFILE
else

  Old_IP=$(cat $LOGFILE)

  if [ "$Current_IP" = "$Old_IP" ] ; then
    echo IP address has not changed
  else
    ufw --force delete allow from $Old_IP
    ufw insert 1 allow from $Current_IP
    echo $Current_IP > $LOGFILE
    echo ufw have been updated
  fi
fi

编辑建立

发表回复