首页 > 代码库 > 初见shell,设置网卡参数
初见shell,设置网卡参数
初见shell,写一个设置网卡参数的脚本试一下。效率很低,但是凑合用了。
===================
#!/bin/bash
LANG=zh_CN.UTF-8
clear #Select the network card
echo "`date`"
echo -e "\033[34m Select Network card(Enter to continue)\033[0m"
ceshi=$(find /etc/sysconfig/network-scripts/ -name ‘*ifcfg*‘)
#Segmentation variables data
OLD_IFS="$IFS"
IFS=" "
arr=($ceshi)
IFS="$OLD_IFS"
i=0 #Definitions and variables
for s in ${arr[@]} #for
do
# echo "$s"
# echo "--"
conid[i]=$s
echo "No." $i "Network card:" $s
((i++))
done
# echo ${conid[0]}
# echo ${conid[1]}
#echo ${#conid[*]} #pinrt conid[*]
#检测用户选择网卡
echo -e "\033[34m Select Network card No.: \033[0m"
read conidNum #user in
#echo $conidNum
clear #onboot
echo -e "\033[34m You select No. $conidNum Network card:${conid[$conidNum]} \033[0m"
echo "You want to boot with this card(ONBOOT):"
echo -e "1.ONBOOT=\033[33m YES \033[0m"
echo -e "2.ONBOOT=\033[33m NO \033[0m"
echo "Enter 1 or 2 :"
read onboot
case $onboot in
1)
onboottxt="YES"
;;
2)
onboottxt="NO"
;;
esac
clear #Bootproto
echo -e "\033[34m You select No. $conidNum Network card:${conid[$conidNum]} \033[0m"
echo -e "\033[34m Boot with this card $onboottxt 。(ONBOOT=${onboottxt}) \033[0m"
echo "Select network mode (BOOTPROTO):"
echo -e "1.BOOTPROTO=\033[33m STATIC \033[0m"
echo -e "2.BOOTPROTO=\033[33m DHCP \033[0m"
echo "Enter 1 or 2 :"
read bproto
case $bproto in
1)
bprototxt="STATIC"
;;
2)
bprototxt="DHCP"
echo "Any key to Finish,Ctrl+c to Exit 。"
read -n 1
sed -i ‘/ONBOOT/d‘ ${conid[$conidNum]}
sed -i ‘/BOOTPROTO/d‘ ${conid[$conidNum]}
echo "ONBOOT="$onboottxt >>${conid[$conidNum]}
echo "BOOTPROTO="$bprototxt >>${conid[$conidNum]}
#del old config
sed -i ‘/IPADDR/d‘ ${conid[$conidNum]}
sed -i ‘/NETMASK/d‘ ${conid[$conidNum]}
sed -i ‘/GATEWAY/d‘ ${conid[$conidNum]}
sed -i ‘/DNS1/d‘ ${conid[$conidNum]}
sed -i ‘/DNS2/d‘ ${conid[$conidNum]}
echo "Modifications are complete!!! service reatst。。"
service network restart
#service network restart
echo "-------Print ifconfig-------"
ifconfig
echo "-------Print ifconfig-------"
#echo ${conid[$conidNum]}
echo "Any key to Exit。"
read -n 1
exit
;;
esac
clear #IPADDR
echo -e "\033[34m You select No. $conidNum Network card:${conid[$conidNum]} \033[0m"
echo -e "\033[34m Boot with this card $onboottxt 。(ONBOOT=${onboottxt}) \033[0m"
echo -e "\033[34m The network mode $bprototxt。(BOOTPROTO=${bprototxt}) \033[0m"
echo "Plase enter IP Address:"
read ipaddr
echo "Plase enter NetMask:"
read netmask
echo "Plase enter GateWay:"
read gateway
echo "Plase enter NDS Main:"
read dns1
echo "Plase enter NDS Alternate(Enter to Default configuration):"
read dns2
echo "Plase check the configuration,Enter to Finish or Ctrl+c to Exit。"
#echo ${conid[$conidNum]}
#del old config
sed -i ‘/ONBOOT/d‘ ${conid[$conidNum]}
sed -i ‘/BOOTPROTO/d‘ ${conid[$conidNum]}
sed -i ‘/IPADDR/d‘ ${conid[$conidNum]}
sed -i ‘/NETMASK/d‘ ${conid[$conidNum]}
sed -i ‘/GATEWAY/d‘ ${conid[$conidNum]}
sed -i ‘/DNS1/d‘ ${conid[$conidNum]}
sed -i ‘/DNS2/d‘ ${conid[$conidNum]}
#add new config
echo "ONBOOT="$onboottxt >>${conid[$conidNum]}
echo "BOOTPROTO="$bprototxt >>${conid[$conidNum]}
echo "IPADDR="$ipaddr >>${conid[$conidNum]}
echo "NETMASK="$netmask >>${conid[$conidNum]}
echo "GATEWAY="$gateway >>${conid[$conidNum]}
echo "DNS1="$dns1 >>${conid[$conidNum]}
if test -z "$dns2"
then
$dns2="Null"
else
echo "DNS2="$dns2 >>${conid[$conidNum]}
fi
#echo "已完成修改!!! 正在重启服务。。"
echo "Modifications are complete!!! service reatst。。"
#service network restart
service network restart
echo "-------Print ifconfig-------"
ifconfig
echo "-------Print ifconfig-------"
#echo "任意键退出。"
echo "Any key to Exit。"
read -n 1
exit
===============================================================
后记:能够3行解决的问题写了好多好多的行。在centos6.5上跑了一下,能够满足需求。第一次写shell脚本,才发现原来英文信息才是“最简单的”。
初见shell,设置网卡参数