最近觉得每次做实验装虚拟机都要配置一次网络还有yum源那些东西,很繁琐,所以将那些操作全部写成了一个交互式的脚本

其他的就不多说了,直接上代码吧

#!/bin/bash

############User testing###############

ID=`id -u`

if [ ! $ID -eq 0 ] ; then

echo "This script must be run by root."

exit 1

fi


#############Start installing####################

echo "Installing software..."

mount /dev/sr0 /mnt &>/dev/null

read -p "input the software you want to install:" WARE

function INSTALL ()

{

yum install -y $WARE > /dev/null

}

if [ ! -e $WARE ] ; then

if [ -f  /etc/yum.repos.d/local.repo ]; then

for i in $WARE;do

INSTALL $i

done

else

echo "[base]

name=base

baseurl=file:///mnt

enabled=1

gpgcheck=0"> /etc/yum.repos.d/local.repo

for i in $WARE;do

INSTALL $i

done

fi

fi


##############Network setting##################

echo -e "\nSetting network..."

DW=`route -n | grep ^0 | awk '{print $2}'`

ifconfig | grep eth | awk '{print $1}' | while read i

do

IP=`ifconfig | grep -A1 "$i" | awk '{print $2}' | awk -F":" '{print $2}' | grep "^[0-9]"`

MASK=`ifconfig | grep -A1 "$i" | awk '{print $4}' | awk -F":" '{print $2}' | grep "^[0-9]"`

echo "The current Network devices are $i,IP is $IP,Netmask is $MASK,the defatlt gateway is $DW."

done

while true

do

read -p "The Network device to interface(like eth0):" DEV

if echo $DEV | grep "eth[0-9]\{1,\}"

then

break

fi

echo "please check device format..."

done

select j in "DHCP" "STATIC"

do

case "$j" in

DHCP)

echo -e "DEVICE="$DEV"\nONBOOT=yes\nNM_CONTROLLED=no\nBOOTPROTO=dhcp" > /etc/sysconfig/network-scripts/ifcfg-$DEV

break;;

STATIC)

while true

do

read -p "please input IP(like 0.0.0.0):" IP

if echo $IP | grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"

then

echo -e "DEVICE="$DEV"\nONBOOT=yes\nNM_CONTROLLED=no\nBOOTPROTO=none\nIPADDR="$IP"" > /etc/sysconfig/network-scripts/ifcfg-$DEV

break

fi

echo "please check IP format..."

done

while true

do

read -p "please input Netmask(like 0.0.0.0):" NM

if echo $NM | grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"

then

echo -e "NETMASK="$NM"" >> /etc/sysconfig/network-scripts/ifcfg-$DEV

break

fi

echo "please check Netmask format..."

done

while true

do

read -p "please input Gateway(like 0.0.0.0):" GW

if echo $GW | grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"

then

echo -e "GATEWAY="$GW"" >> /etc/sysconfig/network-scripts/ifcfg-$DEV

break

fi

echo "please check Gateway format..."

done

break;;

esac

done

/sbin/service network restart &>/dev/null