#!/bin/bash
A="IPADDR.*"
B="NETMASK.*"
C="GATEWAY.*"
D="DNS1.*"
E="/etc/sysconfig/network-scripts/ifcfg-eth1"
F="cat $E | sed -n '/BOOTP/p'"
if [ ! "$F" = "BOOTPROTO=dhcp" ]
then
read -t 30 -p "Please enter the IP address:" IP
read -t 30 -p "Please enter mask:" MASK
read -t 30 -p "Please enter gateway:" GW
read -t 30 -p "Please enter DNS address:" DNS
if [ ! -z "$IP" -a ! -z "$MASK" -a ! -z "$GW" -a ! -z "$DNS" ]
then
sed -i "s/$A/IPADDR=$IP/" $E
sed -i "s/$B/NETMASK=$MASK/" $E
sed -i "s/$C/GATEWAY=$GW/" $E
sed -i "s/$D/DNS1=$DNS/" $E
sed -i 's/ONBOOT.*/ONBOOT=yes/' $E
else
echo "Error:The input is invalid!!!!"
exit 1
fi
echo "Please later....."
/etc/init.d/network restart &>/dev/null
echo "Network card has been configured successfully"
else
echo "You are already enabled for DHCP"
exit 2
fi
这是一个简单的设置静态IP的shell脚本,和大家分享