Files
shell-scripts/基础脚本/获取本机的内网IP.sh
2023-05-15 16:49:09 +08:00

33 lines
1017 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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