??????????Linux SA?????????к??????????????????????????ip?????t??ip????MAC????????????????????????????????????????????????????ip?????????????á???????????????????????????????????????????????????ip?????????????????????????????????????????????
#!/bin/sh
#auto Change ip netmask gateway scripts
#wugk 2012-12-17
cat << EOF
++++++++??????ip????????????????+++++++++
ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0
HOSTS=/etc/hosts
NETWORK=/etc/sysconfig/network
DIR=/data/backup/`date +%Y%m%d`
NETMASK=255.255.255.0
+++++++++-------------------------+++++++++++
EOF
#Define Path  ?????????????????????????
ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0
HOSTS=/etc/hosts
NETWORK=/etc/sysconfig/network
DIR=/data/backup/`date +%Y%m%d`
NETMASK=255.255.255.0
echo "================================================"
echo
#????change_ip????
function Change_ip ()
{
#?ж???????????????????????п?????????shell??????????#
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
echo "Now Change ip address ??Doing Backup Interface eth0"
cp $ETHCONF $DIR
grep "dhcp"  $ETHCONF
#????$??????ж?????β??????????0?????????β??????????????#
if
[ $? -eq 0 ];then
#read -p ???????????IPADDR???e????п??sed -i ??????????#
read -p "Please insert ip Address:" IPADDR
sed -i 's/dhcp/static/g' $ETHCONF
#awk -F. ???????.????????????????#
echo -e "IPADDR=$IPADDR NETMASK=$NETMASK GATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF
echo "This IP address Change success !"
else
echo -n  "This $ETHCONF is static exist ??please ensure Change Yes or NO":
read i
fi
if
[ "$i" == "y" -o "$i" == "yes" ];then
read -p "Please insert ip Address:" IPADDR
count=(`echo $IPADDR|awk -F. '{print $1??$2??$3??$4}'`)
#???????飬 ${#count[@]}????????????????#
A=${#count[@]}
#while????????ж????????????????????????????????????[0-9]???ж?ip#
while
[ "$A" -ne "4" ]
do
read -p "Please re Inster ip Address??example 192.168.0.11 ip": IPADDR
count=(`echo $IPADDR|awk -F. '{print $1??$2??$3??$4}'`)
A=${#count[@]}
done
#sed -e ?????????????????#
sed -i -e 's/^IPADDR/#IPADDR/g' -e 's/^NETMASK/#NETMASK/g' -e 's/^GATEWAY/#GATEWAY/g' $ETHCONF
#echo -e ??????????????????????#
echo -e "IPADDR=$IPADDR NETMASK=$NETMASK GATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF
echo "This IP address Change success !"
else
echo "This $ETHCONF static exist??please exit"
exit $?
fi
}
#????hosts????
############function hosts##############
function Change_hosts ()
{
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
cp $HOSTS $DIR
read -p "Please insert ip address": IPADDR
host=`echo $IPADDR|sed 's/./-/g'`
cat $HOSTS |grep 127.0.0.1 |grep "$host"
if
[ $? -ne 0 ];then
sed -i "s/127.0.0.1/127.0.0.1 $host/g" $HOSTS
echo "This hosts change success "
else
echo "This $host IS Exist .........."
fi
}
###########fuction network###############
#????network????
function Change_network ()
{
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
cp $NETWORK $DIR
read -p "Please insert ip address": IPADDR
host=`echo $IPADDR|sed 's/./-/g'`
grep "$host" $NETWORK
if
[ $? -ne 0 ];then
sed -i "s/^HOSTNAME/#HOSTNAME/g" $NETWORK
echo "NETWORK=$host" >>$NETWORK
else
echo "This $host IS Exist .........."
fi
}
#PS3?????????????#
PS3="Please Select ip or hosts Menu":
#select???????????????select $var in ..command.. do .... done
select i in  "Change_ip" "Change_hosts" "Change_network"
do
#case ????????????????????μ??ж?
case $i in
Change_ip )
Change_ip
;;
Change_hosts )
Change_hosts
;;
Change_network )
Change_network
;;
*)
echo
echo "Please Insert $0: Change_ip(1)|Change_hosts(2)|Change_network(3)"
echo
;;
esac
done