当前位置: > shell编程 >

linux shell脚本配置IP的实现代码

时间:2015-01-14 12:47来源:linux.it.net.cn 作者:IT
分享一例shell脚本,实现linux ip地址的程序化配置,包括dhcp自动获取ip地址与手动配置ip地址二种情况。

linux shell脚本: 
 

 

#!/bin/bash
# 配置ip地址脚本

fun0 () {
        ipfile="/etc/sysconfig/network-scripts/ifcfg-eth0"
        hwaddr=`ifconfig |grep eth0 |awk -F " " '{print $5}'`
        device=`ifconfig |grep eth0 |awk -F " " '{print $1}'`
        type=`ifconfig |grep eth0 |awk -F ":" '{ print $2 }'|awk -F " " '{
print $1 }'`
        echo "DEVICE=$device" > $ipfile
        echo "HWADDR=$hwaddr" >>$ipfile
        echo "ONBOOT=yes" >>$ipfile
        echo "TYPE=$type" >>$ipfile
}
fun1 () {
         echo "Enter the IP that you want to set:"
        read IP
        echo "Enter the netmask:"
        read netmask
        echo "Enter the gateway:"
        read gateway
        echo "Enter the DNS:"
        read dns
}
echo "Enter the IP model you want to set (DHCP/STATIC):"
read model
if [ $model = "DHCP" ]
then
        fun0
        echo "BOOTPROTO=dhcp" >>$ipfile
        service network restart
elif [ $model = "STATIC" ]
then
        fun1
        fun0
        echo "BOOTPROTO=none" >>$ipfile
        echo "NETMASK=$netmask" >>$ipfile
        echo "IPADDR=$IP" >>$ipfile
        echo "GATEWAY=$gateway" >>$ipfile
        echo "$dns" > /etc/resolv.conf
        service network restart

else
        echo "error:please enter DHCP or STATIC"
        exit 0
fi

 


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容