获取 IPv4 地址

huangapple go评论82阅读模式
英文:

Get IP v4 address

问题

使用以下命令获取IP地址,我已经使用-Match,因为有时它是Ethernet或Ethernet0、Ethernet1,我想要排除所有以127.0.0.1或169*开头的网络适配器,你能帮我实现吗?谢谢。

Get-NetIPAddress -AddressFamily IPv4 | where Interfacealias -Match 'Ethernet'

只获取一个IP,而不是127和169。

英文:

i get the IP address with the following command, i've putted -Match because sometimes it's Ethernet or Ethernet0, Ethernet1, i would like to excluse every network adapteur who come with ip LIke 127.0.0.1 or 169* in my following where, can you help me to do that ? thanks.

Get-NetIPAddress -AddressFamily IPv4 | where Interfacealias -Match 'Ethernet'

get only 1 IP, not 127 and 169

答案1

得分: 2

保留地址空间中的地址,例如本地回环(127.0.0.0/8)和链路本地地址(169.254.0.0/16),将标记为PrefixOrigin: WellKnown - 您可以使用这个来过滤它们:

$actualAddresses = Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet | Where PrefixOrigin -ne WellKnown

如果您只想要第一个可用的结果,请使用Select-Object -First 1

$firstActualAddress = Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet | Where PrefixOrigin -ne WellKnown | Select-Object -First 1
英文:

Addresses from the reserved parts of the address space - like the localhost loopback (127.0.0.0/8) and link-locals (169.254.0.0/16) - will be marked with PrefixOrigin: WellKnown - you can use this to filter them out:

$actualAddresses = Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet |Where PrefixOrigin -ne WellKnown

If you want only the first available result, use Select-Object -First 1:

$firstActualAddress = Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet |Where PrefixOrigin -ne WellKnown |Select-Object -First 1

huangapple
  • 本文由 发表于 2023年3月15日 20:52:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定