Automated operation and maintenance pssh use

Automated operation and maintenance
pssh

Configure keyless login (pssh requires keyless login)

[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 [email protected]
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

Install pssh (no special requirements suggest yum installation)

[root@localhost ~]# yum -y install pssh

View version

[root@localhost ~]# pssh --version
2.3.1

Write a file to control which hosts

[root@localhost ~]# echo -e "192.168.182.150\n192.168.182.151" > host.txt

Send remote commands to view disk space

[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

Install apache on the remote host

[root@localhost ~]# pssh -i -h host.txt -l root yum -y install httpd 

Check whether the remote host has apache installed

[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

Write every morning mysql backup script for all hosts and view

[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"

Copy files to remote host

[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

Copy remote host file to local

[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

Guess you like

Origin blog.csdn.net/zeorg/article/details/111697164