当前位置: 移动技术网 > 网络运营>服务器>Linux > Shell脚本检查IP格式及mysql操作实例

Shell脚本检查IP格式及mysql操作实例

2017年12月12日  | 移动技术网网络运营  | 我要评论
还是cronjob的一部分,就是在rails的定时任务里,后台交互运行 checkipaddress() { echo $1 |grep "^[0-9

还是cronjob的一部分,就是在rails的定时任务里,后台交互运行

checkipaddress()
{
    echo $1 |grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" > /dev/null
    if [ $? = 1 ];  then
        return 1
    else
        a=`echo $1 | awk -f. '{print $1}'`
        b=`echo $1 | awk -f. '{print $2}'`
        c=`echo $1 | awk -f. '{print $3}'`
        d=`echo $1 | awk -f. '{print $4}'`
        #echo $a $b $c $d

        for loop in $a $b $c $d
        do
            if [ $loop -ge 255 ] || [ $loop -lt 0 ]; then
                return 2
            fi
        done
    fi  

}


configuredefaultregion() {
  echo "please input region ip"
  ret=1
  while [ $ret != 0 ]
  do
   read region_ip
   checkipaddress $region_ip
   ret=$?
   #echo $ret
   if [ $ret = 1 ]; then
    echo "wrong ip address, please reinput region ip:"
   fi
  done
  /usr/bin/mysql -u root realworx_production -e "update regions set ip='$region_ip' where id=1" 1>/dev/null 2>/dev/null
  if [ $? = 0 ]; then
          /usr/bin/mysql -u root realworx_production -e "update config_params set val=1 where ident=55" 1>/dev/null 2>/dev/null
          echo "set '$region_ip' as default and admin region ip"
  else
          val=`/usr/bin/mysql -u root realworx_production -e "select id from regions where ip='$region_ip'" | awk '{if ($1 != "id") print $1}'`
          /usr/bin/mysql -u root realworx_production -e "update config_params set val='$val' where ident=55" 1>/dev/null 2>/dev/null
          region_name=`/usr/bin/mysql -u root realworx_production -e "select name from regions where ip='$region_ip'" | awk '{if ($1 != "name") print $1}'`
          echo "ip already exists. so set '$region_name' as admin region. "
  fi
  echo "region setting successfull."
}

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网