在网络诊断和系统管理中,快速获取设备的 IP 地址是一项常见且重要的任务。本文将介绍跨平台查看 IP 地址的方法,并提供一个实用的脚本,帮助您同时获取内网和外网 IP 地址。
(图片来源网络,侵删)
#技术分享查找以 inet 开头的行,例如:
inet 192.168.1.100/24 dev eth0这里的 192.168.1.100 就是您的 局域网 IP(内网 IP) 。
方法2:使用传统 ifconfig 命令ifconfig查找活跃接口(如 eth0 、wlan0 或 macOS 的 en0 、en1 )下的 inet 地址。
方法3:查看公网IPcurl ifconfig.mecurl ipinfo.io/ipcurl api.ipify.orgC ️ macOS 系统由于 macOS 不包含 ip 命令,我们建议使用以下方法:
方法1:使用 ifconfig 命令ifconfig查找你的网络接口(如 en0 或 en1 ),找到类似下面的行:
inet 192.168.1.100 netmask 0xffffff00 broadcast 192.168.1.255方法2:直接获取指定接口的IPipconfig getifaddr en0方法3:查看公网IPcurl ifconfig.mecurl ipinfo.io/ip Windows 系统方法1:使用 ipconfigipconfig# 仅显示IPv4地址ipconfig | findstr IPv4查找对应网络适配器下的"IPv4 地址"项。
方法2:查看公网IP在 PowerShell 中:
(Invoke-WebRequest -Uri "https://api.ipify.org").Content在 CMD 中:
curl ifconfig.me 内网IP vs 公网IP:理解差异内网IP :在局域网内使用的私有地址,通常格式为:10.x.x.x172.16.x.x - 172.31.x.x192.168.x.x公网IP :互联网上的唯一标识,由ISP分配 一键获取内网和外网IP的脚本下面是一个跨平台的 Bash 脚本,可以同时显示内网和外网 IP 地址:
#!/bin/bashecho "==================================" echo " IP 地址信息查询" echo "=================================="OS="" case "$(uname -s)" in Linux*) OS="Linux" ;; Darwin*) OS="macOS" ;; CYGWIN*|MINGW*|MSYS*) OS="Windows" ;; *) OS="UNKNOWN" ;; esacecho "系统类型: $OS" echo "----------------------------------"echo "内网 IP 地址:" if [[ "$OS" == "Linux" || "$OS" == "macOS" ]]; then if command -v ip &> /dev/null; then ip -4 addr show | grep -oP '(?<=inet\s)\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 命令" fiecho "----------------------------------"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" fiecho "=================================="简化版本(仅显示IP,无错误处理)#!/bin/bashecho "内网 IP:" hostname -I 2>/dev/null || ip -4 addr show 2>/dev/null | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v "127.0.0.1" | head -n1echo "外网 IP:" curl -s ifconfig.me echo 使用方法将上述脚本保存为 get_ip.sh给予执行权限:chmod +x get_ip.sh运行脚本:./get_ip.sh️ 各系统安装必要工具C macOSbrew install curlLinux (Ubuntu/Debian)sudo apt updatesudo apt install curl iproute2C Linux (CentOS/RHEL)sudo yum install curl iprouteWindows安装 Git Bash 或 WSL或使用PowerShell版本:Shell PowerShell 版本对于 Windows 用户,这里是一个 PowerShell 脚本:
# Get-IPInfo.ps1Write-Host "==================================" -ForegroundColor Green Write-Host " IP 地址信息查询" -ForegroundColor Green Write-Host "==================================" -ForegroundColor GreenWrite-Host "内网 IP 地址:" -ForegroundColor Yellow Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.IPAddress -ne '127.0.0.1'} | Format-Table InterfaceAlias, IPAddress -AutoSizeWrite-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/bashget_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 '(?<=inet\s)\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/^[ \t]*//;s/[ \t]*$//' | while read line; do echo " $line" done else public_ip=$(curl -s ifconfig.me) echo " IP: $public_ip" fi echo "==============================================" }get_network_info 实际应用场景网络故障排查 :快速确认设备网络连接状态服务器配置 :获取服务器IP用于服务配置远程访问 :确定需要连接的IP地址网络拓扑分析 :了解设备在网络中的位置⚠️ 注意事项公网IP查询需要网络连接某些网络环境可能屏蔽外部IP查询服务内网IP在重启网络服务或DHCP续租后可能变化生产环境中建议使用多个IP查询服务以提高可靠性转载请注明来自海坡下载,本文标题:《ip批量查询的方法有哪些(一键获取设备IP地址内网与外网IP的完整指南)》
京公网安备11000000000001号
京ICP备11000001号
还没有评论,来说两句吧...