#!/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登录; 正在重启, 请稍等......
文章评论