shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd、scp
对自动部署免去用户交互很痛苦,expect能很好的解决这类问题。
expect的核心是spawn expect send set
spawn 调用要执行的命令
expect 等待命令提示信息的出现,也就是捕捉用户输入的提示:
send 发送需要交互的值,替代了用户手动输入内容
set 设置变量值
interact 执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。
expect eof 这个一定要加,与spawn对应表示捕获终端输出信息终止,类似于if....endif
expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了。
设置expect永不超时
set timeout -1
设置expect 300秒超时,如果超过300没有expect内容出现,则推出
set timeout 300
expect编写语法,expect使用的是tcl语法。
一条Tcl命令由空格分割的单词组成. 其中, 第一个单词是命令名称, 其余的是命令参数
cmd arg arg arg
foo
方括号执行了一个嵌套命令. 例如, 如果你想传递一个命令的结果作为另外一个命令的参数, 那么你使用这个符号
[cmd arg]
双引号把词组标记为命令的一个参数. ""符号, 引号,方括号和大括号的特殊含义
expect使用实例
1。首先确认expect的包要安置。
rpm -qa | grep expect
如果没有则需要下载安装,
yum install expect
2.安装完成后,查看expect的路径,可以用
which expect
/usr/bin/expect
3.编辑脚本
vi test.sh
添加如下内容
#!/usr/bin/expect -f //这个expect的路径就是用which expect 查看的结果
spawn su - nginx //切换用户
expect "password:" //提示让输入密码
send "testr" //输入nginx的密码
interact //操作完成
4.确定脚本有可执行权限
chmod +x autosu.sh
5.执行脚本 expect autosu.sh 或 ./autosu.sh
expect常用脚本
登陆到远程服务器:
#!/usr/bin/expect
set timeout 5
set server [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh -l $user $server
expect {
"(yes/no)" { send "yesr"; exp_continue }
"password:" { send "$passwdr" }
}
expect "*Last login*" interact
scp拷贝文件
#!/usr/bin/expect
set timeout 10
set host [lindex $argv 0] //第1个参数,其它2,3,4参数类似
set username [lindex $argv 1]
set password [lindex $argv 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn scp $src_file $username@$host:$dest_file
expect {
"(yes/no)?"
{
send "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:"
{
send "$passwordn"
}
}
expect "100%"
expect eof
#!/bin/bash
set timeout 30
expect<<-END
spawn ssh-keygen -t rsa
expect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send "\n"
expect "Overwrite (y/n)? "
send "\n"
expect eof
exit
END
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
echo $i
expect<<-END
spawn ssh root@$i "mkdir /root/.ssh/"
expect "password: "
send "du77\n"
expect eof
exit
END
expect<<-END
spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
expect "password: "
send "du77\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "touch /root/.ssh/authorized_keys"
expect "password: "
send "du77\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
expect "password: "
send "du77\n"
expect eof
exit
END
done
#!/bin/bash
set timeout 30
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
echo $i
expect<<-END
spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
expect "password: "
send "123@123\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "touch /root/.ssh/authorized_keys"
expect "password: "
send "123@123\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
expect "password: "
send "123@123\n"
expect eof
exit
END
done