侧边栏壁纸
Linux入门自学网博主等级

每日学一条Linux命令,终成Linux大神

  • 累计撰写 725 篇文章
  • 累计创建 143 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Linux pssh命令教程:如何并行在多台Linux服务器上执行ssh命令(附实例详解和注意事项)

Parallel SSH (pssh) 是一种实用工具,它允许同时在多个服务器上执行命令。这对于系统管理员来说极为有用,特别是当需要在集群或大量服务器上执行相同的任务时。pssh可以大幅提高工作效率,减少重复性工作。

Linux pssh命令介绍

Parallel SSH(pssh)是一种使用户能够并行在多台Linux服务器上执行SSH命令的工具。它是Python编写的一组程序,包括pssh本身,还有pscp、prsync、pslurp和pnuke等工具。pssh通过在多台服务器上并行执行命令来提高任务执行的效率。

Linux pssh命令适用的Linux版本

pssh适用于大多数Linux发行版,包括但不限于Ubuntu, Debian, Fedora, CentOS, 和 Red Hat。不过,某些较旧的发行版,如CentOS 7,可能没有预装pssh,需要手动安装。

  • 对于基于Debian的系统(如Ubuntu),可以使用以下命令安装:

    [linux@bashcommandnotfound.cn ~]$ sudo apt-get install pssh
    ```
    
    
  • 对于CentOS 7,可以使用以下命令:

    [linux@bashcommandnotfound.cn ~]$ sudo yum install pssh
    ```
    
    
  • 对于CentOS 8,可以使用dnf

    [linux@bashcommandnotfound.cn ~]$ sudo dnf install pssh
    ```
    

Linux pssh命令的基本语法

pssh的基本语法如下:

pssh [OPTIONS] -h hosts.txt -l user -A -i "COMMAND"

这里,hosts.txt 是一个包含主机名或IP地址的文件,每行一个;user 是远程主机的用户名;-A 参数提示输入密码;-i 参数表示立即显示输出。

Linux pssh命令的常用选项或参数说明

选项描述
-h指定包含主机列表的文件
-l指定登录名
-p同时运行的最大进程数
-o输出文件的目录
-e错误输出文件的目录
-A提示输入密码
-i显示每个节点的输出
-t指定超时时间(秒)
-OSSH选项,可以用来指定端口等
-x传递给ssh的额外参数
-X传递给scp的额外参数
-P提示输入密码,用于scp
-v显示详细信息
-I指定输入文件,其内容会发送到远程节点上的命令
-H直接指定远程主机,而不是通过文件

Linux pssh命令实例详解

实例1:在多台服务器上执行命令

假设我们有一个名为hosts.txt的文件,其中包含了服务器列表,我们需要在所有服务器上检查当前日期和时间。

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A -I "date"

实例2:并行复制文件到多台服务器

假设我们需要将本地的file.txt复制到所有服务器的/tmp目录。

[linux@bashcommandnotfound.cn ~]$ pscp -h hosts.txt -l username -A file.txt /tmp

实例3:并行执行具有多个选项的命令

假设我们需要在所有服务器上查找名为httpd的进程。

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A -I "ps aux | grep httpd"

实例4:指定SSH选项来执行命令

如果需要特定的SSH选项,如指定非标准端口或使用不同的加密密钥,可以使用-O参数。

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A -O 'Port=2222' -O 'IdentityFile=~/.ssh/custom_id_rsa' "echo 'Hello, World!'"

实例5:设置命令超时

当你想要限制每个SSH会话的执行时间时,使用-t参数来设置超时(单位为秒)。

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A -t 30 "sleep 60; echo 'This will not show up if timeout works'"

在这个例子中,由于命令的执行时间超过了设置的超时时间,所以echo命令将不会执行。

实例6:并行重启多台服务器

在系统维护过程中,有时需要批量重启服务器。以下命令将并行重启列表中的所有服务器:

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A -t 60 "sudo reboot"

在这个例子中,-t 60设置了60秒的超时时间以防命令挂起。

实例7:收集多台服务器的硬件信息

如果你想要收集所有服务器的硬件信息,例如CPU信息,可以使用以下命令:

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A "lscpu"

实例8:检查多台服务器的磁盘使用情况

为了检查所有服务器的磁盘使用情况,可以使用df命令。

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A "df -h"

这将在所有服务器上显示磁盘空间使用情况。

实例9:并行更新多台服务器

如果你需要在多台服务器上执行系统更新,可以使用以下命令:

# 对于基于Debian的系统
[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A "sudo apt-get update && sudo apt-get upgrade -y"

# 对于基于Red Hat的系统
[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A "sudo yum update -y"

实例10:在多台服务器上创建相同的用户账号

如果需要在一群服务器上创建一个新用户,可以并行执行useradd命令:

[linux@bashcommandnotfound.cn ~]$ pssh -i -h hosts.txt -l username -A "sudo useradd newuser"

Linux pssh命令的注意事项

  • 确保所有服务器的用户名和密码都是正确的,否则命令会失败。
  • 如果连接到远程服务器时提示pssh: command not found,请按照上面的指示安装pssh。
  • 使用pssh时,请注意网络的安全性和带宽,因为大量的并发连接可能会对网络造成影响。
0

评论区