How to use the command 'pssh' (with examples)

How to use the command 'pssh' (with examples)

The command ‘pssh’ stands for parallel SSH and is a program that allows users to run commands on multiple remote servers simultaneously. It is especially useful for running commands in parallel on a large number of hosts.

Use case 1: Run a command on two hosts, and print its output on each server inline:

Code:

pssh -i -H "host1 host2" hostname -i

Motivation:

This use case is beneficial when you want to run a command on multiple hosts and view the output of each server inline. It helps in comparing the results or making sure the command is executed properly on all the hosts.

Explanation:

  • -i: Specifies that the output of the command should be printed on each server inline.

Example output:

[1] 19:43:49 [SUCCESS] host1: host1
[2] 19:43:49 [SUCCESS] host2: host2

Use case 2: Run a command and save the output to separate files:

Code:

pssh -H host1 -H host2 -o path/to/output_dir hostname -i

Motivation:

This use case is useful when you want to run a command on multiple hosts and save the output of each host to separate files. It helps in organizing and analyzing the command output later.

Explanation:

  • -o path/to/output_dir: Specifies the directory where the output files should be saved. The output files will have the format “host.txt”, where “host” represents the hostname of each server.

Example output:

Files saved in the specified output directory:

  • path/to/output_dir/host1.txt: Output of the command on host1.
  • path/to/output_dir/host2.txt: Output of the command on host2.

Use case 3: Run a command on multiple hosts, specified in a new-line separated file:

Code:

pssh -i -h path/to/hosts_file hostname -i

Motivation:

This use case is beneficial when there are a large number of hosts to run a command on and listing them all in the command line would be tedious. By specifying a new-line separated file with hostnames, it becomes easier to manage and add/remove hosts as needed.

Explanation:

  • -h path/to/hosts_file: Specifies the path to the file containing the list of hostnames. Each hostname should be listed on a new line.

Example output:

[1] 19:43:49 [SUCCESS] host1: host1
[2] 19:43:49 [SUCCESS] host2: host2

Use case 4: Run a command as root (this asks for the root password):

Code:

pssh -i -h path/to/hosts_file -A -l root_username hostname -i

Motivation:

This use case is useful when you need to run a command as the root user on multiple hosts. By specifying the root_username, the command will prompt for the root password for each host, ensuring secure access.

Explanation:

  • -A: Specifies that the command should prompt for a password. In this case, it will prompt for the root password for each host.
  • -l root_username: Specifies the username to use when connecting to the hosts as root.

Example output:

[1] 19:43:49 [SUCCESS] host1: host1
[2] 19:43:49 [SUCCESS] host2: host2

Use case 5: Run a command with extra SSH arguments:

Code:

pssh -i -h path/to/hosts_file -x "-O VisualHostKey=yes" hostname -i

Motivation:

This use case is useful when you want to pass extra SSH arguments to the underlying SSH connection. It allows for customization and fine-tuning of the SSH connection settings.

Explanation:

  • -x "-O VisualHostKey=yes": Specifies the extra SSH arguments to be passed to the SSH connection. In this example, the -O VisualHostKey=yes argument is used to enable visual host key presentation.

Example output:

[1] 19:43:49 [SUCCESS] host1: host1
[2] 19:43:49 [SUCCESS] host2: host2

Use case 6: Run a command limiting the number of parallel connections to 10:

Code:

pssh -i -h path/to/hosts_file -p 10 'cd dir; ./script.sh; exit'

Motivation:

This use case is beneficial when running commands simultaneously on a large number of hosts, but you want to limit the number of parallel connections to avoid overloading the network or hosts.

Explanation:

  • -p 10: Specifies the maximum number of parallel connections to use when executing the command. In this example, it limits it to 10.

Example output:

[1] 19:43:49 [SUCCESS] host1: (command output)
[2] 19:43:49 [SUCCESS] host2: (command output)
...
[10] 19:43:50 [SUCCESS] host10: (command output)

Conclusion:

The ‘pssh’ command provides a convenient and efficient way to execute commands on multiple remote servers in parallel. It offers various options for managing the output, running commands as root, providing extra SSH arguments, and controlling the number of parallel connections. With these examples, you now have a good understanding of how to utilize the ‘pssh’ command for your remote server management tasks.

Related Posts

How to use the command fdp (with examples)

How to use the command fdp (with examples)

The fdp command is a part of the graphviz package, and it is used to render an image of a force-directed network graph from a graphviz file.

Read More
Using the vrms command (with examples)

Using the vrms command (with examples)

List non-free and contrib packages (and their description): vrms Motivation: The motivation for using this command is to get a comprehensive list of all the non-free and contrib packages installed on a Debian-based operating system.

Read More
How to use the command searchsploit (with examples)

How to use the command searchsploit (with examples)

The searchsploit command is a powerful tool that allows users to search the exploit database’s database for exploits, shellcodes, and papers.

Read More