这就是我目前正在使用的,以将我的内部LAN IPv4/IPv6地址抓取到我的每个双IP堆栈变量中:
# This is a one liner(-ish) to get my LAN_IPv4 and IPv6 addresses:
# Begin
$ipv4 = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile |
Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress
$ipv6 = Get-NetIPAddress -AddressFamily IPv6 -InterfaceIndex $(Get-NetConnectionProfile |
Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress
echo IPv4: $ipv4
echo IPv6: $ipv6
# Will be using this for external WAN IP (Below)
# [System.Net.Dns]::GetHostEntry('my.ddnshost.tld')
字符串
我能想到的获取WAN IPv4和IPv6子网的唯一方法是使用DNS主机记录“A”和“AAAA”
Working Local IPv4 and IPv6 address
#BEGIN
$ipv4 = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile |
Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress
$ipv6 = Get-NetIPAddress -AddressFamily IPv6 -InterfaceIndex $(Get-NetConnectionProfile |
Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress
#Use DNS to Resolve WAN_IPv4 and WAN_IPv6 via [Systems.Net.Dns]
$ddns_ipv4 = ([System.Net.DNS]::GetHostAddresses('ipv4.ddns.tld') |
Where-Object {$_.AddressFamily -eq "InterNetwork"} | select-object IPAddressToString)[0].IPAddressToString
$ddns_ipv6 = ([System.Net.DNS]::GetHostAddresses('ipv6zone.ddns.tld') |
Where-Object {$_.AddressFamily -eq "InterNetwork"} | select-object IPAddressToString)[0].IPAddressToString
#$ddns_ipv4 = [System.Net.Dns]::GetHostEntry('my.ipv4zone.ddns.tld')
echo LAN-IPv4:" "$ipv4
echo WAN-IPv4:" "$ddns_ipv4
echo LAN-IPv6:" "$ipv6
echo WAN-IPv6:" "$ddns_ipv6
型
输出组件:
LAN-IPv4: 172.16.12.10
WAN-IPv4: 151.14.98.172
LAN-IPv6: fe80::325:be11:5aaa:2faa%7 2a00:22c3:113e:31aa:ffff:::31b3
WAN-IPv6: 2a00:22c3:113e:31aa:ffff:::31b3
型
什么将是非常有帮助的是,如果有一个更好的方法来获得这个信息内的一个变量中使用的Powershell这是更简单,不依赖于我的脚本,无论是ping/ DNS递归查找从动态DNS提供商。
旁注:我一直在玩cURL(对于WAN端查找,在我的DynDNS A和AAAA记录上使用相同的方法),到目前为止,还没有完全设法将curl的输出返回到PowerShell $IP_Var解决方案还没有让我陶醉。
--必须有一种简单的方法来实现LAN/WAN端IPv4/6查找,所有这些都是在Powershell中完成的,而不必依赖于DDNS,而是直接从网络接口/路由表中完成的,比如查询 nbtstat -an 或类似的东西.欢迎任何建议。谢谢!
```lang-powershell-5.1
Working Local IPv4 and IPv6 address
#BEGIN
$ipv4 = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile |
Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress
$ipv6 = Get-NetIPAddress -AddressFamily IPv6 -InterfaceIndex $(Get-NetConnectionProfile |
Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress
#Use DNS to Resolve WAN_IPv4 and WAN_IPv6 via [Systems.Net.Dns]
$ddns_ipv4 = ([System.Net.DNS]::GetHostAddresses('ipv4.ddns.tld') |
Where-Object {$_.AddressFamily -eq "InterNetwork"} | select-object IPAddressToString)[0].IPAddressToString
$ddns_ipv6 = ([System.Net.DNS]::GetHostAddresses('ipv6zone.ddns.tld') |
Where-Object {$_.AddressFamily -eq "InterNetwork"} | select-object IPAddressToString)[0].IPAddressToString
#$ddns_ipv4 = [System.Net.Dns]::GetHostEntry('my.ipv4zone.ddns.tld')
echo LAN-IPv4:" "$ipv4
echo WAN-IPv4:" "$ddns_ipv4
echo LAN-IPv6:" "$ipv6
echo WAN-IPv6:" "$ddns_ipv6
型
输出组件:
LAN-IPv4: 172.16.12.10
WAN-IPv4: 151.14.98.172
LAN-IPv6: fe80::325:be11:5aaa:2faa%7 2a00:22c3:113e:31aa:ffff:::31b3
WAN-IPv6: 2a00:22c3:113e:31aa:ffff:::31b3
型
1条答案
按热度按时间chy5wohz1#
因为只有路由器知道广域网IP地址,而且路由器实际上是你的DNS服务器,所以它永远不需要通知你的计算机广域网IP地址是什么。广域网IP地址超出了你的本地计算机需要知道的范围。
这是你需要的脚本,用它就行了.
字符串
根据评论更新
要执行IP地址检索并定时更新表单,您可以使用
System.Windows.Forms.Timer
。要扩展脚本以将WAN IP地址结果传输到JSON键/值格式,您可以修改Update-IPAddresses
函数,以创建JSON对象并将其写入文件或根据需要处理它。我还在表单中包含了Last Updated标签下面是更新后的脚本:型