闲来无事,想想做测试的时候配置IP地址很麻烦,写了个简单的脚本。自己觉得还是比较烂吧,能用就行。
#!/bin/bash
#config network value by simple style.
#the system version is RHEL5.6
intdir=/etc/sysconfig/network-scripts
tempfile=/tmp/netconfig.tmp.txt
read -p "which interface you want to config?($(ls ${intdir} | grep ifcfg | cut -d- -f2 | grep -v lo) or? ): " interface
IPADDR=`cat $intdir/ifcfg-${interface} | grep IPADDR | cut -d= -f2`
NETMASK=`cat $intdir/ifcfg-${interface} | grep NETMASK | cut -d= -f2`
GATEWAY=`cat $intdir/ifcfg-${interface} | grep GATEWAY | cut -d= -f2`
function viewconfig
{
echo "current config is:"
echo "IPADDR=$IPADDR"
echo "NETMASK=$NETMASK"
echo "GATEWAY=$GATEWAY"
read -p "Are you sure?(y/n):" confirm
}
viewconfig
while [ $confirm != y ]
do
#read value
read -p "please input a value IPv4 address: " IPADDR
read -p "please input a value netmask: " NETMASK
read -p "please input a value IPv4 address of the default gateway: " GATEWAY
cat $intdir/ifcfg-${interface} | grep -v IPADDR | grep -v NETMASK | grep -v GATEWAY | grep -v BOOTPROTO > $tempfile
viewconfig
if [ $confirm != y ]
then continue
fi
done
#load config
echo "BOOTPROTO=static" >> $tempfile
echo "IPADDR=$IPADDR" >> $tempfile
echo "NETMASK=$NETMASK" >> $tempfile
echo "GATEWAY=$GATEWAY" >> $tempfile
cat $tempfile > ${intdir}/ifcfg-${interface}
rm -rf $tempfile
read -p "Do you want to config the DNS Server address?(y/n)" confirm
if [ $confirm = y ]
then
read -p "please input a IPv4 address of first dnsserver: " nameserver1
echo "nameserver $nameserver1" > /etc/resolv.conf
read -p "Do you want to config the second DNS Server address?(y/n)" confirm
if [ $confirm = y ]
then
read -p "please input a IPv4 address of second dnsserver: " nameserver2
echo "nameserver $nameserver2" >> /etc/resolv.conf
fi
fi
/sbin/ifdown $interface
/sbin/ifup $interface
#by Kaiser