Automated operation and maintenance of small tools and transfer tool rsync pssh

First, a brief introduction

1, pssh stands for parallel-ssh, written in Python-based tool to perform concurrent batch commands on multiple servers. Including pssh, pscp, prsync, pnuke and pslurp. The project includes psshlib, can be used in custom applications. It is equivalent to ansible simplified version, execute faster than ansible, support for parallel file copy, remote command execution, kill the process on a remote host, the killer is a parallel file copy .

2, the relevant parameters
  -h -hosts host file list, content format "[the User @] Host [: Port]"
  -H -host single host, content format "[the User @] Host [: Port]"
  the -l -user the user name used to log
  the number of concurrent threads -p -par [optional]
  directory -o -outdir output [optional]
  -e -errdir erroneous input file [optional]
  -t -timeout tIMEOUT timeout, 0 [unrestricted] Alternatively
  -O -option SSH options
  -v -verbose verbose mode
  -A -askpass manual input mode password
  -x -extra-args additional command line parameters blank sign, quote, backslash processing
  -X -extra-arg additional command line parameters, a single model parameter, with the -X-
  -i -inline each server internal processing information output
  Oral output -inline-stdout each server
  -P, -print print out the information returned by the server

3, the main usage:
  1.pssh command execution commands on the local host or the remote script
  2.pscp command to copy a local file to a plurality of remote host
  3.pslurp commands from multiple remote file to the local machine copy
  4.pnuke commands in parallel remote host scrapping process
  5.prsync command uses the rsync protocol from the local to the remote host computer synchronization

Second, the environment

 1, [root @ localhost ~] # vim ip.txt (created to log ip text)

129.168.40.132 (pssh machine) 192.168.40.220    

192.168.40.155                192.168.40.211

2, avoid close Login Script

[root@localhost ~]# vim ssh_key.sh

