您的位置:首页 > 运维架构 > Shell

shell脚本-快速配置网卡参数

2011-05-20 11:35 477 查看
闲来无事,想想做测试的时候配置IP地址很麻烦,写了个简单的脚本。自己觉得还是比较烂吧,能用就行。

#!/bin/bash

#config network value by simple style.

#the system version is RHEL5.6

intdir=/etc/sysconfig/network-scripts

tempfile=/tmp/netconfig.tmp.txt

read -p "which interface you want to config?($(ls ${intdir} | grep ifcfg | cut -d- -f2 | grep -v lo) or? ): " interface

IPADDR=`cat $intdir/ifcfg-${interface} | grep IPADDR | cut -d= -f2`

NETMASK=`cat $intdir/ifcfg-${interface} | grep NETMASK | cut -d= -f2`

GATEWAY=`cat $intdir/ifcfg-${interface} | grep GATEWAY | cut -d= -f2`

function viewconfig

{

echo "current config is:"

echo "IPADDR=$IPADDR"

echo "NETMASK=$NETMASK"

echo "GATEWAY=$GATEWAY"

read -p "Are you sure?(y/n):" confirm

}

viewconfig

while [ $confirm != y ]

do

#read value

read -p "please input a value IPv4 address: " IPADDR

read -p "please input a value netmask: " NETMASK

read -p "please input a value IPv4 address of the default gateway: " GATEWAY

cat $intdir/ifcfg-${interface} | grep -v IPADDR | grep -v NETMASK | grep -v GATEWAY | grep -v BOOTPROTO > $tempfile

viewconfig

if [ $confirm != y ]

then continue

fi

done

#load config

echo "BOOTPROTO=static" >> $tempfile

echo "IPADDR=$IPADDR" >> $tempfile

echo "NETMASK=$NETMASK" >> $tempfile

echo "GATEWAY=$GATEWAY" >> $tempfile

cat $tempfile > ${intdir}/ifcfg-${interface}

rm -rf $tempfile

read -p "Do you want to config the DNS Server address?(y/n)" confirm

if [ $confirm = y ]

then

read -p "please input a IPv4 address of first dnsserver: " nameserver1

echo "nameserver $nameserver1" > /etc/resolv.conf

read -p "Do you want to config the second DNS Server address?(y/n)" confirm

if [ $confirm = y ]

then

read -p "please input a IPv4 address of second dnsserver: " nameserver2

echo "nameserver $nameserver2" >> /etc/resolv.conf

fi

fi

/sbin/ifdown $interface

/sbin/ifup $interface

#by Kaiser

本文出自 “凯旋之歌” 博客,请务必保留此出处http://kaiserwu.blog.51cto.com/697390/569438
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: