Mastering the `pssh` Command (with Examples)
The pssh
command, standing for Parallel SSH, is an incredibly powerful tool for system administrators and IT professionals who frequently work with multiple servers. Unlike standard SSH, which connects to one server at a time, pssh
enables the execution of commands across multiple nodes simultaneously. This capability can save significant amounts of time and streamline workflows, particularly in large-scale server environments.
Use Case 1: Running a Command on Two Hosts and Printing Output Inline
Code:
pssh -i -H "host1 host2" hostname -i
Motivation: In scenarios where you want to quickly verify the hostnames of multiple servers, this command is invaluable. Imagine needing to confirm the identity of several machines in your network after a maintenance update; this command allows you to do that efficiently in one go.
Explanation:
pssh
: Invokes the Parallel SSH command.-i
: Ensures that the results are printed inline, making it easier to correlate outputs with their respective hosts.-H "host1 host2"
: Specifies the list of remote hosts (e.g.,host1
andhost2
) on which to run the command.hostname
: The command to be executed on each host.
Example Output:
[host1] out: host1.domain.com
[host2] out: host2.domain.com
Use Case 2: Running a Command and Saving Output to Separate Files
Code:
pssh -H host1 -H host2 -o path/to/output_dir hostname -i
Motivation: When you need to archive the command output from different servers for later analysis, possibly for audit purposes or troubleshooting, saving each output separately in a structured manner is essential.
Explanation:
pssh
: Starts the Parallel SSH command.-H host1 -H host2
: Identifies the target hosts (e.g.,host1
andhost2
).-o path/to/output_dir
: Directspssh
to store the output of each command execution in a separate file within the specified directory.hostname
: Specifies the command to be executed.
Example Output:
- Files saved in
path/to/output_dir
:host1
host2
Each file contains its respective output similar to:
host1.domain.com
Use Case 3: Running a Command on Multiple Hosts, Specified in a File
Code:
pssh -i -h path/to/hosts_file hostname -i
Motivation: As server environments grow, managing multiple hostnames becomes cumbersome. Using a file to store hostnames allows for easier updates and maintenance without altering the command syntax, beneficial for executing routine tasks across all listed servers.
Explanation:
pssh
: Executes the Parallel SSH task.-i
: Prints command outputs inline.-h path/to/hosts_file
: Points to the file containing a newline-separated list of hosts.hostname
: The command to execute on each host listed in the file.
Example Output:
[host1] out: host1.domain.com
[host2] out: host2.domain.com
...
Use Case 4: Running a Command as Root
Code:
pssh -i -h path/to/hosts_file -A -l root_username hostname -i
Motivation: Certain server commands require root-level access. This command configuration enables administrators to execute commands requiring elevated privileges across multiple machines simultaneously, ensuring consistency and reducing manual errors.
Explanation:
pssh
: Initiates the Parallel SSH execution.-i
: Ensures outputs are printed inline with the host information.-h path/to/hosts_file
: Uses a file to list target hosts.-A
: Prompts for a password input, enabling secure execution of commands as root.-l root_username
: Specifies the user to employ for SSH, in this case, the root.hostname
: The command executed on all specified hosts.
Example Output:
Password:
[host1] out: host1.domain.com
[host2] out: host2.domain.com
Use Case 5: Running a Command with Extra SSH Arguments
Code:
pssh -i -h path/to/hosts_file -x "-O VisualHostKey=yes" hostname -i
Motivation: Customizing SSH options can sometimes help in bypassing potential security checks or accommodating specific SSH configurations required by different environments. This example demonstrates how extra SSH parameters can enhance command flexibility.
Explanation:
pssh
: Executes in parallel across specified hosts.-i
: Designates inline output format.-h path/to/hosts_file
: Path to the file listing the SSH target hosts.-x "-O VisualHostKey=yes"
: Custom SSH parameters – in this case, enabling visibility of the host key.hostname
: Command to perform on each targeted server.
Example Output:
[host1] out: host1.domain.com
[host2] out: host2.domain.com
Use Case 6: Limiting Parallel Connections
Code:
pssh -i -h path/to/hosts_file -p 10 'cd dir; ./script.sh; exit'
Motivation: When operating in resource-constrained environments or when server load considerations are paramount, limiting simultaneous connections can prevent server overloads, ensuring stability and reliability.
Explanation:
pssh
: The parallel SSH command, running on specified servers.-i
: Indicates inline output.-h path/to/hosts_file
: Specifies the file containing hostnames.-p 10
: Limits the number of concurrent SSH connections to 10.'cd dir; ./script.sh; exit'
: Compound command to navigate directories, execute scripts, and exit gracefully.
Example Output:
[host1] out: Script executed successfully
[host2] out: Script executed successfully
...
Conclusion
The pssh
command is a versatile utility tailored for managing multiple remote servers concurrently. By employing different flags and options, users can customize its behavior to suit a variety of operational needs, thereby boosting efficiency and reducing the complexity involved in server management tasks. These examples only scratch the surface, but they exemplify the range of possibilities available with pssh
.