安装expect

[root@lynn-04 ~]# yum install -y expect

用脚本登陆远程机器 编写脚本1.expect 内容如下:

[root@lynn-04 expect]# vim 1.expect
#!/usr/bin/expect
set host "192.168.130.128"     #定义变量
set passwd "6811327"             #定义变量
spawn ssh root@$host
expect {
"yes/no" {send "yes\r"; exp_continue }        #当提示语句出现yes/no 输入yes然后继续
"password:" {send "$passwd\r" }                #当提示语句出现password 输入密码
}
interact

执行结果

[root@lynn-04 expect]# chmod a+x 1.expect ; ./1.expect
spawn ssh root@192.168.130.128
root@192.168.130.128's password: 
Last login: Wed Apr 25 07:48:41 2018 from 192.168.130.1
[root@lynn-06 ~]# 

用脚本登陆远程机器执行命令后退出 编写脚本2.expect 内容如下:

[root@lynn-04 expect]# vim 2.expect

#!/usr/bin/expect
set user "root"
set passwd "6811327"
spawn ssh $user@192.168.130.128

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "ls -l /tmp/12.txt\r"
expect "]*"
send "cat /tmp/12.txt\r"
expect "]*"
send "exit\r"

执行结果

[root@lynn-04 expect]# chmod a+x 2.expect ; ./2.expect
spawn ssh root@192.168.130.128
root@192.168.130.128's password: 
Last login: Wed Apr 25 08:07:55 2018 from 192.168.130.116
[root@lynn-06 ~]# touch /tmp/12.txt
[root@lynn-06 ~]# echo 1212 > /tmp/12.txt
[root@lynn-06 ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 4月  25 08:32 /tmp/12.txt
[root@lynn-06 ~]# cat /tmp/12.txt
1212
[root@lynn-06 ~]# [root@lynn-04 expect]# 

脚本传递参数 编写脚本3.expect 内容如下:

[root@lynn-04 expect]# vim 3.expect

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "6811327"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

执行结果

[root@lynn-04 expect]# chmod a+x 3.expect ; ./3.expect root 192.168.130.128 ls
spawn ssh root@192.168.130.128
root@192.168.130.128's password: 
Last login: Wed Apr 25 08:32:02 2018 from 192.168.130.116
[root@lynn-06 ~]# ls
anaconda-ks.cfg  com  zabbix-release-3.2-1.el7.noarch.rpm
[root@lynn-04 expect]# ./3.expect root 192.168.130.128 "ls;w;ps aux|grep sshd"
spawn ssh root@192.168.130.128
root@192.168.130.128's password: 
Last login: Wed Apr 25 08:50:42 2018 from 192.168.130.116
[root@lynn-06 ~]# ls;w;ps aux|grep sshd
anaconda-ks.cfg  com  zabbix-release-3.2-1.el7.noarch.rpm
 08:52:15 up  1:04,  2 users,  load average: 0.00, 0.01, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.130.1    07:48   15.00s  0.01s  0.01s -bash
root     pts/1    192.168.130.116  08:52    0.00s  0.00s  0.00s w
root        826  0.0  0.4 105996  4128 ?        Ss   07:48   0:00 /usr/sbin/sshd -D
root        947  0.0  0.5 148316  5376 ?        Ss   07:48   0:00 sshd: root@pts/0
root       1098  0.0  0.5 148312  5504 ?        Ss   08:52   0:00 sshd: root@pts/1
root       1118  0.0  0.0 112680   984 pts/1    S+   08:52   0:00 grep --color=auto sshd
[root@lynn-06 ~]# [root@lynn-04 expect]# 

设置超时 在执行的命令下面加上一行 set timeout -1 #永不超时 set timeout 1 #1秒超时 set timeout 5 #5秒超时 例如: expect "]*" send "vmstat 1" #这个命令会一直执行 set timeout -1

expect "]*" send "vmstat 1" #这个命令会5秒后自动停止 set timeout 5

自动同步文件 编写脚本 内容如下:

[root@lynn-04 expect]# vim 4.expect

#!/usr/bin/expect
set passwd "6811327"
spawn rsync -av root@192.168.130.128:/tmp/12.txt /tmp/

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}

