一键获取设备IP地址:内网与外网IP的完整指南

  • 时间:2025-11-20 20:56 作者: 来源: 阅读:0
  • 扫一扫,手机访问
摘要:在网络诊断和系统管理中,快速获取设备的 IP 地址是一项常见且重大的任务。本文将介绍跨平台查看 IP 地址的方法,并提供一个实用的脚本,协助您同时获取内网和外网 IP 地址。各操作系统查看IP地址的方法 Linux 系统方法1:使用 ip 命令(推荐)ip a ip -4 addr show hostname -I #技术分享查找以 inet 开头的行,例如:inet 192.168.1.1

在网络诊断和系统管理中,快速获取设备的 IP 地址是一项常见且重大的任务。本文将介绍跨平台查看 IP 地址的方法,并提供一个实用的脚本,协助您同时获取内网和外网 IP 地址。

各操作系统查看IP地址的方法

Linux 系统

方法1:使用 ip 命令(推荐)

ip a

ip -4 addr show

hostname -I

#技术分享查找以 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:查看公网IP

curl ifconfig.me

curl ipinfo.io/ip

curl api.ipify.org

C ️ macOS 系统

由于 macOS 不包含 ip 命令,我们提议使用以下方法:

方法1:使用 ifconfig 命令

ifconfig

查找你的网络接口(如 en0 或 en1 ),找到类似下面的行:

inet 192.168.1.100 netmask 0xffffff00 broadcast 192.168.1.255

方法2:直接获取指定接口的IP

ipconfig getifaddr en0

方法3:查看公网IP

curl ifconfig.me

curl ipinfo.io/ip

Windows 系统

方法1:使用 ipconfig

ipconfig

# 仅显示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.x
  • 172.16.x.x - 172.31.x.x
  • 192.168.x.x
  • 公网IP :互联网上的唯一标识,由ISP分配

一键获取内网和外网IP的脚本

下面是一个跨平台的 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 "=================================="

简化版本(仅显示IP,无错误处理)

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

使用方法

  1. 将上述脚本保存为 get_ip.sh
  2. 给予执行权限:
chmod +x get_ip.sh
  1. 运行脚本:
./get_ip.sh

️ 各系统安装必要工具

C macOS

brew install curl

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install curl iproute2

C Linux (CentOS/RHEL)

sudo yum install curl iproute

Windows

  • 安装 Git Bash 或 WSL
  • 或使用PowerShell版本:

Shell PowerShell 版本

对于 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

实际应用场景

  1. 网络故障排查 :快速确认设备网络连接状态
  2. 服务器配置 :获取服务器IP用于服务配置
  3. 远程访问 :确定需要连接的IP地址
  4. 网络拓扑分析 :了解设备在网络中的位置

⚠️ 注意事项

  • 公网IP查询需要网络连接
  • 某些网络环境可能屏蔽外部IP查询服务
  • 内网IP在重启网络服务或DHCP续租后可能变化
  • 生产环境中提议使用多个IP查询服务以提高可靠性
  • 全部评论(0)
手机二维码手机访问领取大礼包
返回顶部