Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using PSSH to manage Linux server

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about using PSSH to manage the Linux server. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

1) install pssh

It can be installed directly by yum:

[root@bastion-IDC ~] # yum install-y pssh

2) pssh usage

[root@bastion-IDC] # pssh-- help

-h remote host list file that executes the command

-H user@ip:port file content format [user@] host [: port]

-l user name of the remote machine

-p how many connections are allowed at a time

-o the output is redirected to a file

-e perform error redirection to a file

-t sets the timeout for command execution

-A prompts for the password and passes it to ssh (note that when this parameter is added, it is only a prompt. You can enter it or enter it directly)

-O set the specific configuration of ssh parameters, refer to ssh_config configuration file

-x passes multiple SSH commands separated by spaces and enclosed in quotation marks

-X is the same as-x, but can only pass one command at a time.

-I display standard output and standard error after the execution of each host

-I reads each input command and passes it to the ssh process to allow command scripts to be passed to standard input

3) pssh instance description

The format of the information in the [root@bastion-IDC ~] # cat hosts.txt / / list file is "ip: Port". If the local machine and the remote machine use the same ssh port, you can omit the port and use ip directly. However, it is recommended that you take all the ports with you.

192.168.1.101:22

192.168.1.109:22

192.168.1.118:25791

192.168.1.105:25791

For example, the above four machines are placed in a list file hosts.txt, and the machine has made a ssh password-free login trust relationship with these four machines.

Note: the machine in the list file must have a ssh trust relationship with the local machine in advance. If it is not done, then the pssh batch execution will not be executed when it is the machine's turn to do the trust relationship.

A) batch execution of commands

[root@bastion-IDC] # pssh-h hosts.txt-l root-I 'uptime'

[1] 16:05:48 [SUCCESS] 192.168.1.105

03:03:25 up 79 days, 13:44, 0 users, load average: 0.04, 0.01, 0.00

[2] 16:05:48 [SUCCESS] 192.168.1.118

03:03:32 up 75 days, 15:27, 4 users, load average: 0.96, 0.74, 0.45

Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

[3] 16:05:48 [SUCCESS] 192.168.1.109

03:03:25 up 61 days, 21:56, 2 users, load average: 0.02, 0.06, 0.18

Stderr: Address 192.168.1.102 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

[4] 16:05:48 [SUCCESS] 192.168.1.101

16:03:17 up 35 days, 23:45, 1 user, load average: 0.03, 0.04, 0.01

Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

If you add the-A parameter, you will be prompted for a password even if the ssh trust relationship is done in advance!

[root@bastion-IDC] # pssh-h hosts.txt-l root-I-A 'uptime'

Warning: do not enter your password if anyone else has superuser

Privileges or access to your account.

Password: / / Note that this parameter is added as a prompt. You can enter it here or enter directly without entering it.

[1] 16:08:25 [SUCCESS] 192.168.1.105

03:06:03 up 79 days, 13:46, 0 users, load average: 0.00, 0.00, 0.00

[2] 16:08:25 [SUCCESS] 192.168.1.109

03:06:03 up 61 days, 21:59, 2 users, load average: 0.00, 0.04, 0.15

Stderr: Address 192.168.1.102 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

[3] 16:08:25 [SUCCESS] 192.168.1.101

16:05:54 up 35 days, 23:47, 1 user, load average: 0.00, 0.02, 0.00

Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

[4] 16:08:25 [SUCCESS] 192.168.1.118

03:06:10 up 75 days, 15:29, 4 users, load average: 0.85, 0.78, 0.51

Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

[root@bastion-IDC] # pssh-h hosts.txt-l root-I-t 10-o / root/pssh.log 'uptime & & date'

[1] 17:01:02 [SUCCESS] 192.168.1.109

03:58:33 up 79 days, 5:58, 1 user, load average: 0.00, 0.00, 0.00

Wed Feb 8 03:58:33 EST 2017

[2] 17:01:02 [SUCCESS] 192.168.1.105

03:58:40 up 79 days, 14:39, 1 user, load average: 0.00, 0.00, 0.00

Wed Feb 8 03:58:40 EST 2017

[3] 17:01:02 [SUCCESS] 192.168.1.101

16:58:31 up 36 days, 40 min, 1 user, load average: 0.10, 0.03, 0.01

Wed Feb 8 16:58:31 CST 2017

Stderr: Address 192.168.1.101 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

[4] 17:01:02 [SUCCESS] 192.168.1.118

03:58:47 up 75 days, 16:22, 3 users, load average: 0.20, 0.21, 0.31

Wed Feb 8 03:58:47 EST 2017

Stderr: Address 192.168.1.118 maps to localhost, but this does not map back to the address-POSSIBLE BREAK-IN ATTEMPT!

[root@bastion-IDC ~] # ll / root/pssh.log/

Total 16

-rw-r--r--. 1 root root 100 Feb 8 17:01 192.168.1.101

-rw-r--r--. 1 root root 99 Feb 8 17:01 192.168.1.105

