• Pssh -- 使用单个终端在多个远程Linux服务器上执行命令


    OpenSSH毫无疑问是可用于Linux的使用最广泛和强大工具之一,它允许你通过一个shell安全地连接到远程Linux系统,并且允许你与远程系统之间来回传输文件。

    但OpenSSH的最大缺点是你不同一次在多台主机上执行相同命令并且没有开发OpenSSH指定这样的任务。这就是并行SSH或者PSSH工具派上用处的地方,它是一个基于python的应用程序,其允许你同时在多台主机上并行地执行命令。

    PSSH工具包含OpenSSH的并行版本和相关工具,诸如:

    • pssh:是一个用于在多个远程主机上并行地运行ssh的程序。
    • pscp:是一个用于在很多主机中复制文件的程序。两个或多个远程linux服务器上复制/传输文件。
    • prsync:一个用于高效地并行复制文件到多台主机地程序。
    • pnuke:在多台主机上并行的杀死进程。
    • pslurp:从多台远程主机并行复制文件到一个中央主机。

    这些工具对处理网络上大量节点集合的系统管理员有帮助。

    在LInux上安装PSSH或并行SSH

    我们将看到在基于Federa发行(诸如AlmaLinux/Rocky LInux/Oracle Linux)上使用pip命令安装最新版本PSSH程序。

    在AlmaLinux/Rocky LInux发行上,为了安装PSSH程序,你需要首先在你的系统上安装pip包:

    1. [blctrl@areadetector data]$ sudo dnf install python3-pip
    2. Last metadata expiration check: 3:00:19 ago on Wed 29 Jun 2022 07:54:19 PM CST.
    3. Package python3-pip-9.0.3-22.el8.rocky.0.noarch is already installed.
    4. Dependencies resolved.
    5. Nothing to do.
    6. Complete!

    当你安装了pip工具,你能够在pip命令帮助下安装pssh包,如下:

    1. [blctrl@areadetector data]$ pip install pssh
    2. Defaulting to user installation because normal site-packages is not writeable
    3. Collecting pssh
    4. Downloading pssh-2.3.1.tar.gz (23 kB)
    5. Preparing metadata (setup.py) ... done
    6. Using legacy 'setup.py install' for pssh, since package 'wheel' is not installed.
    7. Installing collected packages: pssh
    8. Running setup.py install for pssh ... done
    9. Successfully installed pssh-2.3.1

    在CentOS7上进行安装:

    1. [root@station1 blctrl]# yum install python-pip
    2. Loaded plugins: fastestmirror, langpacks
    3. Loading mirror speeds from cached hostfile
    4. ...
    5. Installing:
    6. python2-pip noarch 8.1.2-14.el7 epel 1.7 M
    7. ...
    8. Installed:
    9. python2-pip.noarch 0:8.1.2-14.el7
    10. Complete!
    11. [root@station1 blctrl]# pip install pssh
    12. Collecting pssh
    13. ...
    14. Installing collected packages: pssh
    15. Running setup.py install for pssh ... done
    16. Successfully installed pssh-2.3.1
    17. You are using pip version 8.1.2, however version 22.1.2 is available.
    18. You should consider upgrading via the 'pip install --upgrade pip' command.

    如何使用pssh

    当使用pssh时,你需要创建一个host文件,在这个文件中写入你需要使用pssh连接远程系统每台主机的IP地址和端口号。

    在这个host文件中的行按照如下格式并且也可以包括空行和注释。

    1. 192.168.50.170:22 # AlmaLinux 8.6
    2. 192.168.50.197:22 # Oracle Linux 8.6
    3. 192.168.50.224:22 # CentOS 7.9
    4. 192.168.50.74:22 # CentOS 7.9 执行pssh命令的主机
    5. 192.168.50.150:22 # Rocky Linux 8.6

    使用pssh在多台服务器上执行单个命令。

    你能够通过一条pssh命令在一个网络中不同或者多台linux主机上执行任意单挑命令。如下所述,有与pssh一起使用的很多选项:

    • 要读取host文件,包含-h host_file_name或者-hosts host_file_name选项
    • 要包含在未定义特定用户的所有主机上指定一个默认用户名,使用-l username或者-user username选项。
    • 你也可以在每台主机结束时显示标准输出和标准错误。通过使用-i或-inline选项。
    • 你可以通过包含-t number_of_seconds选项希望在指定秒数后使得连接超时。
    • 要保存标准输出到一个指定目录,你可以使用-o /directory/path选项。
    • 要请求密码并且发送给ssh,使用-A选项。

    让我们看一下pssh命令的一些示例和用法:

    1、在多台Linux主机上通过root用户执行echo "Hello World"(先做免密登录),运行以下命令。

    重要:记住在host文件中必须记住所有主机

    注意:以下pssh-hosts是一个文件,其中内容是你想要执行命令的远程Linux 服务器IP地址和SSH端口号的列表。

    1. [blctrl@station1 ~]$ cat pssh-hosts
    2. 192.168.50.150:22
    3. 192.168.50.197:22
    4. 192.168.50.224:22
    5. 192.168.50.74:22
    6. 192.168.50.170:22
    1. [blctrl@station1 ~]$ pssh -h pssh-hosts -l root echo "Hello World"
    2. [1] 10:31:51 [SUCCESS] 192.168.50.74:22
    3. [2] 10:31:51 [SUCCESS] 192.168.50.224:22
    4. [3] 10:31:51 [SUCCESS] 192.168.50.150:22
    5. [4] 10:31:51 [SUCCESS] 192.168.50.197:22

    2、查找网络上多台Linux主机的磁盘空间,你可以运行以下命令:

    1. [blctrl@station1 ~]$ pssh -h pssh-hosts -l root -i "df -hT"
    2. [1] 10:35:13 [SUCCESS] 192.168.50.74:22
    3. Filesystem Type Size Used Avail Use% Mounted on
    4. devtmpfs devtmpfs 7.7G 0 7.7G 0% /dev
    5. tmpfs tmpfs 7.8G 20M 7.7G 1% /dev/shm
    6. tmpfs tmpfs 7.8G 11M 7.7G 1% /run
    7. tmpfs tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
    8. /dev/mapper/centos-root xfs 250G 8.3G 242G 4% /
    9. /dev/sda2 xfs 1014M 173M 842M 17% /boot
    10. /dev/mapper/centos-home xfs 207G 125M 207G 1% /home
    11. /dev/sda1 vfat 200M 12M 189M 6% /boot/efi
    12. tmpfs tmpfs 1.6G 52K 1.6G 1% /run/user/1000
    13. tmpfs tmpfs 1.6G 8.0K 1.6G 1% /run/user/1001
    14. tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0
    15. [2] 10:35:13 [SUCCESS] 192.168.50.224:22
    16. Filesystem Type Size Used Avail Use% Mounted on
    17. devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
    18. tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
    19. tmpfs tmpfs 1.9G 9.9M 1.9G 1% /run
    20. tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
    21. /dev/mapper/centos-root xfs 36G 4.7G 31G 14% /
    22. /dev/vda1 xfs 1014M 185M 830M 19% /boot
    23. tmpfs tmpfs 379M 4.0K 379M 1% /run/user/42
    24. tmpfs tmpfs 379M 60K 379M 1% /run/user/1000
    25. tmpfs tmpfs 379M 0 379M 0% /run/user/0
    26. [3] 10:35:13 [SUCCESS] 192.168.50.150:22
    27. Filesystem Type Size Used Avail Use% Mounted on
    28. devtmpfs devtmpfs 63G 0 63G 0% /dev
    29. tmpfs tmpfs 63G 0 63G 0% /dev/shm
    30. tmpfs tmpfs 63G 19M 63G 1% /run
    31. tmpfs tmpfs 63G 0 63G 0% /sys/fs/cgroup
    32. /dev/mapper/rl-root xfs 330G 46G 285G 14% /
    33. /dev/mapper/rl-home xfs 595M 132M 463M 23% /home
    34. /dev/sda2 xfs 1014M 186M 829M 19% /boot
    35. /dev/sda4 xfs 596G 51G 545G 9% /home/blctrl
    36. /dev/sda1 vfat 599M 5.7M 594M 1% /boot/efi
    37. tmpfs tmpfs 13G 100K 13G 1% /run/user/1000
    38. tmpfs tmpfs 13G 0 13G 0% /run/user/0
    39. [4] 10:35:13 [SUCCESS] 192.168.50.197:22
    40. Filesystem Type Size Used Avail Use% Mounted on
    41. devtmpfs devtmpfs 1.8G 0 1.8G 0% /dev
    42. tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
    43. tmpfs tmpfs 1.9G 9.4M 1.9G 1% /run
    44. tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
    45. /dev/mapper/almalinux-root xfs 36G 6.1G 29G 18% /
    46. /dev/vda1 xfs 1014M 255M 760M 26% /boot
    47. tmpfs tmpfs 374M 48K 374M 1% /run/user/1001
    48. tmpfs tmpfs 374M 0 374M 0% /run/user/0

    3、如果你想要一次知道多台Linux服务器的uptime,则你运行以下命令。

    1. [blctrl@station1 ~]$ pssh -h pssh-hosts -l root -i "uptime"
    2. [1] 10:37:37 [SUCCESS] 192.168.50.74:22
    3. 10:37:37 up 2 days, 18:43, 5 users, load average: 0.00, 0.01, 0.05
    4. [2] 10:37:37 [SUCCESS] 192.168.50.224:22
    5. 10:37:37 up 13:24, 6 users, load average: 0.00, 0.01, 0.05
    6. [3] 10:37:37 [SUCCESS] 192.168.50.197:22
    7. 10:37:37 up 1 day, 18:55, 2 users, load average: 0.00, 0.00, 0.00
    8. [4] 10:37:37 [SUCCESS] 192.168.50.150:22
    9. 10:37:37 up 5 days, 20:57, 3 users, load average: 2.24, 2.28, 2.11

    4、如果你想要知道多台Linux服务器的/home/blctrl目录下的文件,则运行命令:

    1. [blctrl@station1 ~]$ pssh -h pssh-hosts -l root -i "ls /home/blctrl"
    2. [1] 10:55:48 [SUCCESS] 192.168.50.74:22
    3. 1
    4. data
    5. perl5
    6. pssh-hosts
    7. [2] 10:55:48 [SUCCESS] 192.168.50.224:22
    8. data
    9. Desktop
    10. Documents
    11. Downloads
    12. Music
    13. Pictures
    14. Public
    15. Templates
    16. Videos
    17. [3] 10:55:48 [SUCCESS] 192.168.50.150:22
    18. data
    19. Desktop
    20. diffraction
    21. Documents
    22. Downloads
    23. Music
    24. Pictures
    25. Public
    26. Templates
    27. Videos
    28. [4] 10:55:48 [SUCCESS] 192.168.50.197:22
    29. data
    30. [5] 10:55:49 [SUCCESS] 192.168.50.170:22
    31. Desktop
    32. Documents
    33. Downloads
    34. Music
    35. Pictures
    36. Public
    37. Templates
    38. Videos

    你能够查看pssh命令的手册页获取很多其他选项来找到使用pssh的更多方式。

    1. [blctrl@station1 ~]$ pssh --help
    2. Usage: pssh [OPTIONS] command [...]
    3. Options:
    4. --version show program's version number and exit
    5. --help show this help message and exit
    6. -h HOST_FILE, --hosts=HOST_FILE
    7. hosts file (each line "[user@]host[:port]")
    8. -H HOST_STRING, --host=HOST_STRING
    9. additional host entries ("[user@]host[:port]")
    10. -l USER, --user=USER username (OPTIONAL)
    11. -p PAR, --par=PAR max number of parallel threads (OPTIONAL)
    12. -o OUTDIR, --outdir=OUTDIR
    13. output directory for stdout files (OPTIONAL)
    14. -e ERRDIR, --errdir=ERRDIR
    15. output directory for stderr files (OPTIONAL)
    16. -t TIMEOUT, --timeout=TIMEOUT
    17. timeout (secs) (0 = no timeout) per host (OPTIONAL)
    18. -O OPTION, --option=OPTION
    19. SSH option (OPTIONAL)
    20. -v, --verbose turn on warning and diagnostic messages (OPTIONAL)
    21. -A, --askpass Ask for a password (OPTIONAL)
    22. -x ARGS, --extra-args=ARGS
    23. Extra command-line arguments, with processing for
    24. spaces, quotes, and backslashes
    25. -X ARG, --extra-arg=ARG
    26. Extra command-line argument
    27. -i, --inline inline aggregated output and error for each server
    28. --inline-stdout inline standard output for each server
    29. -I, --send-input read from standard input and send as input to ssh
    30. -P, --print print output as we get it
    31. Example: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime

    pscp - 使用单个shell传递/复制文件到多台Linux服务器

    pscp工具允许你使用单个终端用单条命令传输/复制文件到多台远程Linux主机,这个工具是pssh(它提供了OpenSSH的并行版本)的一部分。

    如何使用Pscp传输/复制文件到多台Linux服务器

    当使用pscp时,你需要创建一个单独文件,它包含你需要连的Linux服务器的IP地址和SSH端口号的列表。

    复制文件到多个Linux服务器

    让我们创建一个名为:myscphosts.txt:的新文件,并且按如下添加Linux主机IP地址和SSH端口号的列表:

    1. [blctrl@station1 ~]$ cat myscphosts.txt
    2. 192.168.50.150:22
    3. 192.168.50.170:22
    4. 192.168.50.197:22
    5. 192.168.50.224:22
    6. 192.168.50.74:22

    一旦你添加了主机到这个文件,能够将文件从本地主机复制到多台远程主机的指定目录下:

    1、先在远程主机上用blctrl用户,在其家目录下创建一个newpath目录。

    2、将文件myscphost.txt从本地主机当前目录下复制到远程主机的/home/blctrl/newpath下。

    3、验证远程主机上newpath目录是否创建以及本地文件myscphosts.txt是否被复制到这个目录下。

    1. [blctrl@station1 ~]$ pssh -h myscphosts.txt -l blctrl -i "mkdir ~/newpath"
    2. [1] 12:51:31 [SUCCESS] 192.168.50.170:22
    3. [2] 12:51:31 [SUCCESS] 192.168.50.150:22
    4. [3] 12:51:31 [SUCCESS] 192.168.50.74:22
    5. [4] 12:51:31 [SUCCESS] 192.168.50.224:22
    6. [5] 12:51:31 [SUCCESS] 192.168.50.197:22
    7. [blctrl@station1 ~]$ pwd
    8. /home/blctrl
    9. [blctrl@station1 ~]$ pscp -h myscphosts.txt -l blctrl -Av myscphosts.txt /home/blctrl/newpath
    10. Warning: do not enter your password if anyone else has superuser
    11. privileges or access to your account.
    12. Password:
    13. [1] 12:53:02 [SUCCESS] 192.168.50.170:22
    14. [2] 12:53:02 [SUCCESS] 192.168.50.150:22
    15. [3] 12:53:02 [SUCCESS] 192.168.50.74:22
    16. [4] 12:53:02 [SUCCESS] 192.168.50.224:22
    17. [5] 12:53:02 [SUCCESS] 192.168.50.197:22
    18. [blctrl@station1 ~]$ pssh -h myscphosts.txt -l blctrl -i "ls ~/newpath"
    19. [1] 12:53:22 [SUCCESS] 192.168.50.170:22
    20. myscphosts.txt
    21. [2] 12:53:22 [SUCCESS] 192.168.50.150:22
    22. myscphosts.txt
    23. [3] 12:53:22 [SUCCESS] 192.168.50.74:22
    24. myscphosts.txt
    25. [4] 12:53:22 [SUCCESS] 192.168.50.224:22
    26. myscphosts.txt
    27. [5] 12:53:23 [SUCCESS] 192.168.50.197:22
    28. myscphosts.txt

    解释在以上命令中使用的选项:

    • -h 用来从指定文件和位置读取主机。
    • -l:用于在未定义特定用户的所有主机上读取默认用户名。
    • -A:告诉pscp请求密码并且发送给ssh
    • -v :用于以详细模式允许pscp

    复制目录到多个Linux服务器

    如果你想要复制整个目录使用-r选项,这将递归地复制整个目录。

    1、首先在在data目录下创建10个txt文件。

    2、将data目录从本地主机当前目录下复制到远程主机的/home/blctrl/newpath下。

    3、检查data目录是否已经复制到远程主机上/home/blctrl/newpath目录中。

    1. [blctrl@station1 ~]$ touch data/{1..10}.txt
    2. [blctrl@station1 ~]$ ls data/
    3. 10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
    4. [blctrl@station1 ~]$ pssh -h myscphosts.txt -l blctrl -i "ls -R ~/newpath"
    5. [1] 13:14:22 [SUCCESS] 192.168.50.170:22
    6. /home/blctrl/newpath:
    7. data
    8. myscphosts.txt
    9. /home/blctrl/newpath/data:
    10. 10.txt
    11. 1.txt
    12. 2.txt
    13. 3.txt
    14. 4.txt
    15. 5.txt
    16. 6.txt
    17. 7.txt
    18. 8.txt
    19. 9.txt
    20. [2] 13:14:22 [SUCCESS] 192.168.50.150:22
    21. /home/blctrl/newpath:
    22. data
    23. myscphosts.txt
    24. /home/blctrl/newpath/data:
    25. 10.txt
    26. 1.txt
    27. 2.txt
    28. 3.txt
    29. 4.txt
    30. 5.txt
    31. 6.txt
    32. 7.txt
    33. 8.txt
    34. 9.txt
    35. [3] 13:14:22 [SUCCESS] 192.168.50.74:22
    36. /home/blctrl/newpath:
    37. data
    38. myscphosts.txt
    39. /home/blctrl/newpath/data:
    40. 10.txt
    41. 1.txt
    42. 2.txt
    43. 3.txt
    44. 4.txt
    45. 5.txt
    46. 6.txt
    47. 7.txt
    48. 8.txt
    49. 9.txt
    50. [4] 13:14:22 [SUCCESS] 192.168.50.224:22
    51. /home/blctrl/newpath:
    52. data
    53. myscphosts.txt
    54. /home/blctrl/newpath/data:
    55. 10.txt
    56. 1.txt
    57. 2.txt
    58. 3.txt
    59. 4.txt
    60. 5.txt
    61. 6.txt
    62. 7.txt
    63. 8.txt
    64. 9.txt
    65. [5] 13:14:22 [SUCCESS] 192.168.50.197:22
    66. /home/blctrl/newpath:
    67. data
    68. myscphosts.txt
    69. /home/blctrl/newpath/data:
    70. 10.txt
    71. 1.txt
    72. 2.txt
    73. 3.txt
    74. 4.txt
    75. 5.txt
    76. 6.txt
    77. 7.txt
    78. 8.txt
    79. 9.txt

    概要

    并行SSH或PSSH是用于在系统管理员必须处理网络上很多服务器的环境中执行命令的一个好工具。它使得在网络上不同主机上远程执行命令变得简单。

  • 相关阅读:
    数据结构与算法-冒泡排序
    leetCode-hot100-数组专题之求和+数学定理+其他
    黑马程序员 计算机网络(笔记)
    02.Spring Boot入门案例
    核—幂零分解
    策略梯度(Policy Gradient)算法学习
    普冉PY32系列(八) GPIO模拟和硬件SPI方式驱动无线收发芯片XN297LBW
    Java开发面试常见问题总结
    UE4-UMG点击播放关卡序列(Level Sequence)
    厌倦了安装数据库?改用 Docker
  • 原文地址:https://blog.csdn.net/yuyuyuliang00/article/details/125529526