Linux 下SHELL脚本自动同步文件

  1 #!/bin/bash
  2 expect <<EOF
  3 set timeout 100
  4 spawn rsync -avz root@192.168.10.57:/var/www/html/manage_chanpin /var/www/html/bak/ 
  5 expect "password:"
  6 send "wintel
"
  7 spawn rsync -avz root@192.168.10.57:/var/www/html/ekt_chanpin /var/www/html/bak/ 
  8 expect "password:"
  9 send "wintel
"
 10 expect eof
 11 interact
 12 EOF
 13 tar -czvf /var/www/html/bak/manage_chanpin_`date +%Y%m%d`.tar.gz /var/www/html/bak/manage_chanpin
 14 tar -czvf /var/www/html/bak/ekt_chanpin_`date +%Y%m%d`.tar.gz /var/www/html/bak/ekt_chanpin

说明:

Expect是一个用来处理交互的命令。借助Expect,我们可以将交互过程写在一个脚本上,使之自动化完成。

Expect中最关键的四个命令是send,expect,spawn,interact。

send:用于向进程发送字符串
expect:从进程接收字符串
spawn:启动新的进程
interact:允许用户交互

1. send命令

send命令接收一个字符串参数,并将该参数发送到进程。

expect1.1> send "hello world
"
hello world

2. expect命令


(1)基础知识

expect命令和send命令正好相反,expect通常是用来等待一个进程的反馈。expect可以接收一个字符串参数,也可以接收正则表达式参数。和上文的send命令结合,现在我们可以看一个最简单的交互式的例子:

expect "hi
"
send "hello there!
"

这两行代码的意思是:从标准输入中等到hi和换行键后,向标准输出输出hello there。

tips: $expect_out(buffer)存储了所有对expect的输入,<$expect_out(0,string)>存储了匹配到expect参数的输入。

比如如下程序:

expect "hi
"
send "you typed <$expect_out(buffer)>"
send "but I only expected <$expect_out(0,string)>"

当在标准输入中输入

test
hi

是,运行结果如下

you typed: test
hi
I only expect: hi

(2)模式-动作

expect最常用的语法是来自tcl语言的模式-动作。这种语法极其灵活,下面我们就各种语法分别说明。

单一分支模式语法:

expect "hi" {send "You said hi"}

匹配到hi后,会输出"you said hi"

多分支模式语法:

expect "hi" { send "You said hi
" } 
"hello" { send "Hello yourself
" } 
"bye" { send "That was unexpected
" }

匹配到hi,hello,bye任意一个字符串时,执行相应的输出。等同于如下写法:

expect {
"hi" { send "You said hi
"}
"hello" { send "Hello yourself
"}
"bye" { send "That was unexpected
"}
}

3. spawn命令

上文的所有demo都是和标准输入输出进行交互,但是我们跟希望他可以和某一个进程进行交互。spawm命令就是用来启动新的进程的。spawn后的send和expect命令都是和spawn打开的进程进行交互的。结合上文的send和expect命令我们可以看一下更复杂的程序段了。

set timeout -1
spawn ftp ftp.test.com      //打开新的进程,该进程用户连接远程ftp服务器
expect "Name"             //进程返回Name时
send "user
"        //向进程输入anonymous
expect "Password:"        //进程返回Password:时
send "123456
"    //向进程输入don@libes.com
expect "ftp> "            //进程返回ftp>时
send "binary
"           //向进程输入binary
expect "ftp> "            //进程返回ftp>时
send "get test.tar.gz
"  //向进程输入get test.tar.gz

这段代码的作用是登录到ftp服务器ftp ftp.uu.net上,并以二进制的方式下载服务器上的文件test.tar.gz。程序中有详细的注释。


4.interact

到现在为止,我们已经可以结合spawn、expect、send自动化的完成很多任务了。但是,如何让人在适当的时候干预这个过程了。比如下载完ftp文件时,仍然可以停留在ftp命令行状态,以便手动的执行后续命令。interact可以达到这些目的。下面的demo在自动登录ftp后,允许用户交互。

spawn ftp ftp.test.com
expect "Name"
send "user
"
expect "Password:"
send "123456
"
原文地址:https://www.cnblogs.com/yuzhoushenqi/p/6950490.html