• 判断IP是否规范并添加或修改IP地址的shell脚本


    .

    #!/bin/bash
    read -p "请输入ip:" IP
    read -p "请输入掩码:" MASK

    #read -p "IP ADDRESS:" IPADD
    echo $IP > /tmp/tmpserverip
    echo $IP | grep "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" > /dev/null
    if [ $? = 1 ]
    then
      echo "error ip,please enter correct"
      exit 1
    else
      a=`awk -F. '{print $1}' /tmp/tmpserverip`
      b=`awk -F. '{print $2}' /tmp/tmpserverip`
      c=`awk -F. '{print $3}' /tmp/tmpserverip`
      d=`awk -F. '{print $4}' /tmp/tmpserverip`
        if [ $a -ge 255 ]||[ $a -le 0 ]
        then
          echo "a:error ip"
          exit 1
        else
          echo a > /tmp/tang
        fi

        if [ $b -ge 255 ] || [ $b -lt 0 ]
        then
          echo "b:error ip"
          exit 1
        else
          echo b >> /tmp/tang
        fi

        if [ $c -ge 255 ] || [ $c -lt 0 ]
        then
          echo "c:error ip"
          exit 1
        else
          echo c >> /tmp/tang
        fi
        if [ $d -ge 255 ] || [ $d -le 0 ]
        then
          echo "d:error ip"
          exit 1
        else
          echo d >> /tmp/tang
        fi
    fi

    sed -i "s/ONBOOT=no/ONBOOT=yes/g" /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i "s/BOOTPROTO=dhcp/BOOTPROTO=static/g" /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i 's/NM_CONTROLLED=yes/NM_CONTROLLED=no/g' /etc/sysconfig/network-scripts/ifcfg-eth0
    grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 &> /dev/null
    if [ $? -eq 0 ]
    then
      sed -i "s/IPADDR=*.*.*.*/IPADDR=$IP/g" /etc/sysconfig/network-scripts/ifcfg-eth0
      sed -i "s/NETMASK=*.*.*.*/NETMASK=$MASK/g" /etc/sysconfig/network-scripts/ifcfg-eth0
    else
      echo "IPADDR=$IP" >> /etc/sysconfig/network-scripts/ifcfg-eth0
      echo "NETMASK=$MASK" >> /etc/sysconfig/network-scripts/ifcfg-eth0
    fi

    /etc/init.d/network restart

     

  • 相关阅读:
    【BFS】【HDOJ-1195】Open the Lock
    hadoop经典案例
    eclipse中下载maven插件解决办法
    eclipse中导入maven项目:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.Maven
    mysql Alter 的问题
    代理模式:利用JDK原生动态实现AOP
    JAVA中关于set()和get()方法的理解及使用
    java 中 Cannot make a static reference to the non-static 解决方法
    maven clean 异常问题
    自定义scoll样式
  • 原文地址:https://www.cnblogs.com/Leonardo-li/p/8440522.html
Copyright © 2020-2023  润新知