Question NCAT 7.4 not returning expected values in Windows 7 Pro 64-bit ?

Nov 23, 2022
7
0
10
Attempting to automatically monitor a network device using an old W7 box (no monitor or keyboard) with the intent to send an email using smtpMail if a connection goes MIA

Using a variant of NetCat in Linux returns Zero with a connection and non-Zero when not connected ... attempting this in W7 with Ncat 7.4 always returns 0 ... attempts to install the latest versions of NMAP/NCAT in W7 (7.92 and 7.93) fail ... ping is too wordy and arp -a 192.168.0.100 appears to always show a value (cached ? )

Are there other ways to programmatically determine if a device has disappeared from a network?

Thanks in advance for any help you can provide

from Linux 10 buster (pi 4b) this shell script works

if nc -zw1 192.168.0.100 80; then
echo "connected"
else
echo "not connected"
fi

from W7pro 64b with ncat 7.4 the following vbs script always returns 0 (ExitCode)

DIM wshShell, oExec

Set wshShell = CreateObject ("WScript.Shell")
Set oExec = wshShell.exec("c:\temp\nmap\ncat.exe -zw1 192.168.0.100 80;")

Wscript.echo(oExec.ExitCode)
 

Ralston18

Titan
Moderator
Two questions:

1) Why is ping being considered as "too wordy"? Is that because it is a continuous ping?

2) How quickly do you want to receive an smtpMail email if the connection goes MIA?

Powershell Test-Connection using the -Quiet parameter could be used in a simple script with the cmdlet's True-False result being used to send the e-mail if the result is False.

Reference:

https://petri.com/powershell-test-connection/

There are other similar links to be found. One or more may serve as a template and modifed to meet your monitoring requirements.
 
Nov 23, 2022
7
0
10
Thank you ... still stuck in the DOS (bat) and or UNIX (sh) worlds ... I know PS exists but have yet to enter that world ... generated the following Powershell script based on your link ... this script appears to work in PowerShell but would need the resulting contest.ps1 file to be called from an AutoHotKey script ... PS run policy is currently set to yes

$a = test-connection 192.168.0.100 -count 1 -quiet
if ($a -eq $True) {
echo "wahoo"
}else{
echo "whoops"
}

1) Ping results are too wordy because there are many variations that would need to be parsed for a go-nogo test (within BAT files) ... nc in UNIX returns a 0 or -1 ... ncat was expected to do the same ... test-connection with -quiet provides the required response but not familiar with mixing PS scripts in the DOS side of Windoze which is where AutoHotKey hangs out

2) the AutoHotKey scripts loop forever and would be expected to test for lost connections once every 5 minutes ... an email would be sent if the state went mia (only one email would be required meaning the AutoHotKey will need to keep track of the last poll status and only send out an email if the connection status changes .... Whoops would Send and URGENT (HI Priority Stating a device has gone missing )... a Wahoo result would send a normal email stating the connection has been restored)

Basically, AutoHotKey would need to be able to call this PS1 script every 5 minutes

Tried a bat file that calls this ps1 script but it returns gibberish (more like some key AAAAA-AAAAA-AAAAA-AAAAA-AAAAA)

@Echo off
a=c:\temp\contest.ps1
echo %a
b < c:\temp\contest.ps1
echo %b
 

Ralston18

Titan
Moderator
"wahoo/whoops" is a good start. :)

Just do not improvise. Keep it all as close to to the requirements and existing terminology as possible.

E.g. echo "True" if the connection is "true" or "False" if the connection is not found which in turn sends the email. Simplest case but not clear overall.

I would be even more explicit with my echos: "Connected, Devicename -OK " and Not Connected, Devicename down - Email sent".

I would forego mixing in any DOS/bat files. Just do it all in Powershell.

Powershell can easily create timed loops that, in turn, execute some action, function, etc. at a designated interval.

And it is also very straight forward to have Powershell generate and send an email when some IF-THEN-ELSE conditions occur. Tied to the True/False Condition for simplicity but echoed as above for readability and display purposes. The variable being $a perhaps which should be either True or False.

After the echo "No Connection Found" then send the email as the next cmdlet or maybe a function.

Google both topics: timer loops and sending emails. - There are multiple links, tutorials, and examples to be found.

= = = =

Not sure that I understand the requirement for Auto Hot Key.

Powershell scripts can be launched at startup, launched by a desktop icon, the task bar, or triggered via Task Scheduler.

== = =

Noted "contest".

Question: Are you actually trying to discover when some website connection becomes available in order to send a participating contest email of some sort?

That is the opposite of monitoring for a device going down and/or no longer connected.

Very likely, if so, that such contest websites have measures in place to prevent automatic monitoring. Simply by turning off/disabling any responses to a ping attempt. No True or False.

Just FYI.
 
Nov 23, 2022
7
0
10
The explicatives in my example are just a form of; FOO, BLAH, and or FUBAR ... there was no intent for real-life use

