如何使用Powershell将WAN IPv4和IPv6地址放入变量中,而不是依赖外部A & AAAA DynDNS区域

gcuhipw9  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(117)

这就是我目前正在使用的,以将我的内部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

chy5wohz

chy5wohz1#

因为只有路由器知道广域网IP地址,而且路由器实际上是你的DNS服务器,所以它永远不需要通知你的计算机广域网IP地址是什么。广域网IP地址超出了你的本地计算机需要知道的范围。
这是你需要的脚本,用它就行了.

Add-Type -AssemblyName System.Windows.Forms

# Retrieve WAN IP addresses
$wanIPv4 = Invoke-RestMethod -Uri 'http://api.ipify.org'
$wanIPv6 = Invoke-RestMethod -Uri 'http://api64.ipify.org'

# Create the form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "WAN IP Addresses"
$Form.Size = New-Object System.Drawing.Size(300,200)
$Form.StartPosition = "CenterScreen"

# Create the label for IPv4
$LabelIPv4 = New-Object System.Windows.Forms.Label
$LabelIPv4.Location = New-Object System.Drawing.Point(10,10)
$LabelIPv4.Size = New-Object System.Drawing.Size(280,20)
$LabelIPv4.Text = "WAN IPv4: $wanIPv4"

# Create the label for IPv6
$LabelIPv6 = New-Object System.Windows.Forms.Label
$LabelIPv6.Location = New-Object System.Drawing.Point(10,40)
$LabelIPv6.Size = New-Object System.Drawing.Size(280,20)
$LabelIPv6.Text = "WAN IPv6: $wanIPv6"

# Add the labels to the form
$Form.Controls.Add($LabelIPv4)
$Form.Controls.Add($LabelIPv6)

# Show the form
$Form.ShowDialog()

# RUN: powershell -ExecutionPolicy ByPass -File hello.ps1

字符串

根据评论更新

要执行IP地址检索并定时更新表单,您可以使用System.Windows.Forms.Timer。要扩展脚本以将WAN IP地址结果传输到JSON键/值格式,您可以修改Update-IPAddresses函数,以创建JSON对象并将其写入文件或根据需要处理它。我还在表单中包含了Last Updated标签下面是更新后的脚本:

Add-Type -AssemblyName System.Windows.Forms
Import-Module PowerShellGet

# Function to update IP addresses, convert to JSON, and update time
function Update-IPAddresses {
    $global:wanIPv4 = Invoke-RestMethod -Uri 'http://api.ipify.org'
    $global:wanIPv6 = Invoke-RestMethod -Uri 'http://api64.ipify.org'
    $LabelIPv4.Text = "WAN IPv4: $global:wanIPv4"
    $LabelIPv6.Text = "WAN IPv6: $global:wanIPv6"

    # Update the last updated label
    $global:LastUpdatedLabel.Text = "Last Updated: " + (Get-Date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss") + " UTC"

    # Convert to JSON
    $ipInfo = @{
        WANIPv4 = $global:wanIPv4
        WANIPv6 = $global:wanIPv6
    }

    $json = $ipInfo | ConvertTo-Json
    Write-Output $json
    # Optional: Write to file
    # $json | Out-File "path\to\your\file.json"
}

# Create the form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "WAN IP Addresses"
$Form.Size = New-Object System.Drawing.Size(320,250)
$Form.StartPosition = "CenterScreen"

# Create labels
$LabelIPv4 = New-Object System.Windows.Forms.Label
$LabelIPv4.Location = New-Object System.Drawing.Point(10,10)
$LabelIPv4.Size = New-Object System.Drawing.Size(300,20)

$LabelIPv6 = New-Object System.Windows.Forms.Label
$LabelIPv6.Location = New-Object System.Drawing.Point(10,40)
$LabelIPv6.Size = New-Object System.Drawing.Size(300,20)

$global:LastUpdatedLabel = New-Object System.Windows.Forms.Label
$global:LastUpdatedLabel.Location = New-Object System.Drawing.Point(10,70)
$global:LastUpdatedLabel.Size = New-Object System.Drawing.Size(300,20)

# Add the labels to the form
$Form.Controls.Add($LabelIPv4)
$Form.Controls.Add($LabelIPv6)
$Form.Controls.Add($global:LastUpdatedLabel)

# Timer setup
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 300000 # 5 minutes in milliseconds
$Timer.add_Tick({ Update-IPAddresses })
$Timer.Start()

# Initial update
Update-IPAddresses

# Show the form
$Form.ShowDialog()

# RUN: powershell -ExecutionPolicy ByPass -File hello.ps1

相关问题