SSH SSH: Running Commands

Summary

This page describes the process this plugin uses to:

  • Run SSH commands
  • Interpret prompts
  • Clean & return command output

Starting the Session

When the session starts, PPA waits to see the command prompt (line 15).

It uses the base_prompt expression in the SSH Server dictionary to do this.

Wait for Initial Prompt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-1057-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Fri Apr 17 07:23:03 UTC 2020

  System load:  0.0               Processes:           90
  Usage of /:   29.4% of 7.69GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 1.2.3.4
  Swap usage:   0%

Last login: Thu Apr 16 07:00:03 2020 from 1.2.3.4
ubuntu@hostname:~$

Base Prompt

See the SSHPasswordAuth & SSHKeyAuth dictionaries for more information on base_prompt.

Running Commands

Once the opening prompt is found, commands are run using the following process.

Each step references a line number from the example.

  1. Type a command and run it (line 1)
  2. Any questions prompted by the command are answered (line 2)
  3. The command is complete when the base_prompt expression is found (line 14)

Steps 1-3 are repeated when running multiple commands.

Running Commands
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
ubuntu@hostname:~$ sudo ls -lsah
[sudo] password for user:
total 36K
4.0K drwxr-xr-x 5 ubuntu ubuntu 4.0K Mar 20 13:00 .
4.0K drwxr-xr-x 4 root   root   4.0K Apr 16 07:01 ..
4.0K -rw------- 1 ubuntu ubuntu 3.4K Apr 16 11:21 .bash_history
4.0K -rw-r--r-- 1 ubuntu ubuntu  220 Apr  4  2018 .bash_logout
4.0K -rw-r--r-- 1 ubuntu ubuntu 3.7K Apr  4  2018 .bashrc
4.0K drwx------ 2 ubuntu ubuntu 4.0K Feb  6 10:59 .cache
4.0K drwx------ 3 ubuntu ubuntu 4.0K Feb  6 10:59 .gnupg
4.0K -rw-r--r-- 1 ubuntu ubuntu  807 Apr  4  2018 .profile
4.0K drwx------ 2 ubuntu ubuntu 4.0K Feb  6 10:52 .ssh
   0 -rw-r--r-- 1 ubuntu ubuntu    0 Feb  6 12:11 .sudo_as_admin_successful
ubuntu@hostname:~$

Getting Command Output

Anything seen before the prompt, excluding questions, is the command output.

Output from Command
total 36K
4.0K drwxr-xr-x 5 ubuntu ubuntu 4.0K Mar 20 13:00 .
4.0K drwxr-xr-x 4 root   root   4.0K Apr 16 07:01 ..
4.0K -rw------- 1 ubuntu ubuntu 3.4K Apr 16 11:21 .bash_history
4.0K -rw-r--r-- 1 ubuntu ubuntu  220 Apr  4  2018 .bash_logout
4.0K -rw-r--r-- 1 ubuntu ubuntu 3.7K Apr  4  2018 .bashrc
4.0K drwx------ 2 ubuntu ubuntu 4.0K Feb  6 10:59 .cache
4.0K drwx------ 3 ubuntu ubuntu 4.0K Feb  6 10:59 .gnupg
4.0K -rw-r--r-- 1 ubuntu ubuntu  807 Apr  4  2018 .profile
4.0K drwx------ 2 ubuntu ubuntu 4.0K Feb  6 10:52 .ssh
   0 -rw-r--r-- 1 ubuntu ubuntu    0 Feb  6 12:11 .sudo_as_admin_successful

Sanitising Command Output

Cleaning Sensitive Data

Characters sent over SSH are echoed back to the client automatically.

Leaving the command in the output can expose sensitive information.

This plugin will remove any occurrence of the issued command from the output.

Exceptions

Terminal size may change the response, making it impossible to find & remove the command.

Most cases are caused by line wrapping when a command is longer than the terminal width.

You can avoid this issue by using run_commands & setting terminal width.

Terminal Width - Ubuntu
1
2
3
- ssh.client.run_commands:
    commands:
      - command: stty columns 150
Terminal Width - Cisco
1
2
3
- ssh.client.run_commands:
    commands:
      - command: terminal width 150