PowerShell中常用的網絡命令: 等效IPConfig、PING 和 NSLookup


如何你經常使用 Windows,或多或少可能在 cmd 中使用過 IPConfig、Ping 和 NSLookup 這 3 個非常常用的網絡命令,其實在 Powershell 中也有等效的網絡命令可供使用。

IPConfig

IPConfig 命令用來查看 Windows 中每個網卡的 TCP/IP 配置信息,在 PowerShell 中有如下 2 條命令與其功能類似:

PS C:\> Get-NetIPConfiguration  | select InterfaceIndex, InterfaceAlias, IPv4Address, InterfaceDescription
InterfaceIndex InterfaceAlias             IPv4Address         InterfaceDescription
-------------- --------------             -----------         --------------------
            23 vEthernet (Default Switch) {XXX.XXX.XXX.XXX}   Hyper-V Virtual Ethernet Adapter
             3 Ethernet 3                 {XXX.XXX.XXX.XXX }  Cisco AnyConnect Secure Mobility Client Virtual Miniport Adapter for Windows x64
             8 Wi-Fi                      {192.168.3.8}       Intel(R) Dual Band Wireless-AC 8265
            14 Ethernet                   {XXX.XXX.XXX.XXX}   Intel(R) Ethernet Connection (4) I219-LM


PS C:\> Get-NetIPAddress -AddressFamily IPv4 | select InterfaceIndex,InterfaceAlias, IPv4Address,  PrefixLength
InterfaceIndex InterfaceAlias              IPv4Address     PrefixLength
-------------- --------------              -----------     ------------
            23 vEthernet (Default Switch)  XXX.XXX.XXX.XXX           28
             3 Ethernet 3                  XXX.XXX.XXX.XXX           19
            11 Local Area Connection* 2    XXX.XXX.XXX.XXX           16
             9 Local Area Connection* 1    XXX.XXX.XXX.XXX           16
            14 Ethernet                    XXX.XXX.XXX.XXX           16
             8 Wi-Fi                       192.168.3.8               24
             1 Loopback Pseudo-Interface 1 127.0.0.1                  8

Get-NetIPConfiguration 可以直接獲取當前計算機中每塊網卡的TCP/IP配置信息,Get-NetIPAddress 用於獲取當前計算機的所有IP地址配置信息。

Ping

PowerShell 可以使用 Test-ConnectionTest-NetConnection 獲取與 Ping 命令等效的網絡連接診斷信息,它們可以在 DNS 查詢到 IP 地址之后進行 TCP 連接測試,並輸出測試結果。Test-NetConnection 還可以使用 -TraceRoute 參數獲取路由的路徑和跳數,該功能相當於 tracert 命令。

PS C:\> Test-Connection www.baidu.com | ft -AutoSize

Source     Destination   IPV4Address   IPV6Address Bytes Time(ms)
------     -----------   -----------   ----------- ----- --------
XXXXXXXXXX www.baidu.com 180.101.49.12             32    12
XXXXXXXXXX www.baidu.com 180.101.49.12             32    12
XXXXXXXXXX www.baidu.com 180.101.49.12             32    12
XXXXXXXXXX www.baidu.com 180.101.49.12             32    26


PS C:\> Test-NetConnection www.baidu.com

ComputerName           : www.baidu.com
RemoteAddress          : 180.101.49.11
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.3.8
PingSucceeded          : True
PingReplyDetails (RTT) : 12 ms

PS C:\> Test-NetConnection www.baidu.com -TraceRoute

ComputerName           : www.baidu.com
RemoteAddress          : 180.101.49.11
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.3.8
PingSucceeded          : True
PingReplyDetails (RTT) : 11 ms
TraceRoute             : 192.168.3.1
                         192.168.1.1
                         0.0.0.0
                         61.152.11.1
                         0.0.0.0
                         0.0.0.0
                         58.213.94.82
                         0.0.0.0
                         58.213.96.110
                         0.0.0.0
                         0.0.0.0
                         0.0.0.0
                         0.0.0.0
                         180.101.49.11

NSLookup

Powershell 中可以使用 Resolve-DnsName 來等效 NSLookup 進行 DNS 名稱查詢,與 NSLookup 交互式使用方法不同的是,Resolve-DnsName 可以直接跟像 -Server 這樣的參數來指定要查詢的 DNS 服務器,非常方便。

PS C:\> Resolve-DnsName www.baidu.com

Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
www.baidu.com                  CNAME  698   Answer     www.a.shifen.com

Name       : www.a.shifen.com
QueryType  : A
TTL        : 163
Section    : Answer
IP4Address : 180.101.49.11


Name       : www.a.shifen.com
QueryType  : A
TTL        : 163
Section    : Answer
IP4Address : 180.101.49.12


Name                   : a.shifen.com
QueryType              : SOA
TTL                    : 110
Section                : Authority
NameAdministrator      : baidu_dns_master.baidu.com
SerialNumber           : 2007260007
TimeToZoneRefresh      : 5
TimeToZoneFailureRetry : 5
TimeToExpiration       : 2592000
DefaultTTL             : 3600



PS C:\> Resolve-DnsName www.baidu.com -Server 8.8.8.8

Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
www.baidu.com                  CNAME  272   Answer     www.a.shifen.com
www.a.shifen.com               CNAME  272   Answer     www.wshifen.com

Name       : www.wshifen.com
QueryType  : A
TTL        : 162
Section    : Answer
IP4Address : 104.193.88.123


Name       : www.wshifen.com
QueryType  : A
TTL        : 162
Section    : Answer
IP4Address : 104.193.88.77


Name                   : wshifen.com
QueryType              : SOA
TTL                    : 243
Section                : Authority
NameAdministrator      : baidu_dns_master.baidu.com
SerialNumber           : 2007230001
TimeToZoneRefresh      : 60
TimeToZoneFailureRetry : 30
TimeToExpiration       : 2592000
DefaultTTL             : 3600

更多參考

  1. Basic networking powershell cmdlets cheatsheet to replace netsh, ipconfig, nslookup and more
  2. Superping – powershell test-netconnection


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM