Execute multiple commands over SSH Linux [7 Methods]


Written By - Deepak Prasad

In the modern era of distributed systems and remote working environments, the ability to securely and efficiently manage remote systems has become an essential skill for system administrators, developers, and IT professionals alike. SSH (Secure Shell) is a widely used protocol that offers a secure and reliable way to execute multiple commands over SSH in a single session, making remote management more streamlined and efficient. In this article, we will delve into various techniques that enable you to execute multiple commands over SSH, providing a comprehensive understanding of how to effectively manage remote systems using this powerful tool.

From semicolon-separated commands to double ampersand-separated commands, we will explore different methods that cater to various use cases, such as executing commands sequentially, conditionally, or in parallel. By mastering these techniques, you can tailor your approach based on your specific needs and preferences, ensuring optimal efficiency in your remote command execution.

Furthermore, we will also discuss advanced techniques, such as using subshells, executing local scripts remotely, and employing Here Documents, which offer additional flexibility and control when executing multiple commands over SSH. Through practical examples and detailed explanations, this article aims to equip you with the knowledge and skills to manage remote systems with ease and confidence, ultimately enhancing your productivity and effectiveness in the interconnected world of today.

 

Different methods to execute multiple commands over SSH in Linux

Here's a list of different methods to execute multiple commands on remote systems via SSH, along with a brief description of each:

  1. Semicolon-separated commands: Run multiple commands sequentially by separating them with semicolons.
    ssh user@host 'command1; command2; command3'
    
  2. Double ampersand-separated commands: Execute the next command only if the previous command was successful, using double ampersands.
    ssh user@host 'command1 && command2 && command3'
    
  3. Single ampersand-separated commands: Run multiple commands in parallel by separating them with single ampersands.
    ssh user@host 'command1 & command2 & command3'
    
  4. Command grouping: Group commands using parentheses or braces, which can be helpful when combining with other methods like background execution.
    ssh user@host '(command1; command2) & (command3; command4)'
    
  5. Using a subshell: Execute multiple commands within a subshell on the remote system, providing a clean environment for the commands.
    ssh user@host 'bash -c "command1; command2; command3"'
    
  6. Executing a local script remotely: Run a local script containing multiple commands on the remote system.
    ssh user@host 'bash -s' < local_script.sh
    
  7. Using a Here Document: Execute multiple commands on a remote system using a Here Document to embed commands within the SSH command.
    ssh user@host << 'EOF'
    command1
    command2
    command3
    EOF

Now let's understand each of these methods in detail with examples

 

1. Using Semicolon-separated commands

This method allows you to run multiple commands sequentially on the remote system by separating them with semicolons.

Syntax:

ssh user@host 'command1; command2; command3'

This example will execute the commands in the following order: print "Hello, World!", list the contents of the remote directory, and display the current working directory.

ssh user@host 'echo "Hello, World!"; ls; pwd'

This example will change to the /var/log directory, count the number of lines containing "error" in the syslog file, and display the last 5 lines of the syslog file.

ssh user@host 'cd /var/log; grep "error" syslog | wc -l; tail -n 5 syslog'

 

2. Using double ampersand-separated commands

This method allows you to run multiple commands sequentially, but only execute the next command if the previous one was successful, using double ampersands.

Syntax:

ssh user@host 'command1 && command2 && command3'

This example will create a new folder called my_folder, change to that folder, and create a new file called my_file.txt only if the previous commands were successful.

ssh user@host 'mkdir my_folder && cd my_folder && touch my_file.txt'

This example will change to the /var/log directory, create a compressed file of syslog as syslog.gz, and remove the original syslog file only if the previous commands were successful.

ssh user@host 'cd /var/log && gzip -c syslog > syslog.gz && rm syslog'

 

3. Using single ampersand-separated commands

This method allows you to run multiple commands in parallel by separating them with single ampersands.

Syntax:

ssh user@host 'command1 & command2 & command3'

