OpenSSH毫无疑问是可用于Linux的使用最广泛和强大工具之一,它允许你通过一个shell安全地连接到远程Linux系统,并且允许你与远程系统之间来回传输文件。
但OpenSSH的最大缺点是你不同一次在多台主机上执行相同命令并且没有开发OpenSSH指定这样的任务。这就是并行SSH或者PSSH工具派上用处的地方,它是一个基于python的应用程序,其允许你同时在多台主机上并行地执行命令。
PSSH工具包含OpenSSH的并行版本和相关工具,诸如:
这些工具对处理网络上大量节点集合的系统管理员有帮助。
我们将看到在基于Federa发行(诸如AlmaLinux/Rocky LInux/Oracle Linux)上使用pip命令安装最新版本PSSH程序。
在AlmaLinux/Rocky LInux发行上,为了安装PSSH程序,你需要首先在你的系统上安装pip包:
[blctrl@areadetector data]$ sudo dnf install python3-pip
Last metadata expiration check: 3:00:19 ago on Wed 29 Jun 2022 07:54:19 PM CST.
Package python3-pip-9.0.3-22.el8.rocky.0.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
当你安装了pip工具,你能够在pip命令帮助下安装pssh包,如下:
[blctrl@areadetector data]$ pip install pssh
Defaulting to user installation because normal site-packages is not writeable
Collecting pssh
Downloading pssh-2.3.1.tar.gz (23 kB)
Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for pssh, since package 'wheel' is not installed.
Installing collected packages: pssh
Running setup.py install for pssh ... done
Successfully installed pssh-2.3.1
[root@station1 blctrl]# yum install python-pip
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
...
Installing:
python2-pip noarch 8.1.2-14.el7 epel 1.7 M
...
Installed:
python2-pip.noarch 0:8.1.2-14.el7
Complete!
[root@station1 blctrl]# pip install pssh
Collecting pssh
...
Installing collected packages: pssh
Running setup.py install for pssh ... done
Successfully installed pssh-2.3.1
You are using pip version 8.1.2, however version 22.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
当使用pssh时,你需要创建一个host文件,在这个文件中写入你需要使用pssh连接远程系统每台主机的IP地址和端口号。
在这个host文件中的行按照如下格式并且也可以包括空行和注释。
192.168.50.170:22 # AlmaLinux 8.6
192.168.50.197:22 # Oracle Linux 8.6
192.168.50.224:22 # CentOS 7.9
192.168.50.74:22 # CentOS 7.9 执行pssh命令的主机
192.168.50.150:22 # Rocky Linux 8.6
使用pssh在多台服务器上执行单个命令。
你能够通过一条pssh命令在一个网络中不同或者多台linux主机上执行任意单挑命令。如下所述,有与pssh一起使用的很多选项:
让我们看一下pssh命令的一些示例和用法:
1、在多台Linux主机上通过root用户执行echo "Hello World"(先做免密登录),运行以下命令。
重要:记住在host文件中必须记住所有主机
注意:以下pssh-hosts是一个文件,其中内容是你想要执行命令的远程Linux 服务器IP地址和SSH端口号的列表。
[blctrl@station1 ~]$ cat pssh-hosts
192.168.50.150:22
192.168.50.197:22
192.168.50.224:22
192.168.50.74:22
192.168.50.170:22
[blctrl@station1 ~]$ pssh -h pssh-hosts -l root echo "Hello World"
[1] 10:31:51 [SUCCESS] 192.168.50.74:22
[2] 10:31:51 [SUCCESS] 192.168.50.224:22
[3] 10:31:51 [SUCCESS] 192.168.50.150:22
[4] 10:31:51 [SUCCESS] 192.168.50.197:22
2、查找网络上多台Linux主机的磁盘空间,你可以运行以下命令:
[blctrl@station1 ~]$ pssh -h pssh-hosts -l root -i "df -hT"
[1] 10:35:13 [SUCCESS] 192.168.50.74:22
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 7.7G 0 7.7G 0% /dev
tmpfs tmpfs 7.8G 20M 7.7G 1% /dev/shm
tmpfs tmpfs 7.8G 11M 7.7G 1% /run
tmpfs tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 250G 8.3G 242G 4% /
/dev/sda2 xfs 1014M 173M 842M 17% /boot
/dev/mapper/centos-home xfs 207G 125M 207G 1% /home
/dev/sda1 vfat 200M 12M 189M 6% /boot/efi
tmpfs tmpfs 1.6G 52K 1.6G 1% /run/user/1000
tmpfs tmpfs 1.6G 8.0K 1.6G 1% /run/user/1001
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0
[2] 10:35:13 [SUCCESS] 192.168.50.224:22
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 9.9M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 36G 4.7G 31G 14% /
/dev/vda1 xfs 1014M 185M 830M 19% /boot
tmpfs tmpfs 379M 4.0K 379M 1% /run/user/42
tmpfs tmpfs 379M 60K 379M 1% /run/user/1000
tmpfs tmpfs 379M 0 379M 0% /run/user/0
[3] 10:35:13 [SUCCESS] 192.168.50.150:22
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 63G 0 63G 0% /dev
tmpfs tmpfs 63G 0 63G 0% /dev/shm
tmpfs tmpfs 63G 19M 63G 1% /run
tmpfs tmpfs 63G 0 63G 0% /sys/fs/cgroup
/dev/mapper/rl-root xfs 330G 46G 285G 14% /
/dev/mapper/rl-home xfs 595M 132M 463M 23% /home
/dev/sda2 xfs 1014M 186M 829M 19% /boot
/dev/sda4 xfs 596G 51G 545G 9% /home/blctrl
/dev/sda1 vfat 599M 5.7M 594M 1% /boot/efi
tmpfs tmpfs 13G 100K 13G 1% /run/user/1000
tmpfs tmpfs 13G 0 13G 0% /run/user/0
[4] 10:35:13 [SUCCESS] 192.168.50.197:22
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 9.4M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/almalinux-root xfs 36G 6.1G 29G 18% /
/dev/vda1 xfs 1014M 255M 760M 26% /boot
tmpfs tmpfs 374M 48K 374M 1% /run/user/1001
tmpfs tmpfs 374M 0 374M 0% /run/user/0
3、如果你想要一次知道多台Linux服务器的uptime,则你运行以下命令。
[blctrl@station1 ~]$ pssh -h pssh-hosts -l root -i "uptime"
[1] 10:37:37 [SUCCESS] 192.168.50.74:22
10:37:37 up 2 days, 18:43, 5 users, load average: 0.00, 0.01, 0.05
[2] 10:37:37 [SUCCESS] 192.168.50.224:22
10:37:37 up 13:24, 6 users, load average: 0.00, 0.01, 0.05
[3] 10:37:37 [SUCCESS] 192.168.50.197:22
10:37:37 up 1 day, 18:55, 2 users, load average: 0.00, 0.00, 0.00
[4] 10:37:37 [SUCCESS] 192.168.50.150:22
10:37:37 up 5 days, 20:57, 3 users, load average: 2.24, 2.28, 2.11
4、如果你想要知道多台Linux服务器的/home/blctrl目录下的文件,则运行命令:
[blctrl@station1 ~]$ pssh -h pssh-hosts -l root -i "ls /home/blctrl"
[1] 10:55:48 [SUCCESS] 192.168.50.74:22
1
data
perl5
pssh-hosts
[2] 10:55:48 [SUCCESS] 192.168.50.224:22
data
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
[3] 10:55:48 [SUCCESS] 192.168.50.150:22
data
Desktop
diffraction
Documents
Downloads
Music
Pictures
Public
Templates
Videos
[4] 10:55:48 [SUCCESS] 192.168.50.197:22
data
[5] 10:55:49 [SUCCESS] 192.168.50.170:22
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
你能够查看pssh命令的手册页获取很多其他选项来找到使用pssh的更多方式。
[blctrl@station1 ~]$ 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[:port]")
-H HOST_STRING, --host=HOST_STRING
additional host entries ("[user@]host[:port]")
-l USER, --user=USER username (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 directory 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 turn on warning and diagnostic messages (OPTIONAL)
-A, --askpass Ask for a password (OPTIONAL)
-x ARGS, --extra-args=ARGS
Extra command-line arguments, with processing for
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 standard 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
pscp工具允许你使用单个终端用单条命令传输/复制文件到多台远程Linux主机,这个工具是pssh(它提供了OpenSSH的并行版本)的一部分。
当使用pscp时,你需要创建一个单独文件,它包含你需要连的Linux服务器的IP地址和SSH端口号的列表。
复制文件到多个Linux服务器
让我们创建一个名为:myscphosts.txt:的新文件,并且按如下添加Linux主机IP地址和SSH端口号的列表:
[blctrl@station1 ~]$ cat myscphosts.txt
192.168.50.150:22
192.168.50.170:22
192.168.50.197:22
192.168.50.224:22
192.168.50.74:22
一旦你添加了主机到这个文件,能够将文件从本地主机复制到多台远程主机的指定目录下:
1、先在远程主机上用blctrl用户,在其家目录下创建一个newpath目录。
2、将文件myscphost.txt从本地主机当前目录下复制到远程主机的/home/blctrl/newpath下。
3、验证远程主机上newpath目录是否创建以及本地文件myscphosts.txt是否被复制到这个目录下。
[blctrl@station1 ~]$ pssh -h myscphosts.txt -l blctrl -i "mkdir ~/newpath"
[1] 12:51:31 [SUCCESS] 192.168.50.170:22
[2] 12:51:31 [SUCCESS] 192.168.50.150:22
[3] 12:51:31 [SUCCESS] 192.168.50.74:22
[4] 12:51:31 [SUCCESS] 192.168.50.224:22
[5] 12:51:31 [SUCCESS] 192.168.50.197:22
[blctrl@station1 ~]$ pwd
/home/blctrl
[blctrl@station1 ~]$ pscp -h myscphosts.txt -l blctrl -Av myscphosts.txt /home/blctrl/newpath
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 12:53:02 [SUCCESS] 192.168.50.170:22
[2] 12:53:02 [SUCCESS] 192.168.50.150:22
[3] 12:53:02 [SUCCESS] 192.168.50.74:22
[4] 12:53:02 [SUCCESS] 192.168.50.224:22
[5] 12:53:02 [SUCCESS] 192.168.50.197:22
[blctrl@station1 ~]$ pssh -h myscphosts.txt -l blctrl -i "ls ~/newpath"
[1] 12:53:22 [SUCCESS] 192.168.50.170:22
myscphosts.txt
[2] 12:53:22 [SUCCESS] 192.168.50.150:22
myscphosts.txt
[3] 12:53:22 [SUCCESS] 192.168.50.74:22
myscphosts.txt
[4] 12:53:22 [SUCCESS] 192.168.50.224:22
myscphosts.txt
[5] 12:53:23 [SUCCESS] 192.168.50.197:22
myscphosts.txt
解释在以上命令中使用的选项:
复制目录到多个Linux服务器
如果你想要复制整个目录使用-r选项,这将递归地复制整个目录。
1、首先在在data目录下创建10个txt文件。
2、将data目录从本地主机当前目录下复制到远程主机的/home/blctrl/newpath下。
3、检查data目录是否已经复制到远程主机上/home/blctrl/newpath目录中。
[blctrl@station1 ~]$ touch data/{1..10}.txt
[blctrl@station1 ~]$ ls data/
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
[blctrl@station1 ~]$ pssh -h myscphosts.txt -l blctrl -i "ls -R ~/newpath"
[1] 13:14:22 [SUCCESS] 192.168.50.170:22
/home/blctrl/newpath:
data
myscphosts.txt
/home/blctrl/newpath/data:
10.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
[2] 13:14:22 [SUCCESS] 192.168.50.150:22
/home/blctrl/newpath:
data
myscphosts.txt
/home/blctrl/newpath/data:
10.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
[3] 13:14:22 [SUCCESS] 192.168.50.74:22
/home/blctrl/newpath:
data
myscphosts.txt
/home/blctrl/newpath/data:
10.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
[4] 13:14:22 [SUCCESS] 192.168.50.224:22
/home/blctrl/newpath:
data
myscphosts.txt
/home/blctrl/newpath/data:
10.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
[5] 13:14:22 [SUCCESS] 192.168.50.197:22
/home/blctrl/newpath:
data
myscphosts.txt
/home/blctrl/newpath/data:
10.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
并行SSH或PSSH是用于在系统管理员必须处理网络上很多服务器的环境中执行命令的一个好工具。它使得在网络上不同主机上远程执行命令变得简单。