shell实现将动态ip修改为静态ip,静态ip修改为其它静态ip

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/eagle89/article/details/82023675

 shell实现将动态ip修改为静态ip,静态ip修改为其它静态ip
(1) 将动态ip修改为静态ip的思路
   ① 修改ip之前,先备份ifcfg-eth0文件
   ②  进入网卡配置目录  /etc/sysconfig/network-scripts/ifcfg-eth0
   ③  修改BOOTPROTO=static
   ④  修改ONBOOT=yes
   ⑤  配置静态ip地址 IPADDR
   ⑥  配置DNS  NETMASK
   ⑦  配置网关  GATEWAY
   ⑧  重启网卡验证配置,验证配置结果   service   network  restart
(2) 将静态ip修改为其它静态ip的思路
   ☆  先判断是否是静态ip   grep "dhcp" /etc/sysconfig/network-scripts/ifcfg-eth0/
   ☆  
         if[$?  -ne   0]; then
            sed -i `s/^IPADDR/#IPADDR/g` ifcfg-eth0
            read  -p   "please Enter  ip:"IPADDR
            echo "IPADDR="$IPADDR">>/etc/sysconfig/network-scripts/
         fi
  ☆  重启网卡验证配置结果   service network  restart
代码示例:

#!/bin/bash
#2018年08月14日
#change ip  shell
NET_FILE="/etc/sysconfig/network-scripts"
NET_DIR="ifcfg-eth0"
cd $NET_FILE/
#change ip static for static:
grep "dhcp" $NET_DIR
if [ $? -ne 0 ];then
sed -i s/^IPADDR/#IPADDR/g $NET_DIR
read -p "Please enter ip Address,example 192.168.0.11 ip:"  IPADDR
echo "IPADDR=$IPADDR">>$NET_DIR
echo "NETMASK=255.255.255.0">>$NET_DIR
echo "GATEWAY=192.168.2.254">>$NET_DIR
service network restart
else

#change ip dhcp for static
sed -i s/dhcp/static/g $NET_DIR
sed -i s/ONBOOT=no/ONBOOT=yes/g $NET_DIR
read -p "Please enter ip Address,example 192.168.0.11 ip:"   IPADDR
cat>>$NET_FILE/$NET_DIR <<EOF
IPADDR=$IPADDR
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
EOF
service network restart
fi

或者


#!/bin/bash
#by author daqi
#change ip  shell
NET_FILE="/etc/sysconfig/network-scripts"
NET_DIR="ifcfg-eth0"
cd $NET_FILE/
#change ip static for static:
grep "dhcp" $NET_DIR
if [ $? -ne 0 ];then
sed -i s/^IPADDR/#IPADDR/g $NET_DIR
#read -p "Please enter ip Address,example 192.168.0.11 ip:"  IPADDR
echo "IPADDR=$10.30.8.80">>$NET_DIR
echo "NETMASK=255.255.255.0">>$NET_DIR
 echo "GATEWAY=192.168.2.254">>$NET_DIR
service network restart
else
#change ip dhcp for static
sed -i s/dhcp/static/g $NET_DIR
sed -i s/ONBOOT=no/ONBOOT=yes/g $NET_DIR
read -p "Please enter ip Address,example 192.168.0.11 ip:"   IPADDR
cat>>$NET_FILE/$NET_DIR <<EOF
IPADDR=$IPADDR
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
EOF
service network restart
fi

自动配IP脚本

#!/bin/bash
#自动配IP
# Author: tianzhaogao (E-mail: 296862380@qq.coom)
 
line_num=$(cat /etc/sysconfig/network-scripts/ifcfg-eth0 | wc -l)
 
while [ "$line_num" -gt 7 ]
    do
        sed -i '8c \' /etc/sysconfig/network-scripts/ifcfg-eth0
        line_num=$(cat /etc/sysconfig/network-scripts/ifcfg-eth0 | wc -l)
    done
     
read -t 30 -p "Please input IP:" ipnum
 
n1=$(echo "$ipnum" | cut -d "." -f 1)
n2=$(echo "$ipnum" | cut -d "." -f 2)
n3=$(echo "$ipnum" | cut -d "." -f 3)
n4=$(echo "$ipnum" | cut -d "." -f 4)
 
if [ "$n1" -le 255 -a "$n2" -le 255 -a "$n3" -le 255 -a "$n4" -le 255 ];then
    echo -e IPADDR="$ipnum""\n"NETMASK=255.255.255.0"\n"GETWAY="$n1"\."$n2"\."$n3"\.1"\n"IPV6INIT=no"\n"USERCTL=no >> /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i 's/ONBOOT="no"/ONBOOT="yes"/g' /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=none/g' /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
    service network restart
fi

展开阅读全文

没有更多推荐了,返回首页