怎么用脚本获取网络适配器设置

wen 实用脚本 2

本文目录导读:

怎么用脚本获取网络适配器设置

  1. Windows系统
  2. Linux系统
  3. 使用建议
  4. 保存脚本

Windows系统

PowerShell脚本(推荐)

# 获取所有网络适配器详细信息
Get-NetAdapter | Select-Object Name, InterfaceDescription, Status, MacAddress, LinkSpeed
# 获取IP配置信息
Get-NetIPAddress | Where-Object {$_.AddressFamily -eq "IPv4"} | 
    Select-Object InterfaceAlias, IPAddress, PrefixLength
# 获取DNS配置
Get-DnsClientServerAddress | Select-Object InterfaceAlias, ServerAddresses
# 完整适配器信息
Get-NetAdapter | ForEach-Object {
    $adapter = $_
    $ipConfig = Get-NetIPAddress -InterfaceIndex $adapter.ifIndex -ErrorAction SilentlyContinue
    $dns = Get-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -ErrorAction SilentlyContinue
    [PSCustomObject]@{
        Name = $adapter.Name
        Status = $adapter.Status
        MAC = $adapter.MacAddress
        IP = ($ipConfig | Where-Object {$_.AddressFamily -eq "IPv4"}).IPAddress -join ", "
        SubnetMask = ($ipConfig | Where-Object {$_.AddressFamily -eq "IPv4"}).PrefixLength
        Gateway = (Get-NetRoute -InterfaceIndex $adapter.ifIndex -DestinationPrefix "0.0.0.0/0").NextHop
        DNS = ($dns.ServerAddresses -join ", ")
    }
} | Format-Table -AutoSize

CMD批处理脚本

@echo off
echo ===== 网络适配器信息 =====
wmic nic get Name, MACAddress, NetEnabled, Speed
echo.
echo ===== IP配置 =====
ipconfig /all
echo.
echo ===== 适配器状态 =====
netsh interface show interface

VBScript

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetEnabled = TRUE")
For Each objAdapter in colAdapters
    WScript.Echo "名称: " & objAdapter.Name
    WScript.Echo "MAC地址: " & objAdapter.MACAddress
    WScript.Echo "速度: " & objAdapter.Speed & " bps"
    WScript.Echo "状态: " & objAdapter.Status
    WScript.Echo "----"
Next

Linux系统

Bash脚本

#!/bin/bash
echo "===== 网络适配器信息 ====="
# 方法1: 使用ip命令(推荐)
echo "--- IP命令输出 ---"
ip addr show | grep -E "^[0-9]:|inet"
# 方法2: 使用ifconfig
echo -e "\n--- ifconfig输出 ---"
ifconfig -a 2>/dev/null
# 方法3: 详细适配器信息
echo -e "\n--- 详细适配器设置 ---"
for iface in $(ls /sys/class/net/); do
    echo "接口: $iface"
    echo "  MAC地址: $(cat /sys/class/net/$iface/address 2>/dev/null)"
    echo "  状态: $(cat /sys/class/net/$iface/operstate 2>/dev/null)"
    echo "  速度: $(cat /sys/class/net/$iface/speed 2>/dev/null) Mbps"
    echo "  IP地址: $(ip -4 addr show $iface | grep -oP '(?<=inet\s)\d+(\.\d+){3}')"
    echo "----"
done

Python脚本(跨平台)

import subprocess
import platform
def get_windows_network_info():
    """Windows网络信息获取"""
    try:
        result = subprocess.run(['ipconfig', '/all'], capture_output=True, text=True)
        print(result.stdout)
    except Exception as e:
        print(f"错误: {e}")
def get_linux_network_info():
    """Linux网络信息获取"""
    try:
        # 使用ip命令
        result = subprocess.run(['ip', 'addr', 'show'], capture_output=True, text=True)
        print(result.stdout)
        # 显示路由表
        route_result = subprocess.run(['ip', 'route'], capture_output=True, text=True)
        print("路由表:")
        print(route_result.stdout)
    except Exception as e:
        print(f"错误: {e}")
def main():
    system = platform.system()
    if system == "Windows":
        get_windows_network_info()
    elif system == "Linux":
        get_linux_network_info()
    else:
        print(f"不支持的系统: {system}")
if __name__ == "__main__":
    main()

使用建议

  1. Windows:推荐使用PowerShell脚本,信息更完整
  2. Linux:推荐使用Bash脚本结合ip命令
  3. 跨平台:使用Python脚本
  4. 管理员权限:某些信息可能需要以管理员/root权限运行

保存脚本

# Windows PowerShell保存为.ps1文件
$script = @'
Get-NetAdapter | Select-Object Name, Status, MacAddress, LinkSpeed
'@
$script | Out-File -FilePath "network_info.ps1"

执行脚本前可能需要设置执行策略:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

抱歉,评论功能暂时关闭!