Bird
Raised Fist0
Bash Scriptingscripting~10 mins

SSH automation in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - SSH automation
Start script
↓
Check SSH key
Yes↓
Run ssh command
↓
Receive output
↓
Process output or error
↓
End script
The script checks for SSH keys, runs a remote command via SSH, receives output, and ends.
Execution Sample
Bash Scripting
ssh user@host 'echo Hello from remote'
echo Done
Connects to remote host via SSH and runs a simple echo command, then prints Done locally.
Execution Table
StepActionCommand/ConditionResultOutput
1Start scriptN/AScript begins
2Check SSH keyssh-keygen -F hostKey found or notEntry printed if key exists
3Run ssh commandssh user@host 'echo Hello from remote'Connected and command executedHello from remote
4Print local messageecho DonePrinted locallyDone
5End scriptN/AScript ends
💡 Script ends after running remote command and printing local message.
Variable Tracker
VariableStartAfter SSH commandFinal
ssh_outputHello from remoteHello from remote
local_messageDone
Key Moments - 2 Insights
Why does the script not ask for a password every time?
Because SSH keys are set up and found in step 2 (see execution_table row 2), allowing passwordless login.
What happens if the remote command fails?
The ssh command returns an error and no output is captured in ssh_output (see execution_table row 3). The script still prints Done locally.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of the ssh command at step 3?
ADone
BHello from remote
CNo output
DError message
💡 Hint
Check the Output column in execution_table row 3.
At which step does the script print the local message 'Done'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Action and Output columns in execution_table row 4.
If SSH keys are not set up, what would change in the execution flow?
AThe script would ask for a password at step 3
BThe script would skip the ssh command
CThe script would print 'Done' before ssh command
DThe script would end immediately
💡 Hint
Think about what happens when ssh-keygen -F host does not find a key (execution_table row 2).
Concept Snapshot
SSH automation runs commands on remote machines without manual login.
Use SSH keys for passwordless access.
Basic command: ssh user@host 'command'.
Capture output to variables for processing.
Automate repetitive remote tasks easily.
Full Transcript
This visual execution shows how SSH automation works in a bash script. The script starts and checks if SSH keys exist for passwordless login. Then it runs a remote command using ssh, capturing the output 'Hello from remote'. After that, it prints a local message 'Done'. The script ends after these steps. Key points include the importance of SSH keys to avoid password prompts and how output is handled. The execution table tracks each step, the commands run, and their results. Variables like ssh_output store remote command output. This helps beginners see how SSH automation scripts flow and behave.

Practice

(1/5)
1. What is the main benefit of using SSH keys for automation instead of passwords?
easy
A. It allows passwordless login for automated scripts.
B. It makes the connection slower but more secure.
C. It requires manual password entry every time.
D. It disables encryption for faster transfers.

Solution

  1. Step 1: Understand SSH key usage

    SSH keys allow scripts to connect without typing passwords manually.
  2. Step 2: Compare with password login

    Password login needs manual input, which breaks automation.
  3. Final Answer:

    It allows passwordless login for automated scripts. -> Option A
  4. Quick Check:

    SSH keys enable passwordless login = B [OK]
Hint: SSH keys = no password prompts for scripts [OK]
Common Mistakes:
  • Thinking SSH keys slow down connection
  • Assuming passwords work without manual input
  • Believing SSH keys disable encryption
2. Which of the following is the correct syntax to copy a local file file.txt to a remote server using SSH automation?
easy
A. ssh file.txt user@remote:/path/
B. scp file.txt user@remote:/path/
C. ssh-copy file.txt user@remote:/path/
D. copy file.txt user@remote:/path/

Solution

  1. Step 1: Identify the command for secure copy

    scp is used to copy files over SSH securely.
  2. Step 2: Check other options

    ssh runs commands, ssh-copy is not a valid command, and copy is not for SSH.
  3. Final Answer:

    scp file.txt user@remote:/path/ -> Option B
  4. Quick Check:

    Use scp to copy files over SSH = A [OK]
Hint: Use scp to copy files remotely [OK]
Common Mistakes:
  • Using ssh instead of scp for file copy
  • Typing ssh-copy which is invalid
  • Using copy command which is local only
3. What will be the output of this command if the remote server is reachable and the user has SSH key access?
ssh user@remote 'echo Hello from remote'
medium
A. Connection timed out.
B. Permission denied (publickey).
C. Hello from remote
D. ssh: Could not resolve hostname remote: Name or service not known

Solution

  1. Step 1: Understand the command

    The command runs echo Hello from remote on the remote server via SSH.
  2. Step 2: Check conditions

    If the server is reachable and SSH keys are set, the command outputs the echoed text.
  3. Final Answer:

    Hello from remote -> Option C
  4. Quick Check:

    SSH runs remote echo command = Hello from remote [OK]
Hint: SSH runs remote commands and shows output [OK]
Common Mistakes:
  • Assuming permission denied without checking keys
  • Confusing hostname errors with command output
  • Thinking SSH command outputs nothing
4. You wrote this script to automate a remote command:
ssh user@remote 'uptime'

But it fails with "Permission denied (publickey)". What is the most likely fix?
medium
A. Generate SSH keys and add the public key to remote's authorized_keys.
B. Change the command to ssh user@remote uptime without quotes.
C. Use scp instead of ssh to run commands.
D. Restart the remote server to reset SSH.

Solution

  1. Step 1: Understand the error

    "Permission denied (publickey)" means SSH keys are missing or not accepted.
  2. Step 2: Fix SSH key setup

    Generate SSH keys locally and add the public key to ~/.ssh/authorized_keys on remote.
  3. Final Answer:

    Generate SSH keys and add the public key to remote's authorized_keys. -> Option A
  4. Quick Check:

    Permission denied means missing keys = A [OK]
Hint: Add your public key to remote authorized_keys [OK]
Common Mistakes:
  • Removing quotes does not fix permission errors
  • Using scp instead of ssh for commands
  • Restarting server won't fix key issues
5. You want to automate running multiple commands on a remote server via SSH in one script. Which is the best way to do this?
hard
A. Use a single SSH command with multiple commands separated by semicolons inside quotes.
B. Run separate SSH commands for each remote command in the script.
C. Use scp to send commands instead of SSH.
D. Copy a script file to remote and run it with one SSH command.

Solution

  1. Step 1: Consider multiple commands execution

    Running many commands inline can be complex and error-prone.
  2. Step 2: Use script file for clarity and reuse

    Copying a script file to remote and running it via SSH is cleaner and easier to maintain.
  3. Final Answer:

    Copy a script file to remote and run it with one SSH command. -> Option D
  4. Quick Check:

    Script file on remote is best for multiple commands = C [OK]
Hint: Send script file and run remotely for many commands [OK]
Common Mistakes:
  • Running many commands inline can cause quoting errors
  • Running separate SSH commands is slower and complex
  • Using scp to send commands is incorrect
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard