自动化运维
pssh
配置免秘钥登录(pssh需要免秘钥登录)
[root@localhost ~]# ssh-keygen (一直回车就行)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 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:/I/x1htSRAFKOEVeTUBMf3m/ON2rsckutz+3njSIhyI root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| +++*=+. |
| oo o.o. .|
| .o o.o|
| . . .o|
| S . .|
| . o.+ o|
| E .oo.B.=.|
| . .*+oBo=|
| ..*B*B+|
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.182.151
如果也想管理pssh本机的话需要执行以下命令
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub localhost
安装pssh(没有特殊要求建议yum安装)
[root@localhost ~]# yum -y install pssh
查看版本
[root@localhost ~]# pssh --version
2.3.1
写一个要控制哪些主机的文件
[root@localhost ~]# echo -e "192.168.182.150\n192.168.182.151" > host.txt
发送远程命令查看磁盘空间
[root@localhost ~]# pssh -i -h host.txt -l root df -hT
[1] 18:45:29 [SUCCESS] 192.168.182.151
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 50G 2.1G 48G 5% /
devtmpfs devtmpfs 478M 0 478M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 6.8M 482M 2% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 143M 872M 15% /boot
/dev/mapper/centos-home xfs 45G 33M 45G 1% /home
tmpfs tmpfs 98M 0 98M 0% /run/user/0
[2] 18:45:29 [SUCCESS] 192.168.182.150
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 475M 0 475M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 50G 4.2G 46G 9% /
/dev/mapper/centos-home xfs 45G 33M 45G 1% /home
/dev/sda1 xfs 1014M 174M 841M 18% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
给远程主机安装apache
[root@localhost ~]# pssh -i -h host.txt -l root yum -y install httpd
查看远程主机是否安装了apache
[root@localhost ~]# pssh -i -h host.txt -l root 'rpm -qa |grep httpd'
[1] 18:49:52 [SUCCESS] 192.168.182.151
httpd-tools-2.4.6-97.el7.centos.x86_64
httpd-2.4.6-97.el7.centos.x86_64
[2] 18:49:52 [SUCCESS] 192.168.182.150
httpd-2.4.6-97.el7.centos.x86_64
httpd-tools-2.4.6-97.el7.centos.x86_64
为所有主机写入每天凌晨mysql备份脚本,并查看
[root@localhost ~]# pssh -i -h host.txt -l root "echo '0 0 * * * mysqldump -uroot -p123 test |gzip > test`date +%Y-%m-%d.mysqldump-sql.gz`' >> /var/spool/cron/root&&crontab -l"
拷贝文件到远程主机
[root@localhost ~]# pscp.pssh -h host.txt host.txt /tmp
[1] 20:29:30 [SUCCESS] 192.168.182.151
[2] 20:29:30 [SUCCESS] 192.168.182.150
[root@localhost ~]# pssh -i -h host.txt -l root "ls /tmp/hos*"
[1] 20:30:58 [SUCCESS] 192.168.182.151
/tmp/host.txt
[2] 20:30:59 [SUCCESS] 192.168.182.150
/tmp/host.txt
拷贝远程主机文件到本地
[root@localhost ~]# pslurp -h host.txt -L $mkdir/ /etc/host.txt host.tar.gz
##这样执行后就会在本地出现host.txt里面指定的所有主机指定文件并且跟据IP地址分别放在相应的目录 (mkdir是具体存放在本地哪里)
示例: pssh -h hosts.txt -l irb2 -o /tmp/foo uptime