Expect SSH
Use the Expect SSH block to automate scripted interactive Secure Shell (SSH) sessions. By using this block, you can securely connect to a remote server and submit a predefined list of commands, on the condition that an expected response is received for each command in the list.
The block automates SSH workflows by processing a defined list of steps. For each list entry, you specify the following information:
- One or more expected output patterns that come from the remote server, such as a command prompt or a specific output message.
- A command that the workflow executes on the remote server when one of the expected patterns is detected.
- A timeout period, in seconds, during which the workflow checks for one of the expected patterns to appear.
Each list entry that defines expected patterns and commands represents a step in the SSH session. If the workflow detects an expected pattern within the timeout period, it executes the corresponding command. If none of the expected patterns match within the timeout, the workflow ends the SSH session.
Setting up the block
- Connect to IBM Automation Library and click Integrations.
- Search for the Expect integration and select it.
- Choose and download the version of the Expect SSH integration that you need to use.
- In your Concert Workflows instance, import this integration by following the steps in Importing integrations.
After the integration is imported, the Expect SSH block is available for use in workflows.
Using the block in a workflow
To incorporate the Expect SSH block into a workflow, complete the following steps:
- In your workflow editor, locate the Expect SSH block in the integration panel.
- Drag and drop the block to the required location in your workflow.
- Configure the following block parameters:
- authkey
- Provide an authentication that contains the required connection credentials for the remote host. In the Expect SSH block, click + Create authentication to open a pop-up window where you enter the authentication details. The selected authentication must use the Expect SSH service. For more information about creating authentications, see Using authentications.
- commands
- Provide the sequence of commands that you want to submit and the expected response for each one. This parameter must be specified as an array of objects. Each array element represents a step in the SSH session and must contain the key-value pairs that are shown in this table:
Table 1. SSH command parameters Key Format Value timeout number The maximum time in seconds that the workflow waits for any of the expected patterns to appear. If no pattern matches within this time, the step fails and the SSH session ends.
expect string [] The patterns that the workflow checks for in the remote shell output, specified as a regular expression or an array of strings.
send string The command that the workflow sends to the remote shell after the expect step successfully matches a pattern, specified as a string.
- Run the workflow, check the logs, and verify that the specified commands were executed on the remote host.
Command array structure and example
The following example shows a commands parameter value that the Expect SSH block processes sequentially:
[
{
"timeout": 10,
"expect": ["[#$]$"],
"send": "echo SHELL is READY"
},
{
"timeout": 10,
"expect": ["SHELL is READY"],
"send": "mkdir -p /tmp/app && echo 'DIR_OK'"
},
{
"timeout": 10,
"expect": ["DIR_OK"],
"send": "cd /tmp/app && pwd"
},
{
"timeout": 10,
"expect": ["/tmp/app"],
"send": "docker -v"
},
{
"timeout": 10,
"expect": ["Docker version [0-9]+\\.[0-9]+\\.[0-9]+"],
"send": "echo 'Docker version is present and ready to use!'"
}
]
When you run the workflow with these commands, it processes each entry in the commands array in the following sequence:
- The Expect SSH block connects to the remote host by using the credentials specified in the
authkeyparameter. For each entry, the workflow waits for the expected output and then sends the corresponding command. - In the first entry, the workflow waits up to 10 seconds for the
[#$] $string pattern in the SSH session.- If the expected pattern is not detected within the timeout period, the SSH session ends and the workflow stops.
- If the pattern is detected, the workflow sends the command
echo SHELL is READYto the remote host. This output helps to ensure that authentication is successful for the user specified in the Expect SSH authentication and that the workflow can send additional commands.
- In the second entry, the workflow waits for the
SHELL is READYoutput and then sends the commandmkdir -p /tmp/app && echo 'DIR_OK'to create the/tmp/appdirectory on the remote host. The'DIR_OK'message confirms that the directory is created. - In the third entry, the workflow waits for the
DIR_OKoutput to confirm that the/tmp/appdirectory exists. It then sends the commandcd /tmp/app && pwdto navigate to the directory. Thepwdcommand displays the absolute path of the current directory, which helps to confirm that the workflow is running in the correct location before navigating or running further commands. - In the fourth entry, the workflow waits for the
/tmp/appoutput to confirm that the user is in the correct directory, and then sendsdocker -vto verify that the Docker is installed on the instance. - In the fifth entry, the workflow waits for Docker version output that matches
Docker version [0-9]+\\.[0-9]+\\.[0-9]+. When the output matches, the workflow confirms that Docker is available on the instance and sends the confirmation messageecho 'Docker version is present and ready to use!'.