Afaics, you are not checking anywhere what the
telnet command actually responded. That's why I was saying, capture the response to the clipboard and then analyze its contents.
Since I forgot everything about telnet (haven't used it since the '90s) I am using a
ping to google.com for demonstration purposes:
Code: Select all
botToken := "somethingsomething"
chatID := 0000000001 ; fill in your credentials for bot token and chat ID
site := "google.com" ; also try non-existing "google.coma"
SetTimer , pingIt, 20000
pingIt:
Clipboard := ""
text := "tbd" ; reset text
RunWait, "%ComSpec%" /c "ping %site% | clip",, Hide ; "| clip" will pipe the result to the clipboard
ClipWait, 0
If ErrorLevel
MsgBox, 48, Error, An error occurred while waiting for the clipboard., 5 ; msgbox times out after 5 seconds
If Instr(clipboard, "nicht finden") ; check the ping result for a certain string
text := "Host " site " could not be found!"
else
text := "Host " site " was found."
tooltip, % clipboard ; for testing
str := "https://api.telegram.org/bot" botToken "/sendmessage?"
param := "chat_id=" chatid "&text=" text
UrlDownloadToFile, % str . param, result.log ; log file contains response from telegram; check if message was sent or error occurred
return
Esc::ExitApp
Please note that I get a ping message in german on my computer. So, if the host can't be found, the message will contain the string "nicht finden" (meaning "not found") - please change it accordingly in
If Instr(clipboard, "nicht finden") to the fail message you want to detect. Of course, I could also parse the success message and extract the milliseconds or whatever... this is just an example.
google.com should usually be online
, but you can try
site := "google.coma" for a failing ping. (I have no idea what the telnet command will actually respond.)
The tooltip will show you the last ping result, for testing (you can remove it, if you want).
Of course, piping the result to the clipboard via
| clip could interfere with your normal clipboard usage. I guess SKAN's
RunCmd() could be an option to capture the result to some other variable instead, and to keep the clipboard available for you. Or, you could probably redirect the result to a text file and then analyze that. Another option would be
RunWaitOne() from
Run-example #7 , but that won't hide the cmd window, afaik. It depends on your requirements.