替换IP4种方法

1.没有配内网IP的话
sed -i s#ONBOOT=no#ONBOOT=yes#g /etc/sysconfig/network-scripts/ifcfg-eth1
cat  >>/etc/sysconfig/network-scripts/ifcfg-eth1<<EOF
IPADDR=192.168.20.157
NETMASK=255.255.255.0
EOF
2.如果配了,可以
A直接修改,省略
B利用sed替换,省略
C用脚本来替换

#!/bin/bash
#qq:40594324
#version 1.0
#this script for change eth
echo "press input eth0 or eth1"
read A
eth=$A
IFCFG=/etc/sysconfig/network-scripts/ifcfg-${eth}
ADDR=`ifconfig ${eth} |sed -ne 's/^.* inet addr:\([^ ]*\).*$/\1/gp'`
MASK=`ifconfig ${eth} |sed -ne 's/^.* Mask:\([^ ]*\).*$/\1/gp'`
echo "你要替换的地址是${eth} ${ADDR} ${MASK}"
echo "you want change :"
read  a b
echo "替换后的地址为${eth} $a $b"

echo "ok?no?"
read c
if [ "$c" == "ok"  ]
    then
                cp ${IFCFG} ${IFCFG}.bak
                sed -i "s/$ADDR/$a/g" $IFCFG
                sed -i "s/$MASK/$b/g" $IFCFG
echo "now: $eth $a $b "        
fi
 

你可能感兴趣的:(IP,替换,脚本,过滤)