美文网首页
shell脚本更改ip地址

shell脚本更改ip地址

作者: 刘伶闯天台 | 来源:发表于2019-04-17 13:45 被阅读0次

    7 年前发布在 51cto 博客的一篇文章。

    原文链接:https://blog.51cto.com/lushang/997964

    shell脚本执行流程如下图:

    执行流程.jpg

    代码:

    #!/bin/sh  
    #脚本用途:用来设置IP、子网掩码、默认网关以及DNS  
    #用法:sh name.sh [-i ipadress] [-n netmask] [-g gateway] #[-d dns]  
    #注意:1.此脚本只能接受设置一个dns值;  
    #     2.可以分别指定相应选项进行设置;  
    #+++++判断是否有输入参数,如果没有则给出脚本用法并退出+++++  
    if [[ $# -lt 1 ]]; then 
        echo "USAGE:`basename $0` [-i ipaderr] [-n netmask] [-g gateway] [-d dnsname]" 
        exit 1  
    fi  
    #+++++自定义函数:IP地址合理性验证+++++  
    #+++此部分代码参考:《编写一个判断IP地址合法性的bash脚本》一文
    #原文链接:http://blog.csdn.net/goodpeter/article/details/2612855
     
    function valid_ip()  
        {  
            local ip=$1  
            local stat=1  
            if [[ $ip =~ '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ]]; then 
            OIFS=$IFS  
            IFS='.' 
            ip=($ip)  
            IFS=$OIFS  
            [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]  
            stat=$?  
            fi  
            return $stat  
        }  
     
    
    #+++++判断选项及参数类型是否正确+++++  
    for i in $(seq 1 $#)  
    do  
        if [ $(($i%2)) == 0 ];then 
         args=$@  
         ip=`echo $args|cut -d" " -f $i`  
         if ! $(valid_ip $ip); then echo "ERROR:ipaddress $ip invalid" && exit;fi  
        else 
         args=$@  
         opt=`echo $args|cut -d" " -f $i`  
         if [[ "$opt" != '-i' && "$opt" != '-n' && "$opt" != '-g' && "$opt" != '-d' ]];then   
             echo "ERROR:  Option \"$opt\" does not exist " && exit  
         fi  
        fi  
    done  
    #+++++getopts分析选项及参数+++++     
    while getopts :i:n:g:d:: name 
    do  
        case $name in 
        "i")  
            ipadd=$OPTARG  
        ;;   
        "n")  
            netmask=$OPTARG  
        ;;  
        "g")  
            gateway=$OPTARG  
        ;;  
        "d")  
            dns=$OPTARG  
        ;;  
        "?")  
            echo "ERROR:  $OPTARG  not exist";exit  
        ;;  
        ":")   
            echo "ERROR:  -$OPTARG Requires an argument ";exit  
        ;;  
        *) echo "ERROR:  Unknow option" 
        ;;  
        esac  
    done  
    #+++++定义一些变量+++++  
    ifcfg=/etc/sysconfig/network-scripts/ifcfg-eth0  
    mac=`grep -i HWADDR $ifcfg|awk -F "=" '{print $2}'`  
    resolv=/etc/resolv.conf  
    network=/etc/sysconfig/network  
    #自定义设置IP函数:  
    function set_ip()  
    {  
        cp -r $ifcfg ${ifcfg}.bak  
        echo -ne "DEVICE=eth0  
        BOOTPROTO=static 
        HWADDR=$mac  
        IPADDR=$ipadd  
        NETMASK=$netmask  
        ONBOOT=yes  
        " > $ifcfg  
        echo "IPaddress set Success!" 
    }  
    #+++++自定义设置网关函数+++++  
    function set_gw()  
    {  
        cp -r $network ${network}.bak  
        echo -ne "NETWORKING=yes  
        NETWORKING_IPV6=no 
        HOSTNAME=`hostname`  
        GATEWAY=$gateway  
        " > $network  
        echo "Netmask set Success!" 
    }  
    #+++++自定义设置dns函数+++++  
    function set_dns()  
    {  
        cp -r $resolv ${resolv}.bak  
        echo -e "nameserver $dns" > $resolv  
        echo "DNS set Success!" 
    }  
    #+++++++++++++++++++++++++++++++++++++++++++++++  
    #1.对脚本选项进行分析,是否有指定ip地址?如果没有指定ip地址选项则#保持当前ip地址不变,如果当前并未设置ip地址,则提示必须指定ip地址#选项并退出脚本;  
    #2.同上对子网掩码进行判断;  
    #3.同上对默认网关进行判断;  
    #+++++++++++++++++++++++++++++++++++++++++++++++  
    if [ ! $ipadd ];then 
        ipadd=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`  
        if [ ! $ipadd ];then 
         echo "There are currently no ip,Please use '-i' option specified IPaddress" 
        fi  
    fi  
    if [ ! $netmask ];then 
        echo "Use the current netmask" 
        netmask=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $4}'|tr -d "Mask:"`  
        if [ ! $netmask ];then 
         echo "The current netmask does not exist Re-run the specified '-n' option or Use the default netmask:255.255.255.0" 
         netmask="255.255.255.0" 
         echo "IP set:$ipadd NETMASK set:$netmask" 
        fi  
    else 
        echo "IP set:$ipadd NETMASK set:$netmask" 
    fi  
    if [ ! $gateway ];then 
        echo "Use the current gateway" 
        gateway=`route|grep default|awk '{print $2}'`  
        if [ ! $gateway ];then 
         gateway=`echo $ipadd|cut -d. -f1-3`  
         gateway=${gateway}.1  
         echo "Using the default gateway $gateway " 
        fi  
    else 
        echo "Gateway set:$gateway" 
    fi  
    #+++++判断是否指定设置dns选项,若指定则修改dns,未指定则不修改;  
    if [ $dns ];then 
        echo "DNS set:$dns" 
        set_dns  
    fi  
    #+++++设置ip地址及子网掩码+++++  
    set_ip  
    #++++设置默认网关+++++  
    set_gw  
    #+++++重启网络服务+++++  
    service network restart >/dev/null 
    #+++++输出设置的ip地址+++++  
    echo "IPADDRESS:$ipadd" 
    #+++++输出设置的子网掩码+++++  
    echo "NETMASK:$netmask" 
    #+++++输出设置的默认网关+++++  
    echo "GATEWAY:$gateway" 
    #+++++输出设置OK信息+++++  
    echo "Set OK!" 
    

    相关文章

      网友评论

          本文标题:shell脚本更改ip地址

          本文链接:https://www.haomeiwen.com/subject/istnwqtx.html