今天突发奇想,想设计一个能以互动形式对网卡配置进行修改的脚本,我们都知道在linux中,想进行dhcp和static模式切换、更改IP等信息变更时,需要找到到/etc/sysconfig/network-scripts/ifcfg-eth0网卡配置文件并用vim编辑修改,然后/etc/init.d/network restart重启生效,配置文件路径比较长不好记,变更步骤也比较繁琐,于是介于刚新学的知识,我搞出了这么个脚本,经测试,有些瑕疵,但可以使用,(说实话shell感觉挺好玩的,哈哈)。脚本功能:可用选项互动式编辑网卡信息,方便快捷先上菜单图:
脚本内容如下:#!/bin/bash
#by LiuPengfang ------2015/08/04-------
#auto_IP_alter.sh
IPPATH=/etc/sysconfig/network-scripts/ifcfg-eth0
IP=`cat $IPPATH |grep "IPADDR="|awk -F "=" '{print$2}'`
NET=`cat $IPPATH |grep "NETMASK="|awk -F "=" '{print$2}'`
GAT=`cat $IPPATH |grep "GATWAY="|awk -F "=" '{print$2}'`
BOOT=`cat $IPPATH |grep "BOOTPROTO=" |awk -F "=" '{print$2}'`
#-------------判断网络模式并判断是否需追加ip信息-----------------------------
cat /etc/sysconfig/network-scripts/ifcfg-eth0 |grep "IPADDR" &>/dev/null
if [ $? -eq 0 ];then
ls &>/dev/null
else
echo "#IPADDR=192.168.1.10" >> $IPPATH
echo "#NETMASK=255.255.255.0" >> $IPPATH
echo "#GATWAY=192.168.1.1" >> $IPPATH
fi
#------------------------菜单显示--------------------------------------------
echo -e "\033[35m----------------------------------------------\033[0m"
echo -e "\033[35m-------------欢迎使用IP修改工具---------------\033[0m"
echo -e "\033[35m----------------------------------------------\033[0m"
if [[ $BOOT = dhcp ]];then
echo -e "\033[37m现在的网络模式是:$BOOT\033[0m"
else
echo -e "\033[37m现在的网络模式是:$BOOT\033[0m"
echo -e "\033[37m现在的IP是:$IP\033[0m"
echo -e "\033[37m现在的mask是:$NET\033[0m"
echo -e "\033[37m现在的gatway是:$GAT\033[0m"
fi
echo -e "\033[32m请按选项选择修改:\033[0m"
echo -e "\033[31m1) | 修改IP\033[0m"
echo -e "\033[31m2) | 修改mask\033[0m"
echo -e "\033[31m3) | 修改gatway\033[0m"
echo -e "\033[31m4) | 修改网络模式\033[0m"
echo -e "\033[31mq) | 退出\033[0m"
echo -e "\033[35m----------------------------------------------\033[0m"
#------------------------修改内容-------------------------------
#------------------------IP修改---------------------------------
function IPADDR (){
read -p "请输入您要修改的IP:" i
sed -i "s/$IP/$i/g" $IPPATH
if [ $? = 0 ];then
echo -e "\033[36m修改成功!\033[0m"
else
echo -e "\033[36m修改失败!\033[0m"
fi
}
#-----------------------掩码修改--------------------------------
function NETMASK (){
read -p "请输入您要修改的子网掩码:" i
sed -i "s/$NET/$i/g" $IPPATH
if [ $? = 0 ];then
echo -e "\033[36m修改成功!\033[0m"
else
echo -e "\033[36m修改失败!\033[0m"
fi
}
#-----------------------网关修改--------------------------------
function GATWAY (){
read -p "请输入您要修改的网关:" i
sed -i "s/$GAT/$i/g" $IPPATH
if [ $? = 0 ];then
echo -e "\033[36m修改成功!\033[0m"
else
echo -e "\033[36m修改失败!\033[0m"
fi
}
#-----------------------注释IP信息-------------------------------
function DHCP (){
sed -i 's/^IPADDR/#IPADDR/g' $IPPATH
sed -i 's/^NETMASK/#NETMASK/g' $IPPATH
sed -i 's/^GATWAY/#GATWAY/g' $IPPATH
if [ $? = 0 ];then
echo -e "\033[36m修改成功!\033[0m"
else
echo -e "\033[36m修改失败!\033[0m"
fi
}
#-----------------------开放IP信息-------------------------------
function STATIC (){
sed -i 's/^#IPADDR/IPADDR/g' $IPPATH
sed -i 's/^#NETMASK/NETMASK/g' $IPPATH
sed -i 's/^#GATWAY/GATWAY/g' $IPPATH
if [ $? = 0 ];then
echo -e "\033[36m修改成功!\033[0m"
else
echo -e "\033[36m修改失败!\033[0m"
fi
}
#-----------------------网络模式修改-----------------------------
function BOOT (){
read -p "请输入您要修改的网络模式:" i
sed -i "s/$BOOT/$i/g" $IPPATH
sleep 2
if [[ $i = dhcp ]];then
DHCP
else
STATIC
fi
}
#-----------------------功能调用----------------------------------
read -p "请输入您要修改的选项:" i
case $i in
1)
IPADDR
;;
2)
NETMASK
;;
3)
GATWAY
;;
4)
BOOT
;;
q)
exit
;;
*)
echo -e "\033[36m请输入正确选项!\033[0m"
sh ip.sh
esac
#-----------------------重启服务----------------------------------
echo -e "\033[36m正在重启网络服务……\033[0m"
/etc/init.d/network restart &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[36m网络服务启动成功!\033[0m"
else
echo -e "\033[36m网络服务启动失败!\033[0m"
fi