expect eof

执行结果

[root@lynn-04 expect]# chmod a+x 4.expect ; ./4.expect
spawn rsync -av root@192.168.130.128:/tmp/12.txt /tmp/
root@192.168.130.128's password: 
receiving incremental file list
12.txt

sent 30 bytes  received 84 bytes  228.00 bytes/sec
total size is 5  speedup is 0.04
[root@lynn-04 expect]# cat /tmp/12.txt
1212

指定host和要同步的文件 编写脚本5.expect 内容如下:

[root@lynn-04 expect]# vim 5.expect

#!/usr/bin/expect

set passwd "6811327"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:$file

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}

expect eof

执行结果

[root@lynn-04 expect]# chmod a+x 5.expect ; ./5.expect 192.168.130.128 "/tmp/12.txt"
spawn rsync -av /tmp/12.txt root@192.168.130.128:/tmp/12.txt
root@192.168.130.128's password: 
sending incremental file list

sent 31 bytes  received 12 bytes  86.00 bytes/sec
total size is 5  speedup is 0.12
[root@lynn-04 expect]# 

构建文件分发系统 创建并编辑rsync.expect 内容如下:

[root@lynn-04 expect]# vim rsync.expect

#!/usr/bin/expect
set passwd "6811327"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

创建并编辑file.list 内容如下:

[root@lynn-04 expect]# vim /tmp/file.list

/tmp/12.txt
/root/shell/01.sh

创建并编辑/tmp/ip.list 内容如下:

[root@lynn-04 expect]# vim /tmp/ip.list

192.168.130.128
127.0.0.1

创建并编辑rsync.sh 内容如下:

[root@lynn-04 expect]# vim rsync.sh

#!/bin/bash
for ip in `cat /tmp/ip.list`
do
   ./rsnyc.expect $ip /tmp/file.list
done

执行结果

root@lynn-04 expect]# sh rsync.sh
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.130.128:/
root@192.168.130.128's password: 
building file list ... done
root/
root/shell/
root/shell/01.sh
tmp/

sent 190 bytes  received 40 bytes  460.00 bytes/sec
total size is 34  speedup is 0.15
spawn rsync -avR --files-from=/tmp/file.list / root@127.0.0.1:/
root@127.0.0.1's password: 
building file list ... done

sent 109 bytes  received 12 bytes  242.00 bytes/sec
total size is 34  speedup is 0.28

远程批量执行命令 创建并编辑exe.expect 内容如下:

[root@lynn-04 expect]# vim exe.expect

#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "6811327"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

创建并编辑exe.sh 内容如下:

[root@lynn-04 expect]# vim exe.sh

#!/bin/bash
for ip in `cat /tmp/ip.list`
do
   ./exe.expect $ip "hostname"
done

执行结果

[root@lynn-04 expect]# sh exe.sh
spawn ssh root@192.168.130.128
root@192.168.130.128's password: 
Last login: Fri Apr 27 09:42:44 2018 from 192.168.130.116
[root@lynn-06 ~]# hostname
lynn-06
[root@lynn-06 ~]# spawn ssh root@127.0.0.1
root@127.0.0.1's password: 
Last login: Fri Apr 27 09:42:44 2018 from 127.0.0.1
[root@lynn-04 ~]# hostname
lynn-04
[root@lynn-04 ~]# spawn ssh root@192.168.130.118
root@192.168.130.118's password: 
Last login: Fri Apr 27 09:42:44 2018 from 192.168.130.116
[root@lynn-05 ~]# hostname
lynn-05
[root@lynn-05 ~]# [root@lynn-04 expect]#