pssh命令是一个python编写可以在多台服务器上执行命令的工具,同时文件的并行复制,杀掉远程主机上的进程等。要使用pssh工具包,必须配置本地主机和被管理的远程主机之间ssh单向的免秘钥认证。
参数:
–version:查看版本
-h:主机文件列表,内容格式”[user@]host[:port]”
-H:主机字符串,内容格式”[user@]host[:port]”
-l:登录使用的用户名
-p:并发的线程数【可选】
-o:输出的文件目录【可选】
-e:错误输入文件【可选】
-t:TIMEOUT 超时时间设置,0无限制【可选】
-O:SSH的选项
-v:详细模式
-A:提示手动输入密码模式
-x:额外的命令行参数使用空白符号,引号,反斜线处理
-X:额外的命令行参数,单个参数模式,同-x
-i:每个服务器内部处理信息输出
-P:打印出服务器返回信息
操作系统:CentOS 7
实验平台:VMware Workstation 16 (不要求相同)
网络模式:NAT模式
两台主机IP分别为192.168.225.10和192.168.225.11
1.创建密钥
一直回车就行
[root@node10 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //密钥保存目录
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): //输入证书保护口令,也可以直接回车
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa. //私钥存放路径
Your public key has been saved in /root/.ssh/id_rsa.pub. //公钥存放路径
The key fingerprint is:
SHA256:7380AFoGtRmzqG55ksK96PZT1d4YBYil+2m0oQqt6IE root@node10
The key's randomart image is:
+---[RSA 2048]----+
| .==... |
| .o=* . |
| o++o . |
| .... + |
| . S.o. = |
| . . + o.= +o + |
|E . + O.o * . . |
| o.*.= o . |
| .oo+.+. .... |
+----[SHA256]-----+
2.将公钥添加至远程主机中
[root@node10 ~]# ssh-copy-id 192.168.225.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.225.11 (192.168.225.11)' can't be established.
ECDSA key fingerprint is SHA256:oppsZfD6Hqh1dleccvZFiBlU2INZC+isxNYGbWNqANM.
ECDSA key fingerprint is MD5:53:10:25:4e:ae:f3:d6:a8:7f:0e:3d:fd:3e:0c:56:6f.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.225.11's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.225.11'"
and check to make sure that only the key(s) you wanted were added.
注意:这里注意将公钥也给本台主机添加一份 ,否则使用PSSH命令无法操作本台主机
[root@node10 ~]# ssh-copy-id 192.168.225.10
........
root@192.168.225.10's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.225.10'"
and check to make sure that only the key(s) you wanted were added.
这种安装方式需要有epel源
[root@node10 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo //这里我安装了阿里云的epel源
[root@node10 ~]# yum install pssh -y
简单示例:
操作单台主机
1. 显示主机名
[root@node10 ~]# pssh -H "192.168.225.11" -i 'hostname'
[1] 13:02:41 [SUCCESS] 192.168.225.11 //这里显示[SUCCESS]即为成功
node11
操作多台主机
将主机IP写入文件中
显示主机名
[root@node10 ~]# vi host-list.txt
[root@node10 ~]# cat host-list.txt
192.168.225.10
192.168.225.11
[root@node10 ~]# pssh -h host-list.txt -i 'hostname'
[1] 13:06:38 [SUCCESS] 192.168.225.11
node11
[2] 13:06:39 [SUCCESS] 192.168.225.10
node10
2.将本地的文件复制到多台远程主机
[root@node10 ~]# echo "pssh.hello" >11.txt
[root@node10 ~]# pscp.pssh -h host-list.txt 11.txt /tmp/
[1] 13:39:48 [SUCCESS] 192.168.225.11
[2] 13:39:49 [SUCCESS] 192.168.225.10
[root@node10 ~]# cat /tmp/11.txt
pssh.hello
[root@node11 ~]# cat /tmp/11.txt
pssh.hello
[root@node11 ~]#
将远程主机文件复制到本地
将远端主机/etc/passwd文件复制到本地/test目录
[root@node10 ~]# pslurp -h host-list.txt -L /test /etc/passwd ./
[1] 13:47:39 [SUCCESS] 192.168.225.11
[2] 13:47:40 [SUCCESS] 192.168.225.10
[root@node10 ~]# cd /test
[root@node10 test]# ls
192.168.225.10 192.168.225.11
[root@node10 test]# cd 192.168.225.10
[root@node10 192.168.225.10]# ls
passwd
[root@node10 192.168.225.10]# cd
[root@node10 ~]#
批量结束远程进程
结束http服务
[root@node10 ~]# pnuke -h host-list.txt httpd