Copying Files to Multiple Systems Simultaneously with PSSH
Have you ever wondered if there is a simple way to copy files and run commands on multiple machines from one command?
Well there are several tools available that will allow us to do just that, but for starters we will discuss just pssh (Parallel SSH).
pssh (Parallel SSH)
pssh (Parallel SSH) is a command-line tool that includes pscp and allows you to send commands and copy files to multiple server via a list or individually. It makes it very easy to distribute commands and files to a few or hundreds of servers.
Installation is pretty simple with Fedora and RHEL/CentOS:
sudo yum install pssh
Say I wanted to copy two files to multiple hosts to the /tmp directory and limit the command to execute to 5 systems at a time and execute as root I would run the following command on RHEL/CentOS 5.x and higher:
pscp.pssh -vA -h ~/myhosts.txt -l id -p 5 -e /tmp upgrade_prog upgrade_prog.tar.gz /tmp/
-vA switch tells pscp to run in verbose mode and ask for a password.
-h switch tells pscp the list of hosts are located in this file and this location.
-p switch says execute on 5 systems at a time
-e switch tells pscp to output the error log to /tmp on my system
Then finally, the files to send and the destination on the systems.
NAME pscp — parallel process kill program SYNOPSIS pscp [-vAr] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par] [-o outdir] [-e errdir] [-t timeout] [-O options] [-x args] [-X arg] local remote DESCRIPTION pscp is a program for copying files in parallel to a number of hosts. It provides features such as passing a password to scp, saving output to files, and timing out. OPTIONS -h host_file --hosts host_file
Read hosts from the given host_file. Lines in the host file are of the form [user@]host[:port] and can include blank
lines and comments (lines beginning with “#”). If multiple host files are given (the -h option is used more than once),
then pscp behaves as though these files were concatenated together. If a host is specified multiple times, then pscp will
connect the given number of times.
-H [user@]host[:port] --host [user@]host[:port] -H "[user@]host[:port] [ [user@]host[:port ] ... ]" --host "[user@]host[:port] [ [user@]host[:port ] ... ]" Add the given host strings to the list of hosts. This option may be given multiple times, and may be used in conjunction with the -h option. -l user --user user Use the given username as the default for any host entries that don't specifically specify a user. -p parallelism --par parallelism Use the given number as the maximum number of concurrent connections. -t timeout --timeout timeout Make connections time out after the given number of seconds. With a value of 0, pscp will not timeout any connections. -o outdir --outdir outdir
Save standard output to files in the given directory. Filenames are of the form [user@]host[:port][.num] where the user
and port are only included for hosts that explicitly specify them. The number is a counter that is incremented each time
for hosts that are specified more than once.
-e errdir --errdir errdir Save standard error to files in the given directory. Filenames are of the same form as with the -o option. -x args --extra-args args Passes extra SSH command-line arguments (see the ssh(1) man page for more information about SSH arguments). This option may be specified multiple times. The arguments are processed to split on whitespace, protect text within quotes, and escape with backslashes. To pass arguments without such processing, use the -X option instead. -X arg --extra-arg arg Passes a single SSH command-line argument (see the ssh(1) man page for more information about SSH arguments). Unlike the -x option, no processing is performed on the argument, including word splitting. To pass multiple command-line arguments, -O options --options options SSH options in the format used in the SSH configuration file (see the ssh_config(5) man page for more information). This option may be specified multiple times. -A --askpass
Prompt for a password and pass it to SSH. The password may be used for either to unlock a key or for password authentication. The password is transferred in a fairly secure manner. e.g. It will not show up in argument list. However, be
aware that a Root user on your system could potentially intercept the password.
-v --verbose Include error messages from ssh with the -i and \ options. -r --recursive Recursively copy directories. TIPS The ssh_config file can include an arbitrary number of Host sections. Each host entry specifies ssh options which apply only to the given host. Host definitions can even behave like aliases if the HostName option is included. This ssh feature, in combina- tion with pssh host files, provides a tremendous amount of flexibility. EXIT STATUS The exit status codes from pscp are as follows: 0 Success 1 Miscellaneous error 2 Syntax or usage error 3 At least one process was killed by a signal or timed out. 4 All processes completed, but at least one scp process reported an error (exit status other than 0).
As with PSCP you can use PSSH to run Command multiple hosts. The syntax is similar to PSCP.
pssh -vA -h ~/myhosts.txt -l id -p 5 -t 5 -e /tmp -o /tmp hostname
This example is running the hostname command on the list of servers and writes the output to the /tmp directory.
Using the two commands you can accomplish many tasks on multiple servers with a short amount of time and effort.
Also note, when using pssh or pssh.pscp for the first time you will receive a failure, this is due to the ssh key not existing in your known_hosts file. If you use the -X arg option you can pass -oStrictHostKeyChecking=no ssh parameter and it will add the key to your known_hosts file. Using -x args you can string multiple ssh options.
pssh -vA -h ~/myhosts.txt -l id -p 5 -t 5 -e /tmp -o /tmp -X "-oStrictHostChecking=no" hostname
Like us on Facebook
We need your assistance to stay live
This week Top Posts
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX : Wow! Early last week it was all over the internet, if you haven't heard it yet then this is the t...0 comments |
- Grive: An Unofficial, Open Source Linux Client For Google Drive : A couple of months before, we have featured a Linux client for Google Drive named Syncdrive, which w...1 comment |
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- How To "Fix E: Could not open file /var/lib/dpkg/status" : QUESTION: I get the following error when I use sudo apt-get update: E: Could not open file /var/...0 comments |
- Install Sublime Text 3 In Fedora Easily With Fedy : Sublime Text is a sophisticated text editor for code, markup and prose. In this tutorial we are goi...0 comments |
- Install nSnake Game In Terminal : From github repo nsnake is a clone of the classic snake game that we all used to play on our cellp...0 comments |
- Install Sublime Text 3 In Fedora Easily With Fedy
- Grive: An Unofficial, Open Source Linux Client For Google Drive
- Install nSnake Game In Terminal
- How To “Fix E: Could not open file /var/lib/dpkg/status”
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX
- Fedora 21 Scheduled For Release In October
- Install Uget Download Manager In Ubuntu, Fedora, Debian
- Download Elementary OS ‘Luna’ Official Wallpapers
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04
- 5 Android Apps that’s Going to Make Running Your Business Less Stressful
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .