本文目录导读:

根据您提到的“内部地址范围”,通常是指企业内网的私有IP地址段(如10.0.0.0/8、172.16.0.0/12、192.168.0.0/16)或内部DNS/路由信息,以下是几种常见场景的脚本获取方法:
通过本机网络接口获取内网IP
Linux/Mac (Bash)
#!/bin/bash
# 获取所有非回环接口的IP地址(排除docker等虚拟接口)
ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1'
# 更精确的方法(只保留物理接口)
ifconfig | grep -E "inet " | grep -v 127.0.0.1 | awk '{print $2}'
Windows (PowerShell)
# 获取所有启用的网络适配器IPv4地址
Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -notlike '*Loopback*'} | Select-Object IPAddress
# 更详细的输出(包含子网掩码)
Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -notmatch 'Loopback|Bluetooth|vEthernet'} | Format-Table IPAddress,PrefixLength
扫描内网地址段
Python 3 (快速扫描活跃主机)
#!/usr/bin/env python3
import subprocess
import ipaddress
def scan_subnet(subnet):
"""扫描指定子网,返回活跃IP列表"""
network = ipaddress.ip_network(subnet, strict=False)
active_hosts = []
for ip in network.hosts():
# 使用ping测试(Linux/Windows自适应)
response = subprocess.call(
['ping', '-c', '1', '-W', '1', str(ip)] if __import__('sys').platform != 'win32'
else ['ping', '-n', '1', '-w', '1000', str(ip)],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
if response == 0:
active_hosts.append(str(ip))
return active_hosts
# 使用示例:扫描192.168.1.0/24
subnet = "192.168.1.0/24"
print(f"扫描子网 {subnet} 的活跃主机...")
hosts = scan_subnet(subnet)
print(f"发现 {len(hosts)} 个活跃主机:")
for host in hosts:
print(host)
获取路由器/网关地址
Bash (Linux)
# 获取默认网关
ip route | grep default | awk '{print $3}'
# 或使用更精确的方法
route -n | grep 'UG[ \t]' | awk '{print $2}'
PowerShell (Windows)
# 获取默认网关 (Get-NetRoute -DestinationPrefix '0.0.0.0/0').NextHop
全网段自动发现(nmap方式)
#!/bin/bash
# 需要安装nmap: sudo apt-get install nmap
# 扫描常见内网段
for subnet in "192.168.0.0/24" "192.168.1.0/24" "10.0.0.0/24"; do
echo "正在扫描 $subnet ..."
nmap -sn $subnet --exclude 192.168.1.1 2>/dev/null | grep "Nmap scan report for" | awk '{print $5}'
done
# 如果没有nmap,可以用arp扫描
arp -a | grep -E '^192\.168\.|^10\.|^172\.(1[6-9]|2[0-9]|3[01])\.' | awk '{print $1}'
从DHCP服务器获取信息
读取DHCP租约文件 (Linux)
# 常见DHCP租约文件位置
if [ -f /var/lib/dhcp/dhcpd.leases ]; then
grep "lease " /var/lib/dhcp/dhcpd.leases | awk '{print $2}'
fi
# 或使用dhcpdump工具(需要安装)
sudo dhcpdump -i eth0 2>/dev/null | grep "YOUR_CLIENT_IP" | awk '{print $2}'
常用组合脚本(获取本机所有内网信息)
Bash (Linux)
#!/bin/bash
echo "=== 本机内网信息 ==="
echo "IP地址: $(hostname -I | awk '{print $1}')"
echo "子网掩码: $(ip -4 addr show | grep inet | grep -v 127.0.0.1 | awk '{print $2}' | cut -d'/' -f2)"
echo "默认网关: $(ip route | grep default | awk '{print $3}')"
echo "DNS服务器: $(grep nameserver /etc/resolv.conf | awk '{print $2}')"
echo ""
echo "=== ARP表中的内网主机 ==="
arp -a | grep -E '\(192\.168\.|\(10\.|\(172\.' | awk '{print $2}' | tr -d '()'
PowerShell (Windows)
Write-Host "=== 本机内网信息 ===" -ForegroundColor Green
$network = Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -eq "Up"}
$network | ForEach-Object {
$ip = $_.IPv4Address.IPAddress
$mask = $_.IPv4Address.PrefixLength
$gateway = $_.IPv4DefaultGateway.NextHop
Write-Host "IP地址: $ip / $mask"
Write-Host "默认网关: $gateway"
Write-Host "DNS服务器: $($_.DNSServer.ServerAddresses -join ', ')"
}
Write-Host "`n=== ARP表中的内网主机 ===" -ForegroundColor Green
Get-NetNeighbor | Where-Object {$_.IPAddress -match '^192\.168\.|^10\.|^172\.(1[6-9]|2[0-9]|3[01])\.'} | Format-Table IPAddress,LinkLayerAddress,State -AutoSize
注意事项
- 扫描内网时注意权限(部分命令需要root/管理员权限)
- 频繁扫描可能引起网络设备报警或性能问题
- 动态IP环境(DHCP)建议定期更新缓存
- 虚拟化环境(Docker、VMware)可能产生多个虚拟网段
根据具体场景选择合适的脚本,如果需要获取特定设备(如路由器、打印机)的地址范围,建议结合SNMP或设备API查询。