PRTG Script: Remote PortPing

This script allows you to see if a machine can see yet another remote machine, on a given port.

Say you want to see if “Server1” can see port 1234 open on “Server2”. This might be very different that checking if your PRTG probe can see Server2 on port 1234, certainly if its on a different subnet would complex routing and firewall policies in the middle.

You can either run a PRTG probe on “Server1” – this is not a good idea and does not scale well at all.

Or, you can run this script.

Pre-Reqs:

You need “Server1” to have valid windows credentials (explicitly set, or inherited) inside of PRTG. To test this you need things like WMI calls working (eg the CPU usage monitor)

Get the code below, put it in a powershell file, save it to the custsom exe sensors folder. Then add a new custom exe sensor inside of PRTG. Select the script and use the following parameters.

Usage:

-UserName %windowsdomain\%windowsuser -Password “%windowspassword” -LocalHost %host -RemoteHost xxx.xxx.xxx.xxx -RemotePort yyy

The windowsdomain, username, password and host all point back to PRTG variables.

The RemoteHost (xxx.xxx.xxx.xxx) and RemotePort(yyy) need to be set.

Param(
[string]$LocalHost,
[string]$RemoteHost,
[string]$RemotePort,
[string]$Username,
[string]$Password
)
 
#$LocalHost
#$RemoteHost
#$RemotePort
#$Username
#$Password
 
#$hostaddr = "google.com"
#$port = 800
$SecPassword = $Password | ConvertTo-SecureString -AsPlainText -force
$cred = new-object -typename System.Management.Automation.PSCredential ($Username, $SecPassword)
 
$ret = invoke-command -computer $Localhost -Credential $cred -ScriptBlock {
 param($RemoteHost2, $RemotePort2)
 $tcp = new-object system.net.sockets.tcpclient
 Try
 {
 $tcp.connect($RemoteHost2,$RemotePort2)
 write-host "1:OK"
 }
 Catch
 {
 write-host "0:Cannot Connect"
 }
 Finally
 {
 $tcp.Dispose()
 }
} -ArgumentList $RemoteHost, $RemotePort # end script block
$ret

Leave a comment