IMHO, AutoHotKey fits a KISS mantra specifically for use in controlling unattended Windows Machines

I have a number of unattended machines (no keyboard/mouse) running several off-the-shelf unrelated applications ... AutoHotKey provides a way to glue the functionality of these apps together plus ensure they play nicely together

AutoHotkey API has built-in functions that directly access the Win32 API (opens the door to window's background messaging without having to write some complicated function in some high-level language like some variant of C to access anything Win32)

The AutoHotKey functions I use specifically provide a way to; determine the active window, ensure all expected apps are running, click on unaccessible controls (vender restricted/hidden/garbled controls), resize and position windows, and remove any unwanted window aberrations

PowerShell may be able to do any or all of this ... but ... at this point, I would rather not have to rewrite/convert working scripts just because of this one feature that only appears to be offered in PowerShell

Haven't found a PowerShell example that shows how to retrieve a return value when run from a BAT shell script ... there are plenty of examples showing how to run a PS1 script from BAT ... perhaps PowerShell was never intended to return values? The ps1 scripts return values within PowerShell but seemingly these return values are NOT returned when the PowerShell is closed unlike a cmd shell

all of these examples return the same meaningless string of 25 characters when run from inside a BAT script

powershell -noprofile -executionpolicy remotesigned -file <path>\ttt.ps1

powershell -noprofile -ExecutionPolicy Bypass -Command "<path>\ttt.ps1"

.\ttt.ps1

Thinking I may just have to resort to an old kludgy file technique ... if and when a file exists or disappears, AutoHotKey can send an email notification. Get the ps1 script to either write or delete a unique file depending on the Connection result
 
Nov 23, 2022
7
0
10
Reread your last 2 comments ... the ttt.ps1 or contest.ps1 scripts are ONLY monitoring local INTRANET IP's nothing Cloud based ... I have local devices that are highly susceptible to electrical noise (things like electric motors can introduce noise on power lines which in turn can disrupt cheap IOT devices requiring them to be rebooted) ... right now, even though these devices aren't critical, they are visible which means people start screaming at me when they notice they have failed ... this exercise will hopefully enable me to be ahead of the game so to speak. I recently deployed some ESP32 devices that allow me to remotely reboot some of these devices ... now I need a better way to determine when to reboot instead of either waiting for SCREAMS or having to periodically go and manually query the health of all devices.
 
Attempting to automatically monitor a network device using an old W7 box (no monitor or keyboard) with the intent to send an email using smtpMail if a connection goes MIA

Using a variant of NetCat in Linux returns Zero with a connection and non-Zero when not connected ... attempting this in W7 with Ncat 7.4 always returns 0 ... attempts to install the latest versions of NMAP/NCAT in W7 (7.92 and 7.93) fail ... ping is too wordy and arp -a 192.168.0.100 appears to always show a value (cached ? )

Are there other ways to programmatically determine if a device has disappeared from a network?

Thanks in advance for any help you can provide

from Linux 10 buster (pi 4b) this shell script works

if nc -zw1 192.168.0.100 80; then
echo "connected"
else
echo "not connected"
fi

from W7pro 64b with ncat 7.4 the following vbs script always returns 0 (ExitCode)

DIM wshShell, oExec

Set wshShell = CreateObject ("WScript.Shell")
Set oExec = wshShell.exec("c:\temp\nmap\ncat.exe -zw1 192.168.0.100 80;")

Wscript.echo(oExec.ExitCode)


Why not just set up Mikrotiks "The Dude" on that box, it will alert you of anything dropping off the network and act as a syslog server
 
  • Like
Reactions: Ralston18

Ralston18

Titan
Moderator
Do not resort to kludgy. Go forward.

Not a AutoKey user so I will defer to others regarding Autokey's features, functions, and viable use within your environment.

Powershell can easily check for the presence or absence of a file. You can even create an editable list of files to be checked.

That said, I would be generally leery of using the presence of absence of a file to predict incoming problems.

Regarding: "Haven't found a PowerShell example that shows how to retrieve a return value when run from a BAT shell script "

Most likely the BAT would need to be run within a PS script and the BAT's return value captured by PS as a string then parsed to extract the needed information.

Parsing is sometimes more art than science - still there are some very powerful tools to do parsing and extracting discovered data.

And, again, likely doable completely within Powershell without any need for BATs.

= = = =

Took a quick look at "The Dude" per @nigelivey - seems promising but my sense is that it is just a user interface applying commands and/or script you could directly do yourself.

Again doing your own script is likely to be simpler and more within your control. Plus free if you DIY and subsequently need to do some later tweaks.

Consider Get-ComputerInfo and apply to remote computers.

The less reliance on third party tools the better.

Out of curiousity: what are the top 5 precursors that one of those "cheap IOT devices" has dropped off the network or needs to be rebooted?

So that you would be emailed and/or otherwise notified before the screaming starts.....
 
Nov 23, 2022
7
0
10
Is this maybe TERMINOLOGY? ... PowerShell forums claim that PS provides for "EXITING" using an EXIT code

Modified PowerShell code

$ip = $args[0]

$rslt = test-connection $ip -count 1 -quiet

if ($rslt -eq $True) {

if ( test-path c:\test\nc ) {
del c:\test\nc
touch c:\test\cn
exit 0
}

if ( test-path c:\test\cn ) {
} else {
touch c:\test\cn
exit 0
}

} else {

if ( test-path c:\test\cn ) {
del c:\test\cn
touch c:\test\nc
exit 1
}

if ( test-path c:\test\nc ) {
}else {
touch c:\test\nc
exit 1
}

}


But the following batch file returns "Processing" which results in "Not Connected" response for all tests

@Echo off
@for /f %%r in ('powershell -noprofile -executionpolicy bypass -file "c:\test\ttt.ps1 %1"') do @set "rslt=%%r"

@Echo %rslt%

@if %rslt% == 0 (
@Echo Connected
) else (
@Echo Not Connected
)
 
Nov 23, 2022
7
0
10
SOLVED !

Thanks everyone for your input (s)

- while "The Dude" monitoring software is free, Mikrotiks appears to require a proprietary piece of licensed hardware

- I pretty much live on my PC with my email client doing pops every three minutes, a five-minute warning (read email notification) will undoubtedly beat any screaming 99.99% of the time and with my installed ESP32 devices it is short work to remotely kick these devices back online. (ESP32 devices with the simple addition of a relay can be used to reboot a device by interrupting one of its DC power wires) ... recently found pre-packaged Wifi remote AC switches (fully expect will use the same technology) these pre-packaged products would also work with the added bonus of not having to engineer and manufacture something on your own which is what I did.

- after many hours of beating the ps1 & bat script combinations to death with no working solution ... my conclusion from this exercise is that although the ps1 script is correctly returning an exit value to PowerShell, the PowerShell shell is NOT passing that value on to the BAT script ... AND ... There-in lies the Solution!

SOLUTION: (Call "test-connection" directly as a PowerShell command instead of embedded in a ps1 script)

for this solution the ps1 script is no longer required ... only requires a simple BAT file as follows:

@Echo off
@foR /f %%r in ('powershell -noprofile -executionpolicy bypass -command try {test-connection %1 -count 1 -quiet} catch {"PowerShell Error"}') do @set x=%%r

@if "%x%" == "True"
@Echo CN
) else (
@Echo NC
)