#!/bin/bash
rpm -q expect &> /dev/null || yum install expect -y
ssh-keygen -p "" -f "/root/.ssh/id_rsa"
password=xxxxx  (四台机子密码一样)
while read ipaddr;do
expect <<EOF
set timeout 10
spawn ssh-copy-id $ipaddr
expect {
"yes/no"  { send "yes\";exp_continue }
"password" { send "$password\n" }
}
expect eof
EOF
done < ip.txt

3, the Internet can communicate with each other

Third, examples

1, for the first time not login ( not free to do close, you need to log in # ssh 192.168.40.155, enter a yes )

[root@localhost ~]# pssh -H "192.168.40.220 192.168.40.155" -A  -i  hostname
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:04:44 [SUCCESS] 192.168.40.155
php2
[2] 09:04:44 [SUCCESS] 192.168.40.220
zjz

2, avoid close login, batch create user

[root@localhost ~]# pssh  -h   ip.txt   -i  'useradd pssher'
[1] 09:23:06 [SUCCESS] 192.168.40.155
[2] 09:23:07 [SUCCESS] 192.168.40.211
[3] 09:23:07 [SUCCESS] 192.168.40.220
[4] 09:23:07 [SUCCESS] 192.168.40.132

[Root @ localhost ~] # pssh -h ip.txt -i 'getent passwd pssher' (create user verification is successful)

3, specify the output folder to store information (no need to create this folder in advance)

[root@localhost ~]# pssh  -h  ip.txt  -o  /data/    'cat /etc/fstab'
[1] 09:40:56 [SUCCESS] 192.168.40.132
[2] 09:40:56 [SUCCESS] 192.168.40.220
[3] 09:40:56 [SUCCESS] 192.168.40.155
[4] 09:40:56 [SUCCESS] 192.168.40.211
[root@localhost ~]# cd /data
[root@localhost data]# ls
192.168.40.132  192.168.40.155  192.168.40.211  192.168.40.220

4, turn off selinux

[the root @ localhost ~] # the ip.txt the pssh -H 'Sed -i "S / the SELINUX = ^. * / the SELINUX = Disabled /" / etc / SELinux / config'
[. 1] 10:00:18 [SUCCESS] 192.168 .40.220
[2] 10:00:18 [SUCCESS] 192.168.40.155
[. 3] 10:00:18 [SUCCESS] 192.168.40.211
[. 4] 10:00:18 [SUCCESS] 192.168.40.132
[the root @ localhost ~] # cat / etc / selinux / config ( see the results, do not getenforce, slow refresh)

5, pscp.pssh remote push

[root@localhost ~]# pssh -h ip.txt  'mkdir /data'
[1] 10:13:39 [SUCCESS] 192.168.40.220
[2] 10:13:39 [SUCCESS] 192.168.40.155
[3] 10:13:39 [SUCCESS] 192.168.40.211
[4] 10:13:39 [SUCCESS] 192.168.40.132
[root@localhost ~]# pscp.pssh -h ip.txt /root/zj.sh  /data/
[1] 10:13:52 [SUCCESS] 192.168.40.220
[2] 10:13:52 [SUCCESS] 192.168.40.155
[3] 10:13:52 [SUCCESS] 192.168.40.211
[4] 10:13:52 [SUCCESS] 192.168.40.132
[root@localhost ~]# pssh -h ip.txt  'bash /data/zj.sh'
[1] 10:14:25 [SUCCESS] 192.168.40.220
[2] 10:14:25 [SUCCESS] 192.168.40.155
[3] 10:14:25 [SUCCESS] 192.168.40.211
[4] 10:14:25 [SUCCESS] 192.168.40.132
[root@localhost ~]# pssh -h ip.txt -i 'bash /data/zj.sh'
[1] 10:14:31 [SUCCESS] 192.168.40.220
zjz
[2] 10:14:31 [SUCCESS] 192.168.40.211
localhost.localdomain
[3] 10:14:31 [SUCCESS] 192.168.40.155
php2
[4] 10:14:31 [SUCCESS] 192.168.40.132
localhost.localdomain

. 6, pslurp remote download (pull)

[root @ localhost ~] # pslurp -h ip.txt -L / a / var / log / messages messages (-L specified folder, and finally to keep custom name, such as messages)
[. 1] 10:19:25 [ SUCCESS] 192.168.40.155
[2] 10:19:25 [SUCCESS] 192.168.40.211
[. 3] 10:19:25 [SUCCESS] 192.168.40.220
[. 4] 10:19:25 [SUCCESS] 192.168.40.132
[the root @ ~ localhost] # LS / A
192.168.40.132 192.168.40.155 192.168.40.211 192.168.40.220
[the root @ localhost ~] Tree # / A /
/ A /
├── 192.168.40.132
│ └── messages
├── 192.168.40.155
└── messages │
├── 192.168.40.211
│ └── messages
└── 192.168.40.220
    └── messages
[the root @ localhost ~] # pslurp the ip.txt -H -L / -R & lt AA / var / log m (-r recursive download)

7, pnuke parallel process in the remote host killing

[root@localhost ~]# pnuke   -h   ip.txt   httpd
[1] 11:18:04 [SUCCESS] 192.168.40.211
[2] 11:18:04 [SUCCESS] 192.168.40.155
[3] 11:18:04 [SUCCESS] 192.168.40.220
[4] 11:18:05 [SUCCESS] 192.168.40.132

. 8, prsync    -R & lt recursion / home spread hosts / tmp / pssh / directory

[root@localhost ~]# prsync  -h ip.txt -r /home /tmp/pssh/
[1] 11:24:26 [SUCCESS] 192.168.40.132
[2] 11:24:26 [SUCCESS] 192.168.40.220
[3] 11:24:26 [SUCCESS] 192.168.40.155
[4] 11:24:26 [SUCCESS] 192.168.40.211
四、rsync工具

-v: Detailed information display rsync process.

-P: show progress information file transfer.

-n : analog copy process.

-a : archive mode, represents a recursive transfer and maintain file attributes. Equivalent to " -rtopgDl ."

-t: keeping mtime property. Recommended at all times plus "-t" , otherwise the target file mtime will be set to the system time, resulting in the next update: check out the different mtime resulting incremental transfer invalid.

-o: holding property owner (owner).

-g --group: holding the group attribute (genus group).

-p --perms: keep the property perms (permissions, excluding special permission).

-D: it is a combination of "--device --specials" option, which also copy files and device special files.

-l --links: If the file is a soft link file, object soft link itself rather than the soft link points to the copy.

-z: compress improve the efficiency of the transmission.

-R: using a relative path.

-u: only when a new copy mtime source mtime file that already exists than the target. Note that this option is the receiving end judgment does not affect delete behavior.

-d: to not recursively copy directory itself.

-e: Specifies the remote shell program to be used, the default is ssh.

-W: rsync incremental transfer will not be used, but the total amount of transfers.

rsync is more efficient than doing long-distance transmission scp, rsync based on incremental transmission, faster and more efficient.

[root @ zjz ~] # rsync -av / root / zjz / * 192.168.40.211:/root/cui/
[email protected]'s password:
sending with the incremental File List
f1 (/ root / zjz / have f1, f2, f3, three files, f1 is transmitted is changed because only f1)
sent Received 189 185 133 907 bytes bytes 15027.53 bytes / sec
Total size 2,826,960,896 Speedup iS iS 8749.71

 

Guess you like

Origin www.cnblogs.com/zjz20/p/11619566.html