Jump to content

"ping.exe" monitoring function.


mLipok
 Share

Recommended Posts

I had a need to create a "ping.exe" monitoring function.
Here is the result:

#include <AutoItConstants.au3>
#include <Debug.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

_DebugSetup('Check Ping - press SHIFT KEY to close')

If @ScriptName = 'Ping.exe' Then Exit ; compiled script should not be named "Ping.exe" as this is used for Run() and this can lead to recursion

OnAutoItExitRegister(_Adlib_Ping_Exit)
AdlibRegister(_Adlib_Ping)

_Example()
Func _Example()
    Local $hDLL = DllOpen('user32.dll')

    While 1
        Sleep(10)
        If _IsPressed('10', $hDLL) Then ExitLoop
    WEnd

EndFunc   ;==>_Example

Func _Adlib_Ping()
    Local Static $iPID = Run('ping www.google.com -l 1024 -t', @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    Local Static $RUN_ONCE = __Adlib_Ping_Exit($iPID)
    #forceref $RUN_ONCE

    Local Static $sOutput = '' ; Store the output of StdoutRead to a variable.

    Local Static $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.

    $sOutput &= StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.
    If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
        _DebugOut('Ping.exe process (' & $iPID & ') not exist ? ')
        AdlibUnRegister(_Adlib_Ping)
    EndIf
    If StringRegExp($sOutput, '(?is)(\R)', $STR_REGEXPMATCH) Then
        If TimerDiff($hTimer) > 5000 Then
            _DebugOut(@HOUR & ':' & @MIN & ':' & @SEC & ' PING RESULT = ' & $sOutput)
            $hTimer = TimerInit()
        EndIf
        $sOutput = ''
    EndIf

EndFunc   ;==>_Adlib_Ping

Func _Adlib_Ping_Exit()
    __Adlib_Ping_Exit()
EndFunc   ;==>_Adlib_Ping_Exit

Func __Adlib_Ping_Exit($vParameter = Default)
    Local Static $vParameter_static = $vParameter
    If Not @Compiled Then ConsoleWrite('- ' & @ScriptLineNumber & ' Parameter=' & $vParameter & ' Static=' & $vParameter_static & @CRLF)
    If $vParameter = Default And IsInt($vParameter_static) Then
        ProcessClose($vParameter_static)
        If Not @Compiled Then ConsoleWrite('! CLOSE PORCESS' & @CRLF)
        Local $hWndReportWindow = WinGetHandle($__g_sReportTitle_Debug, $__g_sReportWindowText_Debug)
        GUIDelete($hWndReportWindow)
    ElseIf IsInt($vParameter) Then
        $vParameter_static = $vParameter
    EndIf
EndFunc   ;==>__Adlib_Ping_Exit

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

@mLipok A suggestion to improve efficiency of the main loop, use HotKeySet to modify a variable (probably a boolean) and check that instead of calling _IsPressed :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

14 hours ago, TheDcoder said:

@mLipok A suggestion to improve efficiency of the main loop, use HotKeySet to modify a variable (probably a boolean) and check that instead of calling _IsPressed :)

Main loop is only an example and has nothing to do with efficiency.

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

14 hours ago, argumentum said:

Would you explain at length what need this solves ?

This following improved example will explain this.

;~ https://www.autoitscript.com/forum/topic/202843-pingexe-monitoring-function/

#include <AutoItConstants.au3>
#include <Debug.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

_DebugSetup('Check Ping - press SHIFT KEY to close')
OnAutoItExitRegister(__DebugClose)

If @ScriptName = 'Ping.exe' Then Exit ; compiled script should not be named "Ping.exe" as this is used for Run() and this can lead to recursion

_Example()

Func _INIT_PING_TESTING()
    _Adlib_Ping_SetURL('www.onet.pl')
    AdlibRegister(_Adlib_Ping_Wrapper)
EndFunc   ;==>_INIT_PING_TESTING

Func _Example()
    ; this example function is only for simulation of looping some activieties
    Local $hDLL = DllOpen('user32.dll')

    ; here we use ForNext Loop, as a way to simulate some data processing or GUI maintaining
    For $iStep_idx = 1 To 1000
        Sleep(1000)
        If _IsPressed('10', $hDLL) Then ExitLoop
        _DebugOut('Step #' & $iStep_idx & ' Some processing.....')
        If $iStep_idx = 11 Then _INIT_PING_TESTING() ; here we simulates a situation when USER starts testing PING
        If $iStep_idx = 100 Then _Adlib_Ping_Exit_Wrapper() ; here we simulates a situation when USER stop testing PING
    Next

    ; REMARKS
    ; PING testing is processed in the "background" as a Adlib
    ; the basic premise is to interleaves PING logs with logs from the main program loop.

EndFunc   ;==>_Example

Func _Adlib_Ping_Wrapper()
    __Adlib_Ping()
EndFunc   ;==>_Adlib_Ping_Wrapper

Func _Adlib_Ping_SetURL($sURL)
    __Adlib_Ping($sURL)
EndFunc   ;==>_Adlib_Ping_SetURL

Func __Adlib_Ping($sURL = Default)
    Local Static $iPID = 0
    If IsString($sURL) And $iPID = 0 Then
        _DebugOut('Testing URL = ' & $sURL)
        $iPID = Run('ping ' & $sURL & ' -l 1024 -t', @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
        Return
    EndIf

    Local Static $RUN_ONCE_EXIT_1 = OnAutoItExitRegister(_Adlib_Ping_Exit_Wrapper)
    Local Static $RUN_ONCE_EXIT_2 = __Adlib_Ping_Exit($iPID)
    #forceref $RUN_ONCE_EXIT_1, $RUN_ONCE_EXIT_2

    Local Static $sOutput = '' ; Store the output of StdoutRead to a variable.

    Local Static $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.

    $sOutput &= StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.
    If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
        _DebugOut('Ping.exe process (' & $iPID & ') not exist ? ')
        AdlibUnRegister(_Adlib_Ping_Wrapper)
    EndIf
    If StringRegExp($sOutput, '(?is)(\R)', $STR_REGEXPMATCH) Then
        If TimerDiff($hTimer) > 5000 Then
            _DebugOut(@HOUR & ':' & @MIN & ':' & @SEC & ' PING RESULT = ' & $sOutput)
            $hTimer = TimerInit()
        EndIf
        $sOutput = ''
    EndIf

EndFunc   ;==>__Adlib_Ping

Func _Adlib_Ping_Exit_Wrapper()
    __Adlib_Ping_Exit()
EndFunc   ;==>_Adlib_Ping_Exit_Wrapper

Func __Adlib_Ping_Exit($vParameter = Default)
    Local Static $vParameter_static = $vParameter
    If Not @Compiled Then ConsoleWrite('- ' & @ScriptLineNumber & ' Parameter=' & $vParameter & ' Static=' & $vParameter_static & @CRLF)
    If $vParameter = Default And IsInt($vParameter_static) Then
        ProcessClose($vParameter_static)
        If Not @Compiled Then ConsoleWrite('! CLOSE PORCESS' & @CRLF)
    ElseIf IsInt($vParameter) Then
        $vParameter_static = $vParameter
    EndIf
EndFunc   ;==>__Adlib_Ping_Exit

Func __DebugClose()
    Local $hWndReportWindow = WinGetHandle($__g_sReportTitle_Debug, $__g_sReportWindowText_Debug)
    GUIDelete($hWndReportWindow)
EndFunc

 

Edited by mLipok
Example changed

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...