Working with multiple commands
The SSH adapter is used to execute single or multiple commands on a remote host using the SSH protocol. To send multiple commands by using the SSH adapter, one must use the <prompt> element.
If a <prompt> element is not defined, each command is executed in a separate session, as if each command is executed in a new command shell. Each command is executed independently, without any effect on subsequent commands. With the <prompt> element, commands are executed in a sequence, in the same login shell.
The following video shows how to send multiple commands by using the SSH adapter using a session-based approach:
https://www.youtube.com/watch?v=rYURxZ0gO3A&feature=youtu.be
To send multiple commands to the target server
The following figure shows an example of multiple commands run on the target server.
1. login as: lnxuser1
Last login: Mon Nov 23 13:13:33 2015 from abc.bmc.com-bash: ntpdate: command not found
2. [lnxuser1@vl-cou-atm-dx0x ~]$ whoami // here $ is the console prompt lnxuser1
3. [lnxuser1@vl-cou-atm-dx0x ~]$ su root
4. Password: // Password prompt returned for "su root" command
5. [root@vl-cou-atm-dx0x lnxuser1]# whoami // here prompt has changed to # root
6. [root@vl-cou-atm-dx0x lnxuser1]# cd /root
7. [root@vl-cou-atm-dx0x ~]# pwd/root
8. [root@vl-cou-atm-dx0x ~]# exitexit9. [lnxuser1@vl-cou-atm-dx0x ~]$
------------Manual Console output Ends ------------------------------
To build an adapter request based on the command and prompt sequence, perform the following steps.
Log on to the target server.
The prompt to login to the target is done by the adapter while SSH to the target, you need to specify the user and password in the <target> element of adapter request.
The following figure shows the sample XML.<targets>
<target name="">
<host>vl-abc-atm-dx0x/host>
<port>22</port>
<userName>lnxuser1</userName>
<password>XXXX</password>
<timeout-secs>60</timeout-secs>
</target>
</targets>After you log on, the console prompt returns the "$".
This is the prompt on which the first command (specified in the <commands> element) is executed. This prompt must be specified in the <prompt> element inside the target element.
The following figure shows the sample XML.<targets>
<target name="">
<host>vl-cou-atm-dx0x/host>
<port>22</port>
<userName>lnxuser1</userName>
<password>XXXX</password>
<prompt>$</prompt>
</target>
</targets>The actual commands are executed from this step, which has to be specified as part of the <commands> element sequentially, in the <command> attribute.
Command: whoami
Prompt returned after command execution = "$" (which is shown in step 3)<command prompt="$">whoami</command>Command: su root
Prompt returned after command execution (next prompt) = "Password:" (as shown in step 4)Since this command requires a password to be specified by the user, it does not require an exit code. In this case, the <ignore-exit-code> element must be set to "true" because the "Password:" input is not command prompt.
<command ignore-exit-code="true" prompt="Password:">su root</command>Command: password
Specify password for the user root here.
Prompt returned after command execution (next prompt) = "#" (as shown in step 5)<command prompt="#">rootPassword</command>Command : whoami
Prompt returned after command execution (next prompt) = "#" (as shown in step 6)<command prompt="#">whoami</command>Command : whoami
Prompt returned after command execution (next prompt) = "#" (as shown in step 7)_ <command prompt="#">cd /root</command>_Command : pwd
Prompt returned after command execution (next prompt) = "#" (as shown in step 8)<command prompt="#">pwd</command>Command : exit
Prompt returned after command execution (next prompt) = "$"Here exit command required to exit from su root user to switch back to logged in user, which is lnxuser1
This command is the last command in this example and command can be extended sequentially based on the use case and prompts to be specified accordingly.
<command prompt="$">exit</command>After this last command, the adapter executes the exit command internally to terminate the session, but if the shell takes a different command to terminate the shell, for example, logout, then session termination command must be the last command in the request
<command prompt="$">logout</command>
The following figure shows the sample XML adapter request for the SSH adapter for sending multiple commands.
<targets>
<target name="">
<host>vl-cou-atm-dx0x/host>
<port>22</port>
<userName>lnxuser1</userName>
<password>XXXX</password>
<timeout-secs>60</timeout-secs>
<prompt>$</prompt>
<establish-connection-timeout-secs>60</establish-connection-timeout-secs>
</target>
</targets>
<prompts>
<prompt name="initial">$</prompt>
<prompt name="passwordprompt">Password:</prompt>
<prompt name="rootprompt">#</prompt>
</prompts>
<commands>
<command prompt="initial">whoami</command>
<command ignore-exit-code="true" prompt="passwordprompt">su root</command>
<command prompt="rootprompt">rootPassword</command>
<command prompt="rootprompt">whoami</command>
<command prompt="rootprompt">cd /root</command>
<command prompt="rootprompt">pwd</command>
<command prompt="initial">exit</command>
</ssh-request>
The following figure shows the adapter output for the request.
<metadata>
<status>success</status>
</metadata>
<targets-output>
<target-output host="vl-cou-atm-dx0x">
<metadata>
<os-id>Linux</os-id>
<os-version>2.6.18-308.el5</os-version>
<status>success</status>
</metadata>
<commands-output>
<command-output>
<metadata>
<command>whoami</command>
<line-count>2</line-count>
<execution-milliseconds>62</execution-milliseconds>
<exit-code>0</exit-code>
<status>success</status>
</metadata>
<output>
<line index="1">lnxuser1</line>
<line index="2">[lnxuser1@vl-cou-atm-dx0x ~]$</line>
</output>
</command-output>
<command-output>
<metadata>
<command>su root</command>
<line-count>1</line-count>
<execution-milliseconds>0</execution-milliseconds>
<exit-code>9999</exit-code>
<status>success</status>
</metadata>
<output>
<line index="1">Password:</line>
</output>
</command-output>
<command-output>
<metadata>
<command>rootPassword</command>
<line-count>1</line-count>
<execution-milliseconds>31</execution-milliseconds>
<exit-code>0</exit-code>
<status>success</status>
</metadata>
<output>
<line index="1">[root@vl-cou-atm-dx0x lnxuser1]#</line>
</output>
</command-output>
<command-output>
<metadata>
<command>sudo whoami</command>
<line-count>2</line-count>
<execution-milliseconds>16</execution-milliseconds>
<exit-code>0</exit-code>
<status>success</status>
</metadata>
<output>
<line index="1">root</line>
<line index="2">[root@vl-cou-atm-dx0x lnxuser1]#</line>
</output>
</command-output>
<command-output>
<metadata>
<command>cd /root</command>
<line-count>1</line-count>
<execution-milliseconds>16</execution-milliseconds>
<exit-code>0</exit-code>
<status>success</status>
</metadata>
<output>
<line index="1">[root@vl-cou-atm-dx0x ~]#</line>
</output>
</command-output>
<command-output>
<metadata>
<command>pwd</command>
<line-count>2</line-count>
<execution-milliseconds>31</execution-milliseconds>
<exit-code>0</exit-code>
<status>success</status>
</metadata>
<output>
<line index="1">/root</line>
<line index="2">[root@vl-cou-atm-dx0x ~]#</line>
</output>
</command-output>
<command-output>
<metadata>
<command>exit</command>
<line-count>2</line-count>
<execution-milliseconds>31</execution-milliseconds>
<exit-code>0</exit-code>
<status>success</status>
</metadata>
<output>
<line index="1">exit</line>
<line index="2">[lnxuser1@vl-cou-atm-dx0x ~]$</line>
</output>
</command-output>
</commands-output>
</target-output>
</targets-output>
</ssh-command-output>