修改网卡设备名和配置IP地址(脚本)

5 篇文章 0 订阅
#!/bin/bash
#**************************************************
#Author:        sunxiaoshen
#Version:		1.0
#QQ:            2964238553
#Date:          2023-08-30
#FileName:      network.sh
#Description:   The test script
#Copyright (C):    2023 All rights reserved
#**************************************************

function name_ipaddress
{
    function configuration_ipaddress        # Configuration ip address and Modify connection name.
	{
        echo -e "\n开始配置 `echo eth$((number - 1))` 的IP地址\n"
	    read -p "请输入一个合法的IP地址(0.0.0.0/0): " IP
	    read -p "请输入一个合法的网关地址(0.0.0.0): " GATEWAY
	    read -p "请输入一个合法的DNS地址(0.0.0.0): " DNS
	    if [ ! -z "$IP" -a ! -z "$GATEWAY" -a ! -z "$DNS" ];then
            nmcli connection modify uuid $uuid ifname "`echo eth$((number - 1))`" con-name "`echo eth$((number - 1))`" ipv4.addresses $IP ipv4.gateway $GATEWAY ipv4.dns $DNS ipv4.method manual connection.autoconnect yes
	    elif [ -z "$GATEWAY" -a -z "$DNS" ];then
	        nmcli connection modify uuid $uuid ifname "`echo eth$((number - 1))`" con-name "`echo eth$((number - 1))`" ipv4.addresses $IP ipv4.method manual connection.autoconnect yes
	    elif [ -z "$GATEWAY" ];then
            nmcli connection modify uuid $uuid ifname "`echo eth$((number - 1))`" con-name "`echo eth$((number - 1))`" ipv4.addresses $IP ipv4.dns $DNS ipv4.method manual connection.autoconnect yes
	    elif [ -z "$DNS" ];then
	        nmcli connection modify uuid $uuid ifname "`echo eth$((number - 1))`" con-name "`echo eth$((number - 1))`" ipv4.addresses $IP ipv4.gateway $GATEWAY ipv4.method manual connection.autoconnect yes
        else
	        echo "输入错误,程序退出!"
            exit
	    fi
	   	echo -e "\n`echo eth$((number - 1))` 的IP地址配置完毕!\n"
	}

	function modify_network_name            # Modify the name of the device.
	{
	    cd /etc/udev/rules.d/ && touch 70-custom-ifnames.rules
	    numbers=`lshw -C network | grep serial | wc -l`
	    mac=`lshw -C network | grep serial | sed -nr 's/.* (.*)/\1/p'`
	    for ((number=1;number<=$numbers;number++));do
	        for macaddress in $mac;do
	            if ! grep -Eoq $macaddress 70-custom-ifnames.rules;then
	                echo SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$macaddress\",ATTR{type}==\"1\",NAME=\"eth$((number - 1))\" >> 70-custom-ifnames.rules
                    uuid=$(nmcli connection show | tail -n +2 | sed -n "`echo $number`p" | grep -Eo "[a-z0-9]*-[a-z0-9]*" | tr -d '\n')
                    configuration_ipaddress
                break           # Notice here: That after calling configuration_ipaddress;The break terminates the program and exits the macaddress loop.
	            fi
	        done
	    done
	}

	if [ ! -e /etc/udev/rules.d/70-custom-ifnames.rules ];then
        modify_network_name
    else
        configuration_ipaddress
    fi
    echo -e "\n重启后生效所有配置, 启动后请使用新配置的IP地址实现SSH登录; 正在重启, 请稍等......\n"
    reboot
}
name_ipaddress
[root@localhost ~]# chmod +x network.sh
[root@localhost ~]# ./network.sh

开始配置 eth0 的IP地址

请输入一个合法的IP地址(0.0.0.0/0): 192.1.1.123/8
请输入一个合法的网关地址(0.0.0.0): 192.1.1.200
请输入一个合法的DNS地址(0.0.0.0): 180.76.76.76

eth0 的IP地址配置完毕!

重启后生效所有配置, 启动后请使用新配置的IP地址实现SSH登录; 正在重启, 请稍等......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值