Within an AutoHotKey script, I can now call this BAT script every five minutes and if the previously returned value doesn't match have it send me a notification email

Hope this solution can be useful to others struggling with a BAT calling PowerShell scenario
 
SOLVED !

Thanks everyone for your input (s)

- while "The Dude" monitoring software is free, Mikrotiks appears to require a proprietary piece of licensed hardware

- I pretty much live on my PC with my email client doing pops every three minutes, a five-minute warning (read email notification) will undoubtedly beat any screaming 99.99% of the time and with my installed ESP32 devices it is short work to remotely kick these devices back online. (ESP32 devices with the simple addition of a relay can be used to reboot a device by interrupting one of its DC power wires) ... recently found pre-packaged Wifi remote AC switches (fully expect will use the same technology) these pre-packaged products would also work with the added bonus of not having to engineer and manufacture something on your own which is what I did.

- after many hours of beating the ps1 & bat script combinations to death with no working solution ... my conclusion from this exercise is that although the ps1 script is correctly returning an exit value to PowerShell, the PowerShell shell is NOT passing that value on to the BAT script ... AND ... There-in lies the Solution!

SOLUTION: (Call "test-connection" directly as a PowerShell command instead of embedded in a ps1 script)

for this solution the ps1 script is no longer required ... only requires a simple BAT file as follows:

@Echo off
@foR /f %%r in ('powershell -noprofile -executionpolicy bypass -command try {test-connection %1 -count 1 -quiet} catch {"PowerShell Error"}') do @set x=%%r

@if "%x%" == "True"
@Echo CN
) else (
@Echo NC
)

Within an AutoHotKey script, I can now call this BAT script every five minutes and if the previously returned value doesn't match have it send me a notification email

Hope this solution can be useful to others struggling with a BAT calling PowerShell scenario

"The Dude" is completely free and requires no hardware from MikroTik. It will even monitor bandwidth on links
 
Nov 23, 2022
7
0
10
I don't see a standalone version of "The Dude" on their official download site ... the Client is free but their Wiki states the Client requires a Server ... AND ... the Server requires some specific hardware (NOT FREE) ... perhaps you have a standalone version? If so please provide a link to the installation files. OR share what you are using for your FREE server ... I am already using a BOX for monitoring not expecting to have to add yet another box to provide this feature.