• shell基础之更改IP


    要求:

      显示所有网卡及其对应的IP地址,并可更改IP相关内容使其生效。

     1 #!/bin/bash
     2 #获取本机网卡和IP
     3 while :
     4 do
     5 network=$(ifconfig |grep BROAD |awk -F: '{print $1}')
     6 echo -e "本机网卡	IP"
     7 for single_network in $(echo $network);do
     8     all_ip=$(ifconfig $single_network |grep -w "inet" |awk '{print $2}')
     9     echo -e "$single_network		$all_ip"
    10 done
    11 #获取要更改的网卡名称
    12 while :
    13 do
    14 read -p "请输入要更改的网卡名称(默认退出):" new_network
    15     if [ -z $new_network ];then
    16     echo "退出脚本"
    17     exit
    18     else
    19     judge_new_network=`echo "$network" |grep -w "$new_network"`
    20         if [ -z "$judge_new_network" ];then
    21         echo "请输入正确的网卡名!"
    22         else
    23         break
    24         fi
    25     fi    
    26 done    
    27 #要更改的IP,掩码,网关
    28 IPZZ="^([0-9].|[1-9][0-9].|1[0-9][0-9].|2[0-4][0-9].|25[0-5].){3}([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$"
    29 netmask_ZZ="^([0-9].|[1-9][0-9].|1[0-9][0-9].|2[0-4][0-9].|25[0-5].){3}([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"
    30 #输入新的IP地址
    31 while :
    32 do
    33 read -p "请输入新的IP地址:" new_ip
    34     if [ -z $new_ip ];then
    35     echo "IP地址不能为空,请重新输入!"
    36     elif [[ $new_ip =~ $IPZZ ]];then
    37     break
    38     else
    39     echo "请输入正确的IP地址!"
    40     fi
    41 done
    42 #输入掩码
    43 while :
    44 do
    45 read -p "请输入新的掩码(默认为255.255.255.0):" new_netmask
    46     if [ -z $new_netmask ];then
    47     new_netmask=255.255.255.0
    48     break
    49     elif [[ $new_netmask =~ $netmask_ZZ ]];then
    50     break
    51     else
    52     echo "请输入正确的掩码!"
    53     fi
    54 done
    55 #输入网关
    56 while :
    57 do
    58 read -p "请输入新的网关:" new_gateway
    59     if [ -z $new_gateway ];then
    60     echo "网关不能为空,请重新输入!"
    61     elif [[ $new_gateway =~ $IPZZ ]];then
    62     break
    63     else
    64     echo "请输入正确的网关!"
    65     fi
    66 done
    67 #写入配置文件
    68 echo "NAME=$new_network
    69 DEVICE=$new_network
    70 ONBOOT=yes
    71 BOOTPROTO=none
    72 IPADDR=$new_ip
    73 NETMASK=$new_netmask
    74 GATEWAY=$new_gateway
    75 DNS1=114.114.114.114
    76 DNS2=8.8.8.8" > /etc/sysconfig/network-scripts/ifcfg-$new_network
    77 #重启服务
    78 systemctl restart network
    79 done
  • 相关阅读:
    超详细教程2021新版oracle官网下载Windows JAVA-jdk11并安装配置(其他版本流程相同)
    个人总结
    6.15 团队项目心得
    五月团队项目收获
    八大排序算法读书笔记
    设计模式读书笔记3
    设计模式读书笔记2
    结对编程收获
    设计模式读书笔记
    UI-12组结对编程作业总结
  • 原文地址:https://www.cnblogs.com/renyz/p/11305329.html
Copyright © 2020-2023  润新知