From b575173558c3065e2f1c453caae5ec0433bdfb25 Mon Sep 17 00:00:00 2001 From: sky Date: Fri, 13 Dec 2024 19:46:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20RMxxx=5Frgmii=5Ftoolkit.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RMxxx_rgmii_toolkit.sh | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/RMxxx_rgmii_toolkit.sh b/RMxxx_rgmii_toolkit.sh index b0b789f..3c96f15 100644 --- a/RMxxx_rgmii_toolkit.sh +++ b/RMxxx_rgmii_toolkit.sh @@ -434,6 +434,38 @@ WantedBy=multi-user.target" > "$cfun_service_path" fi } +# Function to change hostname +change_hostname() { + # 显示当前主机名 + current_hostname=$(cat /etc/hostname) + echo -e "\e[1;32mCurrent hostname: $current_hostname\e[0m" + + # 提示用户输入新主机名 + echo -e "\e[1;32mEnter new hostname (or 'exit' to cancel):\e[0m" + read new_hostname + + if [ "$new_hostname" = "exit" ]; then + echo -e "\e[1;31mHostname change cancelled.\e[0m" + return + fi + + # 验证主机名格式 + if ! echo "$new_hostname" | grep -qE '^[a-zA-Z0-9-]+$'; then + echo -e "\e[1;31mInvalid hostname. Hostname can only contain letters, numbers, and hyphens.\e[0m" + return + fi + + # 修改主机名 + remount_rw + echo "$new_hostname" > /etc/hostname + sed -i "s/127.0.1.1.*$/127.0.1.1\t$new_hostname/g" /etc/hosts + hostname "$new_hostname" + remount_ro + + echo -e "\e[1;32mHostname has been changed to: $new_hostname\e[0m" + echo -e "\e[1;33mPlease reboot the device for the change to take full effect.\e[0m" +} + ##################################################################### # SimpleAdmin Functions ##################################################################### @@ -979,7 +1011,8 @@ display_main_menu() { echo -e "\e[92m11) Install Speedtest.net CLI app (speedtest command)\e[0m" # Light Green echo -e "\e[92m12) Install Fast.com CLI app (fast command)(tops out at 40Mbps)\e[0m" # Light Green echo -e "\e[92m13) Install OpenSSH Server\e[0m" # Light Green - echo -e "\e[93m14) Exit\e[0m" # Yellow + echo -e "\e[92m14) Change Hostname\e[0m" # Light Green + echo -e "\e[93m15) Exit\e[0m" # Yellow } # Main program loop @@ -1024,7 +1057,8 @@ main() { 11) install_speedtest_cli;; 12) install_fast_cli;; 13) install_sshd;; - 14) + 14) change_hostname;; + 15) echo -e "\e[1;32mGoodbye!\e[0m" break ;;