-rw-r--r--. 1 root root 99 Feb 8 17:01 192.168.1.109

-rw-r--r--. 1 root root 100 Feb 8 17:01 192.168.1.118

B) upload files or directories in bulk (pscp.pssh command)

Batch upload the local file / mnt/test.file to the / tmp directory on the remote server:

[root@bastion-IDC] # pscp.pssh-l root-h hosts.txt / mnt/test.file / tmp/

[1] 16:18:05 [SUCCESS] 192.168.1.105

[2] 16:18:05 [SUCCESS] 192.168.1.109

[3] 16:18:05 [SUCCESS] 192.168.1.101

[4] 16:18:05 [SUCCESS] 192.168.1.118

Batch upload local files / mnt/test.file, / mnt/aa.file, / mnt/bb.file to the / tmp directory on the remote server:

[root@bastion-IDC ~] # pscp.pssh-l root-h hosts.txt / mnt/test.file / mnt/aa.file / mnt/bb.file / tmp/

[1] 16:22:50 [SUCCESS] 192.168.1.109

[2] 16:22:50 [SUCCESS] 192.168.1.105

[3] 16:22:50 [SUCCESS] 192.168.1.118

[4] 16:22:50 [SUCCESS] 192.168.1.101

Or:

[root@bastion-IDC ~] # pscp.pssh-l root-h hosts.txt / mnt/ {test.file,aa.file,bb.file} / tmp/

[1] 16:23:44 [SUCCESS] 192.168.1.109

[2] 16:23:44 [SUCCESS] 192.168.1.105

[3] 16:23:44 [SUCCESS] 192.168.1.101

[4] 16:23:44 [SUCCESS] 192.168.1.118

Batch upload the local directory / mnt/zhong to the / tmp directory on the remote server (you need to add the-r parameter to upload the directory):

[root@bastion-IDC] # pscp.pssh-l root-h hosts.txt-r / mnt/zhong / tmp/

[1] 16:19:36 [SUCCESS] 192.168.1.109

[2] 16:19:36 [SUCCESS] 192.168.1.105

[3] 16:19:36 [SUCCESS] 192.168.1.101

[4] 16:19:36 [SUCCESS] 192.168.1.118

Batch upload local directories / mnt/zhong, / mnt/aa, / mnt/vv to the / tmp directory on the remote server

[root@bastion-IDC] # pscp.pssh-l root-h hosts.txt-r / mnt/zhong / mnt/aa / mnt/vv / tmp/

[1] 16:21:02 [SUCCESS] 192.168.1.105

[2] 16:21:02 [SUCCESS] 192.168.1.109

[3] 16:21:02 [SUCCESS] 192.168.1.101

[4] 16:21:02 [SUCCESS] 192.168.1.118

Or:

[root@bastion-IDC ~] # pscp.pssh-l root-h hosts.txt-r / mnt/ {zhong,aa,vv} / tmp/

[1] 16:22:00 [SUCCESS] 192.168.1.109

[2] 16:22:00 [SUCCESS] 192.168.1.105

[3] 16:22:00 [SUCCESS] 192.168.1.101

[4] 16:22:00 [SUCCESS] 192.168.1.118

C) batch download of files or directories (pslurp command)

If you batch download a file from the server locally, you don't have to worry about duplicate names, because pssh has established a directory with the name of ip in the file list to store the downloaded files:

[root@bastion-IDC] # pslurp-l root-h hosts.txt / etc/hosts.

[1] 16:32:01 [SUCCESS] 192.168.1.109

[2] 16:32:01 [SUCCESS] 192.168.1.105

[3] 16:32:01 [SUCCESS] 192.168.1.101

[4] 16:32:01 [SUCCESS] 192.168.1.118

[root@bastion-IDC ~] # ll

Total 123

Drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.101

Drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.105

Drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.109

Drwxr-xr-x. 2 root root 4096 Feb 8 16:32 192.168.1.118

[root@bastion-IDC ~] # ll 192.168.1.101

Total 4

-rw-r--r--. 1 root root 224 Feb 8 16:32 hosts

[root@bastion-IDC ~] # ll 192.168.1.109

Total 4

-rw-r--r--. 1 root root 252 Feb 8 16:32 hosts

[root@bastion-IDC ~] # ll 192.168.1.105

Total 4

-rw-r--r--. 1 root root 252 Feb 8 16:32 hosts

[root@bastion-IDC ~] # ll 192.168.1.118

Total 4

-rw-r--r--. 1 root root 212 Feb 8 16:32 hosts

In addition, pay special attention to:

The above batch download operation can only be downloaded to the local current directory, not the specified path in the command:

[root@bastion-IDC] # pslurp-l root-h hosts.txt / etc/hosts / mnt/

[1] 16:34:14 [FAILURE] 192.168.1.109 Exited with error code 1

[2] 16:34:14 [FAILURE] 192.168.1.105 Exited with error code 1

[3] 16:34:14 [FAILURE] 192.168.1.101 Exited with error code 1

[4] 16:34:14 [FAILURE] 192.168.1.118 Exited with error code 1

