linux修改Ip的shell指令碼
阿新 • • 發佈:2019-02-05
思路是 首先通過find命令找到/etc/sysconfig/network-scripts/目錄下的ifcfg-en*的檔案,然後通過sort排序,將第一個檔案作為要修改的檔案。
剩下的就是對文字的修改了。。。
注意我這個指令碼是把192.168.130段寫進去了,如果想要修改成別的,需要自己重新修改。
#/bin/bash echo "###input ip you want change to ### " ipfront=192.168.130. read ip1 ip="${ipfront}${ip1}" echo "###ip====" $ip "###" eth=`find /etc/sysconfig/network-scripts/ -name "ifcfg-e*" |sort |head -1 |awk -F/ '{print $5}'` ethfile="/etc/sysconfig/network-scripts/$eth" echo "${ethfile}" sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/g' "${ethfile}" sed -i 's/^ONBOOT=no/ONBOOT=yes/g' "${ethfile}" #testfile="/etc/sysconfig/network-scripts/test" echo "IPADDR=${ip}" >>${ethfile} echo "NETMASK=255.255.255.0" >>${ethfile} echo "GATEWAY=192.168.130.254" >>${ethfile} service network restart