expect写脚本

~/.ssh/know_hosts文件 [远程ip:公钥]

当我们使用公钥去ssh连接一台远程主机时,系统默认会把访问过的主机的公钥都记录在 ~/.ssh/know_hosts

cat ~/.ssh/known_hosts
192.168.221.20 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHzi7l7PDXCUwFW6Xy6Ipg7FwTwHT9oiEfpHSqTTm1pBc0abAGgBjh8KwqkhmP+Au4D4+1k8XiZGvo0TYhSTi+c=

expect远程登录另外一台机器

yum install expect -y
vim /usr/local/sbin/expect/expect-login.sh
#! /usr/bin/expect    //注意与“#!/bin/bash”区分开来
set host "192.168.221.20"   //设置2个变量host,passwd,变量的值不能用单引号引起来
set passwd "root"
spawn ssh root@$host      
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact   //停留在远程机器上不退出来  expect eof  停留在远程机器上一会儿再退出

chmod +x /usr/local/sbin/expect/expect-login.sh
[root@localhost expect]# ./expect-login.sh   //特别注意不能用bash去执行
spawn ssh root@192.168.221.20
root@192.168.221.20's password: 
Last login: Sun Mar 18 10:33:33 2018 from 192.168.221.10
[root@apenglinux-002 ~]#

expect远程登录另外一台机器并执行命令

vim expect-login-exec.sh 
#! /usr/bin/expect
set host "192.168.221.20"
set passwd "root"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/expect.txt\r"
expect "]*"
send "echo expect > /tmp/expect.txt\r"
expect "]*"
send "exit\r"

[root@localhost expect]# chmod +x expect-login-exec.sh 
[root@localhost expect]# ./expect-login-exec.sh 
spawn ssh root@192.168.221.20
root@192.168.221.20's password: 
Last login: Sun Mar 18 11:14:46 2018 from 192.168.221.10
[root@apenglinux-002 ~]# touch /tmp/expect.txt
[root@apenglinux-002 ~]# echo expect > /tmp/expect.txt
[root@apenglinux-002 ~]# [root@localhost expect]

expect脚本传参数

vim /usr/local/sbin/expect/expect-parameter.sh
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "root"
set command [lindex $argv 2]
spawn ssh $user@$host
expect {
"yes/no"  { send "yes\r"; exp_continue}
"password:" { send  "$passwd\r" }
}
expect "]*"
send "$command\r"
expect "]*"
send "exit\r"
[root@localhost expect]# ./expect-parameter.sh root 192.168.221.20 "ifconfig;who;vmstat 1"
//正常情况下运行命令vmstat 1是不会停止的,但这儿就停止了,说明有超时时间限制,大约为10s

设置默认的超时时间

vim /usr/local/sbin/expect/expect-parameter.sh //加上下面一行
set timeout 8  //8s以后就结束
set timeout -1  //没有超时时间

expect脚本同步文件

在192.168.221.10中将192.168.221.20:/etc/passwd同步过来

//在两台机器上都要安装rsync
vim /usr/local/sbin/expect/expect-rsync.sh
#!/usr/bin/expect
set user "root"
set passwd "root"
set host "192.168.221.20"
set file "/etc/passwd"
set dir "/tmp/"
spawn rsync -av $user@$host:$file $dir
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" {send "$passwd\r"}
}
expect eof

将本地的文件(/tmp/passwd)同步到192.168.221.20机器中(传参)

vim /usr/local/sbin/expect/expect-local-another-rsync.sh
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set file "/tmp/passwd"
set path [lindex $argv 2]
set passwd "root"
spawn rsync -av $file $user@$host:$path
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" {send "$passwd\r"}
}
expect eof

[root@localhost expect]# ./expect-local-another-rsync.sh root 192.168.221.20 "/tmp/password.txt"
spawn rsync -av /tmp/passwd root@192.168.221.20:/tmp/password.txt
root@192.168.221.20's password: 
sending incremental file list
passwd

sent 920 bytes  received 31 bytes  634.00 bytes/sec
total size is 846  speedup is 0.89