Using PARALLEL-SSH to bulk execute remote shell commands

Source: Internet
Author: User
Tags print print ssh
pssh Usage Scenarios

Assuming that you now need to configure updates for hundreds of server nodes or perform some short commands, and there is currently no complete deployment tool software, you can choose to log in to the remote terminal in parallel to pssh and execute the shell tool that specifies the command.
It's fine to write a for loop directly with the shell before the machine node is small. When the number of nodes is large, a shell command can take a few seconds to feel the benefits of Pssh's parallel approach, saving time and effort. pssh Optional configuration parameter list

In addition to PSSH, you can use the Sshpass command when you need to pass a login password:

pintai@mg:~/bak$ pssh--help usage:pssh [OPTIONS] command [...]  Options:--version Show Program's version number and exit--help Show this help message and Exit-h Host_file,--hosts=host_file hosts FILE (each line "[user@]host[:p ORT]")-H Host_stri NG,--host=host_string Additional host entries ("[user@]host[:p ORT]")-l user,--user=user user
                        Name (OPTIONAL)-P PAR,--par=par max number of parallel threads (OPTIONAL)-O OUTDIR,--outdir=outdir Output directory for stdout files (OPTIONAL)-e errdir,--errdir=errdir output dire Ctory for stderr files (OPTIONAL)-t timeout,--timeout=timeout timeout (secs) (0 = no timeout) Per host (OPTIONAL)-o-option,--option=option SSH OPTION (OPTIONAL)-V,--verbose Tur N on warning and diagnostic messages (OPTIONAL)-A,--askpass Ask forA password (OPTIONAL)-X ARGS,--extra-args=args Extra command-line arguments, with processing f or spaces, quotes, and Backslashes-x ARG,--extra-arg=arg Extra command -line argument-i,--inline inline aggregated output and error for each server--inline-stdout inline s           Tandard output for each server-i,--send-input read from standard input and send as input to Ssh-p,--print Print output as we get it example:pssh-h hosts.txt-l irb2-o/tmp/foo uptime pintai@mg:~/bak$ sshpass--help s Shpass:invalid option--'-' usage:sshpass [-F|-D|-P|-E] [-HV] command parameters-f filename take password to use From file-d number use number as file descriptor for getting password-p password provide password as Argumen T (security unwise)-e Password is passed as Env-var ' Sshpass ' with no parameters-password would be take     N from Stdin-h       Show Help (this screen)-V Print version information at the most one of-f,-D,-p OR-E should be used 
1. Pass the login password using Sshpass

The host that requires remote login is set to a file, such as HostList, to write to the host list:

192.168.1.11:22
192.168.1.12:22
192.168.1.13:22

Then write the ssh login password to another file, for example, called Remotepass, write the password:

YourPassword

Finally, the relevant commands are executed to print the output of each node directly:

Sshpass-f remotepass pssh-h hostlist-l yourloginname-a-i "hostname"

The output is as follows

Warning:do Not enter your password if anyone else have superuser
privileges or access to your account.
[1] 11:23:21 [SUCCESS] 192.168.1.11:22
test1.hostname
[2] 11:23:21 [SUCCESS] 192.168.1.12:22
Test2.hostname
[3] 11:23:21 [SUCCESS] 192.168.1.13:22
test3.hostname
2. Output the result to the specified file

If you need to collect the output, you can specify the result output directory with the-O option, such as:

Sshpass-f remotepass pssh-h hostlist-l yourloginname-o outputdir-a "hostname"

Terminal output at execution time:

Warning:do Not enter your password if anyone else have superuser
privileges or access to your account.
[1] 11:23:21 [SUCCESS] 192.168.1.11:22
[2] 11:23:21 [SUCCESS] 192.168.1.12:22
[3] 11:23:21 [SUCCESS] 192.168.1.13:22

The current directory generates a OutputDir directory in which each host occupies one file, such as:

pintai@mg:~/bak$ ls output/
192.168.1.11:22 192.168.1.12:22 192.168.1.13:22
pintai@mg:~/bak$ cat output/*
test1.hostname
test2.hostname
test3.hostname
3. Execute the sudo command

Some shell commands may need to be executed with sudo permissions, which is typically done locally.

echo Your_sudo_pass | Sudo-s Your_command

In Pssh, you can do this:

Sshpass-f remotepass pssh-h hostlist-l yourloginname-o outputdir-a "echo your_sudo_pass | Sudo-s Netstat-antup | grep xxx "

After execution, the specific output can be found in the OutputDir directory. 4. Copy Local files to multiple remote terminals using private key

Pscp-x "-i/locadir/id_rsa"-l yourname-h nodes.txt/tmp/local.txt/remote/dir/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.