CmdRunnerWorker ClassCleanCode C# Libraries v1.2.03 API
Executes one command on a collection of remote servers using a telnet session.
Inheritance Hierarchy

OnlineSystem Object
  CleanCode.Threading ThreadWorker
    CleanCode.RemoteCommands CmdRunnerWorker

Namespace: CleanCode.RemoteCommands
Assembly: CleanCode.RemoteCommands (in CleanCode.RemoteCommands.dll) Version: 1.2.3.0 (1.2.03)
Syntax

public class CmdRunnerWorker : ThreadWorker
Remarks

CmdRunnerWorker is an implementation of the abstract ThreadWorker class, which performs work in a background thread allowing your foreground GUI to continue interacting with a user. CmdRunnerWorker executes a single command on one or more remote systems then filters and formats that output per your specifications.

The command on a single server is executed by an instance of either TelnetExternalExec (which uses the external program Onlineplink) or TelnetLibExec (which uses an internal telnet library). Unless you invoke the UseExternalCmd  method, the latter is used. It is preferable and seems to work fine for Ethernet connections, but with servers communicating over GPRS it does not work reliably, hence the choice.

To use any ThreadWorker objects you need to have a ThreadManager which handles multiple background tasks for your application (say, for example, two to execute commands, one to do a ping check, one to get WMI information, etc.). While you may certainly use multiple CmdRunnerWorker objects, the separate CmdRunnerMultiWorker was designed to streamline the process of dealing with multiple remote commands.

See the remarks for ThreadManager for a comprehensive discussion of how to wire up and use ThreadWorkers.

Since CleanCode 0.9.07.

Examples

This instrumentation code is similar but not identical between CmdRunnerWorker and CmdRunnerMultiWorker To use this class you will need to define a callback routine to process results as they become available.
private void MyUpdater(string ip, string cmd, string msg)
{ . . . }
This method is then identified as a delegate to the CmdRunnerWorker.
CmdRunnerWorker.CmdRunnerUpdater cmdCallback =
    new CmdRunnerWorker.CmdRunnerUpdater(MyUpdater);
Next you instantiate an object with the necessary specifications and tell the ThreadManager about it.
cmdRunnerWorker = new CmdRunnerWorker(
    cmdCallback, 21, 10,
    "login", "Password", @"bash[^$]*\$", "mylogin", "mypwd");
threadManager.Add(cmdRunnerWorker);
cmdRunnerWorker.UseExternalCmd();
cmdRunnerWorker.SetIpAddressList(ipList);
cmdRunnerWorker.SetCmd("df",
    @".*?\n\S+\s+(\d+)(?:\s+\d+){2}\s+(\S+)", "{0} ({1})" );
After you have prepared all of your ThreadWorker objects, you tell the ThreadManager to start them.
threadManager.Run();
cmdRunnerWorker.UpdateResults();
Finally, you will need to periodically poll for results with this method, which invokes your delegate as needed.
See Also