#!/bin/bash
#
TMPFILE=`mktemp /tmp/eth.XXXXXXXX`
read -p "Interface: " ETHCARD
ALLECARD=`ifconfig -a | awk '/^[^[:space:]l]/{print $1}'`
until echo $ALLECARD | grep "$ETHCARD" &> /dev/null; do
echo -e "\033[31mWrong Card name.\033[0m"
read -p "Interface: " ETHCARD
done
ETHFILE=/etc/sysconfig/network-scripts/ifcfg-$ETHCARD
read -p "Boot Protocol: " MYBOOTPROTO
until echo $MYBOOTPROTO | grep -E "dhcp|bootp|static|none"; do
echo -e "\033[31mWrong BOOTPROTO.\033[0m"
read -p "Boot Protocol: " MYBOOTPROTO
done
if [ "$MYBOOTPROTO" == "dhcp" ]; then
sed -i "s/^BOOTPROTO=.*/BOOTPROTO=dhcp/g" $ETHFILE
if [ $? -eq 0 ]; then
ifdown $ETHCARD && ifup $ETHCARD
[ $? -eq 0 ] && echo "Set $ETHCARD done."
fi
elif [ "$MYBOOTPROTO" == "static" ]; then
cat $ETHFILE > $TMPFILE
read -p "Ip Address: " MYIP
read -p "Netmask: " MYNM
read -p "Gateway: " MYGW
sed -i "s/^BOOTPROTO=.*/BOOTPROTO=static/g" $TMPFILE
grep "^IPADDR=" $TMPFILE &> /dev/null && sed -i "s/IPADDR=.*/IPADDR=$MYIP/" $TMPFILE || echo "IPADDR=$MYIP" >> $TMPFILE
grep "^NETMASK=" $TMPFILE &> /dev/null && sed -i "s/NETMASK=.*/NETMASK=$MYNM/" $TMPFILE || echo "NETMASK=$MYNM" >> $TMPFILE
if [ -z $MYGW ]; then
sed -i '/^GATEWAY=.*/d' $TMPFILE
else
grep "^GATEWAY=" $TMPFILE &> /dev/null && sed -i "s/GATEWAY=.*/GATEWAY=$MYGW/" $TMPFILE || echo "GATEWAY=$MYGW" >> $TMPFILE
fi
cp -f $TMPFILE $ETHFILE
ifdown $ETHCARD && ifup $ETHCARD
[ $? -eq 0 ] && echo "Set $ETHCARD done."
else
echo "No such options."
exit 1
fi
rm -f $TMPFILE
阅读(1012) | 评论(0) | 转发(0) |