#!/bin/bash # 判断命令是否存在 command_exists() { command -v "$@" >/dev/null 2>&1 } # 获取服务器的公网IP地址 get_Internal_IP() { local Internal_IP="" local Network_Manage_Tool="" if command_exists ip; then Network_Manage_Tool=$(ip addr) Ethernet_Interface_Name=$(echo "$Network_Manage_Tool" | grep -Eo "eth([0-9]{1,3})|eno([0-9]{1,3})|ens([0-9]{1,3})" | head -n 1) echo "正在使用ip addr命令获取内网的IP地址:" Internal_IP=$(ip addr show ${Ethernet_Interface_Name} | grep 'inet ' | head -1 | awk '{print $2}' | cut -f1 -d'/') elif command_exists ifconfig; then Network_Manage_Tool=$(ifconfig) Ethernet_Interface_Name=$(echo "$Network_Manage_Tool" | grep -Eo "eth([0-9]{1,3})|eno([0-9]{1,3})|ens([0-9]{1,3})" | head -n 1) echo "正在使用ifconfig命令获取内网的IP地址:" Internal_IP=$(ifconfig ${Ethernet_Interface_Name} | grep 'inet ' | head -1 | awk '{print $2}' | cut -f1 -d'/') fi echo "" echo "$Internal_IP" echo "" } get_Internal_IP