To download to the local / mnt directory, the right thing to do is to change to the / mnt directory, and then execute the download command: (list files follow the full path)

[root@bastion-IDC ~] # cd / mnt/

[root@bastion-IDC mnt] # pslurp-l root-h / root/hosts.txt / etc/hosts. /

[1] 16:34:34 [SUCCESS] 192.168.1.109

[2] 16:34:34 [SUCCESS] 192.168.1.105

[3] 16:34:34 [SUCCESS] 192.168.1.118

[4] 16:34:34 [SUCCESS] 192.168.1.101

[root@bastion-IDC mnt] # ll

Total 16

Drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.101

Drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.105

Drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.109

Drwxr-xr-x. 2 root root 4096 Feb 8 16:34 192.168.1.118

The above is the batch download file, if the batch download directory, just need to add a-r parameter!

[root@bastion-IDC mnt] # pslurp-l root-h / root/hosts.txt-r / home/. /

[1] 16:39:05 [SUCCESS] 192.168.1.109

[2] 16:39:05 [SUCCESS] 192.168.1.105

[3] 16:39:05 [SUCCESS] 192.168.1.101

[4] 16:39:05 [SUCCESS] 192.168.1.118

[root@bastion-IDC mnt] # ll 192.168.1.101

Total 8

Drwxr-xr-x. 6 root root 4096 Feb 8 16:39 home

-rw-r--r--. 1 root root 224 Feb 8 16:38 hosts

[root@bastion-IDC mnt] # ll 192.168.1.*

192.168.1.101:

Total 8

Drwxr-xr-x. 6 root root 4096 Feb 8 16:39 home

-rw-r--r--. 1 root root 224 Feb 8 16:38 hosts

192.168.1.105:

Total 8

Drwxr-xr-x. 4 root root 4096 Feb 8 16:39 home

-rw-r--r--. 1 root root 252 Feb 8 16:38 hosts

192.168.1.109:

Total 8

Drwxr-xr-x. 4 root root 4096 Feb 8 16:39 home

-rw-r--r--. 1 root root 252 Feb 8 16:38 hosts

192.168.1.118:

Total 8

Drwxr-xr-x. 3 root root 4096 Feb 8 16:39 home

-rw-r--r--. 1 root root 212 Feb 8 16:38 hosts

D) batch synchronization (prsync command)

Synchronize files or directories in the local / mnt/test directory to the / mnt/test path of the remote machine

[root@bastion-IDC] # prsync-l root-h hosts.txt-r / mnt/test/ / mnt/test/

[1] 16:46:41 [SUCCESS] 192.168.1.109

[2] 16:46:41 [SUCCESS] 192.168.1.105

[3] 16:46:41 [SUCCESS] 192.168.1.118

[4] 16:46:41 [SUCCESS] 192.168.1.101

Synchronize files or directories in the local / mnt/test directory to the / mnt path of the remote machine

[root@bastion-IDC] # prsync-l root-h hosts.txt-r / mnt/test/ / mnt/

[1] 16:47:40 [SUCCESS] 192.168.1.109

[2] 16:47:40 [SUCCESS] 192.168.1.105

[3] 16:47:45 [SUCCESS] 192.168.1.101

[4] 16:47:46 [SUCCESS] 192.168.1.118

Note:

The above batch synchronization directory operation is to synchronize the local corresponding directory data to the remote machine, and the redundant files in the directory will be retained on the remote machine (the redundant files will not be deleted).

Similarly, batch synchronize file operations and remove the-r parameter

Note: when synchronizing files, it is actually completely overwritten, and all the files in the corresponding files on the remote machine will be replaced!

As follows:

Synchronize the local / mnt/test/file file contents to the remote server / mnt/test/file file

[root@bastion-IDC] # prsync-l root-h hosts.txt / mnt/test/file / mnt/test/file

[1] 16:53:54 [SUCCESS] 192.168.1.109

[2] 16:53:54 [SUCCESS] 192.168.1.105

[3] 16:53:54 [SUCCESS] 192.168.1.101

[4] 16:53:54 [SUCCESS] 192.168.1.118

[root@bastion-IDC] # prsync-l root-h hosts.txt / mnt/test/file / mnt/aaa

[1] 16:54:03 [SUCCESS] 192.168.1.109

[2] 16:54:03 [SUCCESS] 192.168.1.105

[3] 16:54:03 [SUCCESS] 192.168.1.101

[4] 16:54:04 [SUCCESS] 192.168.1.118

E) processes on batch kill remote machines (pnuke command)

For example, batch kill drops nginx processes on remote machines.

[root@bastion-IDC] # pnuke-h hosts.txt-l root nginx

[1] 17:09:14 [SUCCESS] 192.168.1.109

[2] 17:09:14 [SUCCESS] 192.168.1.105

[3] 17:09:15 [SUCCESS] 192.168.1.118

[4] 17:09:15 [SUCCESS] 192.168.1.101

After reading the above, do you have any further understanding of using PSSH to manage the Linux server? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report