返回顶部

收藏

shell 脚本配置IP

更多
#!/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
#该片段来自于http://outofmemory.cn

标签:shell,系统

收藏

0人收藏

支持

0

反对

0

发表评论