Palo Alto Network VPN监视Powershell脚本

时间:2019-01-24 21:13:59

标签: powershell prtg

我无法从PowerShell脚本中获取所需的输出,我想输出数十个站点到站点VPN隧道的活动状态或非活动状态。

我正在尝试修改脚本,该脚本位于http://www.hospitableit.com/howto/monitoring-an-ipsec-tunnel-on-a-palo-alto-firewall-using-prtg/,该脚本监视单个站点到站点VPN隧道,因此使用该传感器的PRTG:https://www.paessler.com/manuals/prtg/exe_script_advanced_sensor将监视尽可能多的脚本

$SecurePassword = Get-Content "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\vpnmonitor_password.txt <file:///C:/Program%20Files%20(x86)/PRTG%20Network%20Monitor/Custom%20Sensors/EXEXML/vpnmonitor_password.txt> " | ConvertTo-SecureString
$Marshal = [System.Runtime.InteropServices.Marshal]
$Bstr = $Marshal::SecureStringToBSTR($SecurePassword)
$Password = $Marshal::PtrToStringAuto($Bstr)
$Marshal::ZeroFreeBSTR($Bstr)

$VPNTunnelState = & 'C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\curl.exe' -k -X GET "https://hostname/api/?type=op&cmd=<show><running><tunnel><flow><all></all></flow></tunnel></running></show>&key=$Password" | Out-File vpnmonitor.xml
[xml]$XmlDocument = Get-Content -Path .\vpnmonitor.xml
$a = $XmlDocument.SelectNodes('//entry')

foreach($Node in $a) {
if($Node.state -eq 'active') {
    $NodeState = "1"
                }
          Else {
          $NodeState = "0"
          }
}

Write-Host "<prtg>"
Write-Host "<result>"
           "<channel>"+ $a.name +"</channel>"
           "<value>" + $NodeState +"</value>"
           "</result>"
Write-Host "</prtg>" 

$ a显示有关所有VPN隧道的所有信息。

$ a.name和$ a.state也一样。

如果我执行$ node或$ nodestate,它将始终显示来自最后一个VPN隧道的信息。

一个VPN隧道的信息如下(一个示例):

peerip : xxx.xxx.xxx.xxx
name : VPN_tunnel_name
outer-if : ethernet1/1
gwid: 26
localip: xxx.xxx.xxx.xxx
state: active
inner-if: tunnel.31
mon: off
owner : 1
id : 34

PRTG显示包含所有VPN隧道名称和最后一个($ nodestate)状态的通道名称。

我可能不得不更改最后一部分,所以我对每个隧道都单独进行操作,如果没有其他选择,这对我来说很好,但是我确实尝试过

Write-Host "<prtg>"
Write-Host "<result>"
           "<channel>"+ $a.name[28] +"</channel>"
           "<value>" + $a.state[28] +"</value>"
           "</result>"
Write-Host "</prtg>" 

我在PRTG中收到XML解析器错误,指出JSON与预期的结构(无效的JSON)不匹配。

$ nodestate [28]不显示任何内容,只要显示1(活动)或0(无效),一切都可能工作。

我很乐意提供其他信息。

0 个答案:

没有答案