pssh简介
pssh(Parallel SSH)是一个用于在多台远程服务器上并行执行命令的工具,它基于SSH协议,可以在不实际登录到每台服务器的情况下,将命令分发到目标服务器上执行,这样可以大大提高工作效率,特别是在需要对大量服务器进行操作时,pssh支持多种操作系统,包括Linux、macOS和Windows。
安装pssh
1、对于Linux和macOS系统,可以使用以下命令安装pssh:
pip install pssh
2、对于Windows系统,可以从以下网址下载预编译的二进制文件:https://github.com/robbyrussell/pssh/releases
下载完成后,解压缩文件并将可执行文件添加到系统的PATH环境变量中。
使用pssh的基本语法
pssh的基本语法如下:
pssh [options] -l users -A hosts command_to_run
options
:可选参数,例如指定端口号、超时时间等。
-l users
:要登录的目标服务器上的用户列表,用空格分隔。
-A hosts
:要连接的目标服务器列表,用逗号分隔,如果不提供此参数,pssh会自动从~/.ssh/known_hosts
文件中读取目标服务器的公钥。
command_to_run
:要在目标服务器上执行的命令。
示例
假设我们有两台服务器,分别位于IP地址192.168.1.2和192.168.1.3上,用户名为user1和user2,我们需要在这两台服务器上同时执行ls
和pwd
命令,可以使用以下命令:
pssh -l user1,user2 -A 192.168.1.2,192.168.1.3 "ls; pwd"
常用选项
1、-l
:指定要登录的用户列表,如上例所示。
2、-A
:指定要连接的目标服务器列表,如上例所示。
3、-i
:指定私钥文件的路径,默认情况下,pssh会在当前目录下寻找名为id_rsa
(RSA密钥)或id_dsa
(DSA密钥)的私钥文件,如果需要使用其他类型的私钥,可以通过此选项指定。
4、-P
:指定SSH端口号,默认情况下,SSH端口号为22,如果需要使用其他端口号,可以通过此选项指定。
5、-C
:指定连接超时时间(以秒为单位),默认情况下,连接超时时间为30秒,如果需要使用其他超时时间,可以通过此选项指定。
6、-o
:指定SSH选项,可以使用此选项禁止密码认证,只允许密钥认证:
-o PubkeyAuthentication=yes -o PasswordAuthentication=no
相关问题与解答
Q1:如何查看已经连接的目标服务器?
答:可以使用以下命令查看已经连接的目标服务器:
pssh -l user | grep connected | awk '{print $1}' | xargs -I {} ssh {} exit || echo Connection failed for {}
Q2:如何在执行完命令后自动断开与目标服务器的连接?
答:可以使用以下命令实现自动断开连接:
pssh -l user | grep connected | awk '{print $1}' | xargs -I {} ssh {} exit || echo Connection failed for {} & sleep 10 && killall -9 sshd || echo Killing all ssh processes failed for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all ssh processes failed again for {} & sleep 5 && pkill -9 sshd || echo Killing all sh
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/190579.html