设置地址

#!/bin/sh
#$1 IP
#$2 netmask
#$3 gw
netWorkCard=eno1    //以太网
configFile=/etc/rc.d/rc.local   //配置文件
#取得那个位置的ip
if [ -a $configFile ];  //如果文件存在
then
        oldip=`cat $configFile| grep "ifconfig $netWorkCard" | awk '{print $3}'`   //  找出旧的ip
        oldnetmask=`cat $configFile| grep "ifconfig $netWorkCard" | awk '{print $5}'`
        oldgw=`cat $configFile| grep "dev $netWorkCard" | awk '{print $5}'`
        echo $oldip "->" $1   
        echo $oldnetmask "->"  $2
        echo $oldgw "->" $3
        #替换为新的IP
        sudo -S sed -i  "s/$oldip/$1/" $configFile        //替换oldip   用S1
        sudo -S sed -i  "s/$oldnetmask/$2/" $configFile
        sudo -S sed -i  "s/$oldgw/$3/" $configFile
        sudo -S  ifconfig $netWorkCard $1 netmask $2

        currentIp=`ifconfig $netWorkCard |grep -w inet | awk '{print $2}'`
        currentNetmask=`ifconfig $netWorkCard |grep -w inet | awk '{print $4}'`
        echo $currentIp
        oldip=`cat $configFile| grep "ifconfig $netWorkCard" | awk '{print $3}'`
        oldnetmask=`cat $configFile| grep "ifconfig $netWorkCard" | awk '{print $5}'`
  //and 
        if  [ $currentIp == $oldip -a  $currentNetmask == $oldnetmask -a $currentIp == $1 -a  $currentNetmask == $2 ];
        then
                ifconfig $netWorkCard down
                ifconfig $netWorkCard up
                echo "ip setting sucess!"
                route add default gw $3
        else
                echo "ip setting fail!"
        fi
else
        echo "file no exist!"
fi
 
posted @ 2019-08-08 19:39  countryboy666  阅读(192)  评论(0编辑  收藏  举报