一键获取设备IP地址:内网与外网IP的完整指南 In 巴西队世界杯名单 @2026-06-26 01:24:03

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

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

🐧 Linux 系统

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

bash

复制代码

# 查看所有网络接口信息

ip a

# 仅查看IPv4地址

ip -4 addr show

# 简洁版:仅显示IP地址

hostname -I

查找以 inet 开头的行,例如:

复制代码

inet 192.168.1.100/24 dev eth0

这里的 192.168.1.100 就是您的局域网IP(内网IP)。

方法2:使用传统 ifconfig 命令

bash

复制代码

ifconfig

查找活跃接口(如 eth0、wlan0 或 macOS 的 en0、en1)下的 inet 地址。

方法3:查看公网IP

bash

复制代码

curl ifconfig.me

# 或

curl ipinfo.io/ip

# 或

curl api.ipify.org

🖥️ macOS 系统

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

方法1:使用 ifconfig 命令

bash

复制代码

ifconfig

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

复制代码

inet 192.168.1.100 netmask 0xffffff00 broadcast 192.168.1.255

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

bash

复制代码

ipconfig getifaddr en0

方法3:查看公网IP

bash

复制代码

curl ifconfig.me

# 或

curl ipinfo.io/ip

󰀳 Windows 系统

方法1:使用 ipconfig

cmd

复制代码

ipconfig

# 仅显示IPv4地址

ipconfig | findstr IPv4

查找对应网络适配器下的"IPv4 地址"项。

方法2:查看公网IP

在PowerShell中:

powershell

复制代码

(Invoke-WebRequest -Uri "https://api.ipify.org").Content

在CMD中:

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地址:

bash

复制代码

#!/bin/bash

# 获取IP地址信息脚本

# 功能:同时显示内网IP和外网IP

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 "----------------------------------"

# 获取内网IP

echo "内网IP地址:"

if [[ "$OS" == "Linux" || "$OS" == "macOS" ]]; then

# Linux/macOS 系统

if command -v ip &> /dev/null; then

# 使用 ip 命令

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 命令

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

# Windows 系统(通过WSL或Cygwin)

echo " Windows系统请使用 ipconfig 命令"

fi

echo "----------------------------------"

# 获取外网IP

echo "外网IP地址:"

if command -v curl &> /dev/null; then

# 尝试多个IP查询服务,提高成功率

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,无错误处理)

bash

复制代码

#!/bin/bash

# 简化版IP查询脚本

echo "内网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 -n1

echo "外网IP:"

curl -s ifconfig.me

echo

📋 使用方法

将上述脚本保存为 get_ip.sh

给予执行权限:

bash

复制代码

chmod +x get_ip.sh

运行脚本:

bash

复制代码

./get_ip.sh

🛠️ 各系统安装必要工具

macOS

bash

复制代码

# 确保已安装curl(通常已预装)

brew install curl # 如果使用Homebrew

Linux (Ubuntu/Debian)

bash

复制代码

sudo apt update

sudo apt install curl iproute2

Linux (CentOS/RHEL)

bash

复制代码

sudo yum install curl iproute

Windows

安装 Git Bash 或 WSL

或使用PowerShell版本:

💡 PowerShell 版本

对于Windows用户,这里是一个PowerShell脚本:

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

🔧 高级功能扩展

如果您需要更详细的信息,可以使用这个增强版脚本:

bash

复制代码

#!/bin/bash

# 增强版IP信息脚本

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 '(?<=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 "----------------------------------------------"

# 获取公网IP及地理位置信息

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查询服务以提高可靠性

鱼不喂食会不会饿死?——鱼缸里的生存奥秘与紧急喂养指南
oppo a59玩游戏好不好,oppo手机扎玩游戏