What's happening with NCAT 7.4? It's giving me wrong numbers when I run it on my Windows 7 Pro 64-bit computer?
What's happening with NCAT 7.4? It's giving me wrong numbers when I run it on my Windows 7 Pro 64-bit computer?
Trying to check a network device with an old Windows 7 box (no screen or keyboard) so I can send an email if the connection stops working with smtpMail. Using NetCat on Linux gives zero when connected and something else when it's not ... trying this on Windows 7 with Ncat 7.4 always gives zero ... installing newer versions of NMAP/NCAT in W7 (7.92 and 7.93) fails. Pinging is too long and arp -a 192.168.0.100 seems to show a cached value. Are there other ways to tell programmatically if something disappeared from the network? Thanks for any help you can give me with Linux 10 buster (pi 4b) this shell script works on nc -zw1 192.168.0.100 80; then echo "connected" else echo "not connected" fi but from W7pro 64b with ncat 7.4 the following vbs script always returns zero (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)
Two questions: 1) Why does ping feel like too much jargon? Is it because we keep sending pings over and over? 2) How fast do you want an SMTP email reply if the connection stops or drops suddenly? You can check this with a quick PowerShell command that turns on quiet mode. If that check fails, your script can send an email right away. See the guide at https://petri.com/powershell-test-connection/ for more details and other useful links. Some of them might help you build something just like this to fit your needs.
I am still trapped in the old DOS world and trying to move into the new UNIX world, but I know PS exists. I just haven't found where to enter it yet. Here is a script I made based on your link. It looks like it works in PowerShell right now, but I need to make sure that contest.ps1 file gets called from an AutoHotKey script first.
The settings are all set to yes for the PS run policy:
$a = test-connection 192.168.0.100 -count 1 -quiet if ($a -eq $True) { echo "wahoo" } else{ echo "whoops" }
Ping results are getting too long because there are so many different ways to run a go-nogo test inside BAT files, and ncat in UNIX gives back 0 or -1. The command test-connection with the -quiet flag gives me exactly what I need but isn't easy for me to mix up scripts from PowerShell on the DOS side of Windows, which is where AutoHotKey lives.
The script loops forever until it stops, so it will check for lost connections once every five minutes. An email would be sent if the status changes to missing (only one email needed because AutoHotKey needs to remember the last poll status and only send a message when something new happens). The "Whoops" message would tell you urgently that a device is gone, while a Wahoo result would just say the connection is back. Basically, I need for AutoHotKey to run this PS1 script every five minutes.
I tried calling this PS1 script from a BAT file and it gave me nonsense like some repeating key AAAAA-AAAAA-AAAAA-AAAAA-AAAAA.
"wahoo/whoops" is a decent start, but don't make changes just yet. Stick closely to the rules and what's already there. For example, if you see "True," echo that; otherwise, echo "False." This sends an email based on that result. It's simple but not very clear overall. I'd be clearer: say something like "Connected, DeviceName - OK" when everything is fine, or "Not Connected, DeviceName down" when it fails and send the email. Skip mixing in DOS or BAT files instead of using them. Do all this work with PowerShell commands. PowerShell can easily make timed loops that run specific actions at set times, plus it's easy to trigger an email whenever certain conditions happen (like if-then-else rules). Keeping it simple means checking for True or False values, but echoing those results like above helps show what happened. The variable $a might hold either a True value or a False one. After printing "No Connection Found," send the next command or maybe a function to do the rest of the job. You can find lots of links and examples online on timers and sending emails. There are many options here to explore.
= = = = Not sure if I need to use an auto hotkey. PowerShell scripts can start running when you open them, run from a desktop icon or task bar button, and also come in handy with Task Scheduler. == = = "Contest" mentioned above? Question: Are you trying to figure out when a website connection gets back on so you can send an email about a contest? That's different from watching for a device to go offline or lose its connection. Very probably, if that's the case, those contest websites will block automatic monitoring. They likely have rules in place to stop anyone from automatically checking just by sending pings. No True and no False needed here. Just wanted to share this info.
The little tricks in my example are just one thing called: FOO, BLAH, or FUBAR ... honestly, I never thought they would be used for real life anyway. AutoHotKey is perfect for simple tasks like controlling computers that don't have a keyboard or mouse attached. I have several unattended machines running different apps without any human to watch over them. AutoHotKey makes it easy to connect these apps together and make sure everything works well. The AutoHotKey functions let me access the Windows system API directly, which means I can see messages in the background of windows without writing complicated code in a low-level language like C. The specific functions I use are for: finding out which window is currently open, making sure all needed apps are running, clicking on buttons that are hidden or broken so they don't work (vendors hide those controls), resizing and moving windows, and removing unwanted extra windows. PowerShell could probably do any of these things too... but right now, I'd rather not have to rewrite or convert working scripts just because of one feature that only seems to exist in PowerShell. I haven't found a PowerShell example showing how to get back the answer when it runs from an AutoHotKey script. There are plenty of examples showing how to run a PS1 script from an BAT file... maybe PowerShell was never meant to return values? The ps1 scripts give answers within PowerShell, but those answers aren't returned when the PowerShell program ends up closed unlike a regular CMD shell where all these examples just give the same boring 25-character string every time when run from inside a BAT file with settings like: powershell -noprofile -ExecutionPolicy Bypass -Command "<path>\ttt.ps1" .\ttt.ps1. I think I might have to go back to using an old hacky way of working with files just in case... and when a file appears or disappears, AutoHotKey can send me an email message telling me about the connection result. Get the ps1 script to write or delete a special file based on what happened during that run.
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.
Why don't we just put Mikrotik's "The Dude" thingy on that computer? It'll tell you about any stuff that shows up on the net and work like a log server.
So here is what I found out. There isn't a built-in PowerShell way to catch when a program finishes running from a batch file. That's mostly because those two tools need each other; the batch file gives the result, then PowerShell reads it in like text and turns it into something useful. Sometimes getting that data can feel more like magic tricks than math—it's all about knowing what you're looking for. There are great ways to do this without needing a batch file at all. Also, check out this tool from The Dude by @nigelivey. It looks okay but I think it just lets you type commands and run scripts that can be done yourself easily. If you build your own script, you get more control and free tools if you change things later. Use Get-ComputerInfo to talk to computers on the network instead of relying on other software. The less tools you need is better. Now I'm curious: what are the top 5 items that a cheap IoT device dropped from the network or had to restart? So when something goes wrong, someone will get an email and stop the panic before it gets bad.
Is this maybe a new word? ... PowerShell groups say they can "exit" using an EXIT code. The modified PowerShell script $ip gets the first command from the arguments, and it runs a test connection to that IP address with quiet mode turned on. If the connection works (the result is True), then it checks if a file named c:\test\nc exists in the current directory. If it does, it deletes the file and creates another one called cn. The script also adds exit code 0 to finish things up nicely. If the check fails though, it tries again but this time it keeps deleting and making files until it finds them. Wait, that's wrong if the connection isn't there; instead, it deletes the file first and then tries to create another one called cn with an error code of 1. Actually, looking closer at the second part: if the path exists, it removes the file and creates a new one called cn, but this time it says exit 1, which means "error". If the path doesn't exist though, it just keeps touching that same file again until something changes. The final batch file starts by turning off all automatic settings, then runs a PowerShell script against a specific file in the test folder, saving whatever result gets sent back to a variable called rslt. After running that script, it loops through every line of output and checks if the answer is 0 or not. If it's zero, it prints "Connected"; otherwise, it just prints "Not Connected".
SOLVED! Thanks everyone for your input (s). While "The Dude" monitoring software is free, Mikrotik seems like it needs a special piece of licensed hardware. I mostly live on my PC and use my email client to check in every few minutes. A five-minute warning (just an email notification) will surely 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 a simple relay can be rebooted by cutting one of their DC power wires). ... recently found pre-packaged Wifi remote AC switches (fully expect will use the same technology). These ready-to-use products would also work with a big plus: no need to engineer and make something on your own, which is what I did. - after many hours beating the ps1 & bat script combinations to death with no working solution ... my conclusion from this exercise is that although the ps1 script correctly returns an exit value to PowerShell, the PowerShell shell does NOT pass that value on to the BAT script ... AND ... There-in lies the Solution! SOLUTION: Call "test-connection" directly as a PowerShell command instead of embedding it in a ps1 script. For this solution, the ps1 script is no longer needed... only requires a simple BAT file like this: @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 file every five minutes. If the returned value doesn't match, it will send me a notification email. Hope this solution helps others who are struggling with a BAT calling PowerShell scenario