require ("Application"); require ("regexSupport"); /* ============================================================================= * ============================== IMPORTANT NOTE =============================== * ============================================================================= * This script uses an external Microsoft Utility (PortQuery) to query the status of a port. * portquery must be installed on the machine prior to running this script or the script will * NOT work. PortQuery can be downloaded from Microsot below: * * https://www.microsoft.com/en-us/download/confirmation.aspx?id=17148 * * PortQry should ideally be installed in the default directory (C:\PortQryV2\) but can be changed * during install. If changed please make sure to update the file path to the portqry.exe application * on line 40 of the script (in the ShellExecute call). */ class MonitorRemoteProcess Application { /* The host on which the service resides and can be hostname or IP address */ remoteIP = "SWTB-LT-032"; /* The port which should be queried to determine if the service is running. * This port must not be blocked by a firewall - if the port is being filtered * this script will report that the service is not running. */ remotePort = "8080"; /* Where should the DataHub store the temporary working file. * This file will contain the service query results and will * be parsed by the DataHub to determine if the service is listening */ storageFile = "C:/users/mholbach/desktop/temp"; /* How often should the service be queried - in seconds */ queryInterval = 5; } /* Use methods to create functions outside the 'main line'. */ method MonitorRemoteProcess.QueryRemoteProcess () { // Query endpoint and print result to storageFile ShellExecute(0, "open", "cmd", string("/c C:/portqryv2/portqry.exe -n ",.remoteIP," -e ",.remotePort," -p tcp > ",.storageFile), "", SW_HIDE); // Read Storage File content local filePointer = open (.storageFile, "r"); local line = read_line(filePointer); local fileContent; while(line != _eof_) { fileContent = string(fileContent,line,"\n"); line = read_line(filePointer); } close(filePointer); local pattern = string("TCP port ",.remotePort," \(.+\): (.+)"); results = Regex.Match(pattern,fileContent); try { // Parse for service state if(strstr(results[2][2],"NOT LISTENING") == -1 && strstr(results[2][2],"LISTENING") != -1) princ("Service that is running on host ", .remoteIP," and is listening on port ",.remotePort," is Running and is reachable\n"); else princ("Service that is running on host ", .remoteIP," and is listening on port ",.remotePort," is NOT reachable an/or is NOT Running.\n"); } catch { princ("Problem parsing regular expression output\n"); } .TimerAfter(.queryInterval,`(@self).QueryRemoteProcess()); } /* Write the 'main line' of the program here. */ method MonitorRemoteProcess.constructor () { .QueryRemoteProcess(); } /* Any code to be run when the program gets shut down. */ method MonitorRemoteProcess.destructor () { } /* Start the program by instantiating the class. If your * constructor code does not create a persistent reference to * the instance (self), then it will be destroyed by the * garbage collector soon after creation. If you do not want * this to happen, assign the instance to a global variable, or * create a static data member in your class to which you assign * 'self' during the construction process. ApplicationSingleton() * does this for you automatically. */ ApplicationSingleton (MonitorRemoteProcess);