
在网络诊断和系统管理中,快速获取设备的 IP 地址是一项常见且重大的任务。本文将介绍跨平台查看 IP 地址的方法,并提供一个实用的脚本,协助您同时获取内网和外网 IP 地址。
ip a
ip -4 addr show
hostname -I
#技术分享查找以 inet 开头的行,例如:
inet 192.168.1.100/24 dev eth0
这里的 192.168.1.100 就是您的 局域网 IP(内网 IP) 。
ifconfig
查找活跃接口(如 eth0 、wlan0 或 macOS 的 en0 、en1 )下的 inet 地址。
curl ifconfig.me
curl ipinfo.io/ip
curl api.ipify.org
由于 macOS 不包含 ip 命令,我们提议使用以下方法:
ifconfig
查找你的网络接口(如 en0 或 en1 ),找到类似下面的行:
inet 192.168.1.100 netmask 0xffffff00 broadcast 192.168.1.255
ipconfig getifaddr en0
curl ifconfig.me
curl ipinfo.io/ip
ipconfig
# 仅显示IPv4地址
ipconfig | findstr IPv4
查找对应网络适配器下的"IPv4 地址"项。
在 PowerShell 中:
(Invoke-WebRequest -Uri "https://api.ipify.org").Content
在 CMD 中:
curl ifconfig.me
下面是一个跨平台的 Bash 脚本,可以同时显示内网和外网 IP 地址:
#!/bin/bash
echo "==================================" echo " IP 地址信息查询" echo "=================================="
OS="" case "$(uname -s)" in Linux*) OS="Linux" ;; Darwin*) OS="macOS" ;; CYGWIN*|MINGW*|MSYS*) OS="Windows" ;; *) OS="UNKNOWN" ;; esac
echo "系统类型: $OS" echo "----------------------------------"
echo "内网 IP 地址:" if [[ "$OS" == "Linux" || "$OS" == "macOS" ]]; then if command -v ip &> /dev/null; then ip -4 addr show | grep -oP '(?<=inets)d+(.d+){3}' | grep -v "127.0.0.1" | while read ip; do interface=$(ip -4 addr show | grep -B1 "$ip" | head -n1 | awk '{print $2}' | tr -d ':') echo " $interface: $ip" done elif command -v ifconfig &> /dev/null; then ifconfig | grep -E "inet [0-9]+.[0-9]+.[0-9]+.[0-9]+" | grep -v "127.0.0.1" | awk '{print " "$2}' else echo " 无法获取内网 IP:未找到 ip 或 ifconfig 命令" fi else echo " Windows 系统请使用 ipconfig 命令" fi
echo "----------------------------------"
echo "外网 IP 地址:" if command -v curl &> /dev/null; then services=("ifconfig.me" "ipinfo.io/ip" "api.ipify.org" "icanhazip.com") for service in "${services[@]}"; do public_ip=$(curl -s --connect-timeout 5 "https://$service") if [[ -n "$public_ip" && "$public_ip" =~ ^[0-9]+.[0-9]+.[0-9]+.[0-9]+$ ]]; then echo " $public_ip (来源: $service)" break fi done if [[ -z "$public_ip" || ! "$public_ip" =~ ^[0-9]+.[0-9]+.[0-9]+.[0-9]+$ ]]; then echo " 无法获取公网 IP:网络连接问题或所有服务均不可用" fi else echo " 无法获取公网 IP:请先安装 curl" fi
echo "=================================="
#!/bin/bash
echo "内网 IP:" hostname -I 2>/dev/null || ip -4 addr show 2>/dev/null | grep -oP '(?<=inets)d+(.d+){3}' | grep -v "127.0.0.1" | head -n1
echo "外网 IP:" curl -s ifconfig.me echo
chmod +x get_ip.sh
./get_ip.sh
brew install curl
sudo apt update
sudo apt install curl iproute2
sudo yum install curl iproute
对于 Windows 用户,这里是一个 PowerShell 脚本:
# Get-IPInfo.ps1
Write-Host "==================================" -ForegroundColor Green Write-Host " IP 地址信息查询" -ForegroundColor Green Write-Host "==================================" -ForegroundColor Green
Write-Host "内网 IP 地址:" -ForegroundColor Yellow Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.IPAddress -ne '127.0.0.1'} | Format-Table InterfaceAlias, IPAddress -AutoSize
Write-Host "外网 IP 地址:" -ForegroundColor Yellow try { $publicIP = (Invoke-WebRequest -Uri "https://api.ipify.org" -UseBasicParsing).Content Write-Host " $publicIP" -ForegroundColor Cyan } catch { Write-Host " 无法获取公网 IP" -ForegroundColor Red }
Write-Host "==================================" -ForegroundColor Green
如果您需要更详细的信息,可以使用这个增强版脚本:
#!/bin/bash
get_network_info() { echo "==============================================" echo " 网络信息详细报告" echo "==============================================" echo "主机名: $(hostname)" echo "操作系统: $(uname -s) $(uname -r)" echo "----------------------------------------------" echo "网络接口详情:" if command -v ip &> /dev/null; then ip -4 addr show | grep -E "^[0-9]+:" | awk '{print $2}' | tr -d ':' | while read iface; do ip_addr=$(ip -4 addr show dev $iface 2>/dev/null | grep -oP '(?<=inets)d+(.d+){3}') if [ -n "$ip_addr" ]; then echo " $iface: $ip_addr" fi done fi echo "----------------------------------------------" echo "默认网关:" if command -v ip &> /dev/null; then ip route | grep default | awk '{print $3 " via " $5}' fi echo "----------------------------------------------" echo "公网 IP 信息:" if command -v curl &> /dev/null; then public_info=$(curl -s "https://ipinfo.io/json") echo "$public_info" | grep -E '"ip":|"city":|"region":|"country":' | sed 's/"/ /g' | sed 's/^[ ]*//;s/[ ]*$//' | while read line; do echo " $line" done else public_ip=$(curl -s ifconfig.me) echo " IP: $public_ip" fi echo "==============================================" }
get_network_info