2018-06-07 Linux Learning

Source: Internet
Author: User

20.31 Expect script synchronization file

Automatically synchronize files

#!/usr/bin/expect
Set passwd "123456"
Spawn Rsync-av [Email protected]:/tmp/12.txt/tmp/
Expect {
"Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
}
Expect EOF

Operation Process

[[email protected] sbin]# vim 4.expect写入上面 自动同步文件 内容[[email protected] sbin]# chmod a+x 4.expect [[email protected] sbin]# ./4.expect spawn rsync -av [email protected]:/tmp/12.txt /tmp/[email protected]‘s password: receiving incremental file list12.txtsent 30 bytes  received 84 bytes  20.73 bytes/sectotal size is 5  speedup is 0.04

20.32 Expect script specifies host and files to synchronize

Specify host and files to synchronize

#!/usr/bin/expect
Set passwd "123456"
Set host [lindex $ARGV 0]
Set file [lindex $argv 1]
Spawn Rsync-av $file [email protected] $host: $file
Expect {
"Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
}
Expect EOF

Operation Process

[[email protected] sbin]# vim 5.expect写入上面的内容 指定host和要同步的文件[[email protected] sbin]# chmod a+x 5.expect [[email protected] sbin]# ./5.expect 192.168.106.165 /tmp/12.txt spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt[email protected]‘s password: sending incremental file listsent 31 bytes  received 12 bytes  7.82 bytes/sectotal size is 5  speedup is 0.12[[email protected] sbin]# ./5.expect 192.168.106.165 "/tmp/12.txt" spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt[email protected]‘s password: sending incremental file listsent 31 bytes  received 12 bytes  7.82 bytes/sectotal size is 5  speedup is 0.12

20.33 Building a file distribution system

Requirements background
For large companies, there must be a site or configuration file updates from time to time, and the use of the machine is certainly a lot of units, less than a few, more than dozens of or even hundreds of sets. Therefore, automatic synchronization of files is critical.

Implementation ideas
Start with a template machine, prepare the files to be distributed, and then distribute the files that need to be synchronized to the target machine in batches using the expect script.
Core command Rsync-av--files-from=list.txt?? /[email protected]:/

Implementation of file distribution system

Rsync.expect Content

#!/usr/bin/expect
Set passwd "123456"
Set host [lindex $ARGV 0]
Set file [lindex $argv 1]
Spawn Rsync-av--files-from= $file/[email protected] $host:/
Expect {
"Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
}
Expect EOF

Ip.list Content
192.168.133.132
192.168.133.133
......

rsync.sh Content

#!/bin/bash
For IP incat /tmp/ip.list
Do
./rsync.expect $ip/tmp/file.list
Done

Operation Process

[Email protected] sbin]# vim Rsync.expect
#!/usr/bin/expect
Set passwd "123456"
Set host [lindex $ARGV 0]
Set file [lindex $argv 1]
Spawn Rsync-avr--files-from= $file/[email protected] $host:/
Expect {
"Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
}
Expect EOF

[Email protected] sbin]# chmod a+x rsync.expect

[Email protected] sbin]# vim/tmp/file.list
/tmp/12.txt
/root/shell/1.sh
/root/111/222/123.txt

[Email protected] sbin]# vim/tmp/ip.list
192.168.106.165

[Email protected] sbin]# vim rsync.sh
#!/bin/bash
For IP incat /tmp/ip.list
Do
./rsync.expect $ip/tmp/file.list
Done

[[email protected] sbin]# sh -x rsync.sh ++ cat /tmp/ip.list+ for ip in ‘`cat /tmp/ip.list`‘+ ./rsync.expect 192.168.106.165 /tmp/file.listspawn rsync -avR --files-from=/tmp/file.list / [email protected]:/[email protected]‘s password: building file list ... rsync: link_stat "/root/shell/1.sh" failed: No such file or directory (2)doneroot/root/111/root/111/222/root/111/222/123.txttmp/tmp/12.txtsent 221 bytes  received 62 bytes  43.54 bytes/sectotal size is 5  speedup is 0.02rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]

20.34 Bulk Remote execution commands

Exe.expect Content

#!/usr/bin/expect
Set host [lindex $ARGV 0]
Set passwd "123456"
Set cm [lindex $argv 1]
Spawn ssh [email protected] $host
Expect {
"Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
}
Expect "]"
Send "$cm \ r"
Expect "]
"
Send "exit\r"

exe.sh Content

#!/bin/bash
For IP incat ip.list
Do
Echo $ip
./exe.expect $ip "W;free-m;ls/tmp"
Done

Operation Process

[Email protected] sbin]# vim Exe.expect
Write the above exe.expect content

[Email protected] sbin]# chmod a+x exe.expect

[Email protected] sbin]# vim exe.sh
#!/bin/bash
For IP incat /tmp/ip.list
Do
Echo $ip
./exe.expect $ip "W;free-m;ls/tmp"
Done

[[email protected] sbin]# sh exe.sh 192.168.106.165spawn ssh [email protected][email protected]‘s password: Last login: Tue Apr 24 17:47:27 2018 from 192.168.106.160[[email protected] ~]# w;free -m;ls /tmp 17:48:41 up  1:51,  2 users,  load average: 0.00, 0.01, 0.05USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHATroot     pts/0    192.168.106.1    15:57   15:13   0.04s  0.04s -bashroot     pts/1    192.168.106.160  17:48    1.00s  0.02s  0.01s w              total        used        free      shared  buff/cache   availableMem:           1822         159        1460           8         202        1473Swap:          4095           0        409512.txt  systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vgauthd.service-QH8NC0d6z     systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vmtoolsd.service-fDRGcretc

2018-06-07 Linux Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.