Powershell 我想获取默认网关的前3个八位字节
这个命令提供了整个ip,比如Powershell 我想获取默认网关的前3个八位字节,powershell,Powershell,这个命令提供了整个ip,比如192.168.1.1 我对192.168.1感兴趣。一些字符串操作可以做到: $g = (Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | Sort-Object metric1 | select nexthop, metric1, interfaceindex).ne
192.168.1.1
我对
192.168.1感兴趣。
一些字符串操作可以做到:
$g = (Get-WmiObject -Class Win32_IP4RouteTable |
where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} |
Sort-Object metric1 |
select nexthop, metric1, interfaceindex).nexthop
一些字符串操作可以做到这一点:
$g = (Get-WmiObject -Class Win32_IP4RouteTable |
where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} |
Sort-Object metric1 |
select nexthop, metric1, interfaceindex).nexthop
其他解决方案
$ip=(Get-WmiObject -Class Win32_IP4RouteTable | where {'0.0.0.0' -in ( $_.destination, $_.mask)}).nexthop
# method 1
($ip -split "\.")[0..2] -join "."
其他解决方案
$ip=(Get-WmiObject -Class Win32_IP4RouteTable | where {'0.0.0.0' -in ( $_.destination, $_.mask)}).nexthop
# method 1
($ip -split "\.")[0..2] -join "."
无法使用remove,因为有些网关可能类似于10.10.x.y
-replace'\d+$'
$addr.Split('.)[0..2]-join'.
无法使用remove,因为有些网关可能类似于10.10.x.y-replace'\d+$。
$addr.Split('.[0..2]-join'.