This example will print "Hello, World!", wait for 5 seconds, and then print "Finished!" all in parallel.

ssh user@host 'echo "Hello, World!" & sleep 5 & echo "Finished!"'

This example will search for all .txt files in the root directory and save the results to files.txt, wait for 5 seconds, and print "Finished!" all in parallel.

ssh user@host 'find / -name "*.txt" > files.txt & sleep 5 & echo "Finished!"'

 

4. Using command grouping

This method allows you to group commands using parentheses or braces, which can be helpful when combining with other methods like background execution.

Syntax:

ssh user@host '(command1; command2) & (command3; command4)'

This example will execute two groups of commands in parallel: the first group will print "Hello, World!" and wait for 5 seconds, and the second group will print "Starting..." followed by "Finished!".

ssh user@host '(echo "Hello, World!"; sleep 5) & (echo "Starting..."; echo "Finished!")'

This example will execute two groups of commands in parallel: the first group will change to the /var/log directory and save all lines containing "error" from the syslog file to errors.log, and the second group will change to the /var/tmp directory and display the disk usage for each item.

ssh user@host '(cd /var/log; grep "error" syslog > errors.log) & (cd /var/tmp; du -sh *)'

 

5. Using a subshell

This method allows you to execute multiple commands within a subshell on the remote system, providing a clean environment for the commands.

Syntax:

ssh user@host 'bash -c "command1; command2; command3"'

This example will execute the commands in the following order within a subshell: print "Hello, World!", list the contents of the remote directory, and display the current working directory.

ssh user@host 'bash -c "echo Hello, World!; ls; pwd"'

This example will change to the /var/log directory, count the number of lines containing "error" in the syslog file, and display the last 5 lines of the syslog file, all within a subshell.

ssh user@host 'bash -c "cd /var/log; grep error syslog | wc -l; tail -n 5 syslog"'

 

6. Executing a local script remotely

This method allows you to run a local script containing multiple commands on the remote system.

Syntax:

ssh user@host 'bash -s' < local_script.sh

Example: Create a file named local_script.sh with the following contents:

echo "Hello, World!"
ls
pwd

Then execute the script on the remote host:

ssh user@host 'bash -s' < local_script.sh

This example will execute the commands in the following order on the remote host: print "Hello, World!", list the contents of the remote directory, and display the current working directory.

Example: Create a file named local_script.sh with the following contents:

cd /var/log
grep "error" syslog | wc -l
tail -n 5 syslog

Then execute the script on the remote host:

ssh user@host 'bash -s' < local_script.sh

This example will change to the /var/log directory, count the number of lines containing "error" in the syslog file, and display the last 5 lines of the syslog file on the remote host.

 

7. Using a Here Document

This method allows you to execute multiple commands on a remote system using a Here Document to embed commands within the SSH command.

Syntax:

ssh user@host << 'EOF'
command1
command2
command3
EOF

This example will execute the commands in the following order on the remote host: print "Hello, World!", list the contents of the remote directory, and display the current working directory.

ssh user@host << 'EOF'
echo "Hello, World!"
ls
pwd
EOF

This example will change to the /var/log directory, count the number of lines containing "error" in the syslog file, and display the last 5 lines of the syslog file on the remote host.

ssh user@host << 'EOF'
cd /var/log
grep "error" syslog | wc -l
tail -n 5 syslog
EOF

 

Summary

In summary, there are multiple methods to execute several commands on a remote system via SSH. These methods include semicolon-separated commands, double ampersand-separated commands, single ampersand-separated commands, command grouping, using a subshell, executing a local script remotely, and using a Here Document. Each method has its own syntax and use case, allowing for flexibility in executing commands sequentially, conditionally, or in parallel, depending on your specific requirements. By leveraging these techniques, you can efficiently manage remote systems and streamline your workflow when working with SSH connections.

 

References

What is the cleanest way to ssh and run multiple commands in Bash?

 

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment