Discuss this help topic in SecureBlackbox Forum

Execute commands remotely

SSH provides a way to execute one or more commands without dealing with shells and command prompts.

To execute the commads using simple SSH, you need to initiate the connection as described in the the corresponding how-to article.

Before calling Open() method of TElSimpleSSHClient, you need to specify the command(s) to be executed and adjust certain settings, specific to remote shell.

First of all, you need to specify one or more commands to be executed. If you have just one command, set Command property of TElSimpleSSHClient class. If you have several commands, then instead of using Command property, use Commands property.

Next, you can specify the environment variables. This is done using Environment property of TElSimpleSSHClient class.

Also, if you are executing a terminal (command-line) application, you can create an instance of TElTerminalInfo class and assign it to TerminalInfo property of TElSimpleSSHClient class. TElTerminalInfo class lets you specify the parameters of the remote terminal, such as width and height of the terminal window, and width and height of the terminal buffer, and the type of the terminal.
TElTerminalInfo properties can be changed when the connection is already established. If you need to change several parameters when the connection is already present, use BeginUpdate() method of TElTerminalInfo class before you change parameters and EndUpdate() method of TElTerminalInfo class after you've finished your changes.

After you've set the needed parameters, call Open() method of TElSimpleSSHClient class.

After the connection is opened, command(s) are executed automatically. Multiple commands, specified via Commands property, will be executed sequentially.

Use CanReceive() method of TElSimpleSSHClient class to check, if there's the data to receive. If the data is available, use ReceiveData() or ReceiveText() method to receive the remote data.

If you invoke multiple commands using Commands property and need to distinguish between output of the commands, you can do the following:

  1. declare a handler for OnSendCommandRequest event
  2. in the OnSendCommandRequest event handler call something like this:
    if (CommandIdx > 0)
    {
        while (SimpleSSHClient.CanReceive(0))
        {
            NewData = SimpleSSHClient.ReceiveData();
            AppendReplyData(NewData); // merge the received data with previously received data
        }
        ... // handle the complete reply for the previous command
    }
    This will read the rest of reply for the previous executed command

Note, that various servers handle the command channel differently. Some can start the shell, while others will execute the commands as if they were an application name with [optional] parameters. So don't expect the server to handle shell commands. If you need to execute a shell command on Windows, for example, you can use a command in "cmd /c dir c:\" form. Use RequestTerminal property to control the behaviour of the remote server regarding starting the shell.

Discuss this help topic in SecureBlackbox Forum