由于电信一直限制共享,为了破解不得不经常性的改网关。但是手动修改直的很烦,于是写了个shell.
测试系统:CentOS 6
example:
#ipconfig.sh ethx 192.168.1.111
#!/bin/bash
# file--ipconfig.sh
device="$1"
hwaddr=""
bootproto="static"
broadcast=""
ipaddr="$2"
netmask="255.255.255.0"
gateway=""
network=""
onboot="yes"
nm_controlled="yes"
if [ "$device" = "" ] || [ "$ipaddr" = "" ]; then
echo "Usage: $0 \"ethx\" ipaddress"
exit 1
fi
file="/etc/sysconfig/network-scripts/ifcfg-$device"
if [ ! -f "$file" ]; then
touch "$file"
fi
#cp "$file" "$file""_backup"
#echo "$old_gateway"
#value="${old_gateway#*=}"
#echo "value:$value"
device=$(ifconfig "$device" | grep 'HWaddr' | awk '{print $1}')
hwaddr=$(ifconfig "$device" | grep 'HWaddr' | awk '{print $5}')
broadcast=${ipaddr%.*}".255"
gateway=${ipaddr%.*}".1"
network=${ipaddr%.*}".0"
comment=$(grep '#' "$file")
if [ "$comment" = "" ]; then
comment="# Here is the ip configuration file"
fi
cat << !here! > "$file"
$comment
DEVICE=$device
HWADDR=$hwaddr
BOOTPROTO=$bootproto
IPADDR=$ipaddr
NETMASK=$netmask
NETWORK=$network
BROADCAST=$broadcast
GATEWAY=$gateway
NM_CONTROLLED=$nm_controlled
ONBOOT=$onboot
!here!
nameserverfile="/etc/resolv.conf"
if [ ! -f "$nameserverfile" ]; then
touch "$nameserverfile"
fi
chattr -i /etc/resolv.conf
# 这里的nameserver改成离你最近的电信DNS
cat << EOF > /etc/resolv.conf
nameserver 202.101.224.69
nameserver 202.101.226.68
EOF
chattr +i /etc/resolv.conf
ifdown "$device"
ifup "$device"