本文共 2086 字,大约阅读时间需要 6 分钟。
转自:http://www.nginx.cn/1934.html
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. $foo 方括号执行了一个嵌套命令. 例如, 如果你想传递一个命令的结果作为另外一个命令的参数, 那么你使用这个符号 [cmd arg] 双引号把词组标记为命令的一个参数. "$"符号和方括号在双引号内仍被解释 "some stuff" 大括号也把词组标记为命令的一个参数. 但是, 其他符号在大括号内不被解释 {some stuff} 反斜线符号是用来引用特殊符号. 例如:n 代表换行. 反斜线符号也被用来关闭"$"符号, 引号,方括号和大括号的特殊含义 expect使用实例 1。首先确认expect的包要安置。 #rpm -qa | grep expect 如果没有则需要下载安装, #yum install expect 2.安装完成后,查看expect的路径,可以用 #which expect /usr/bin/expect 3.编辑脚本 #vi autosu.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 使用方法 ./expect_scp 192.168.75.130 root 123456 /root/src_file /root/dest_file 以上的命令执行后,将把本地/root目录下的src_file文件拷贝到用户名为root,密码为123456的主机192.168.75.130中的/root下,同时还将这个源文件重命名为dest_file
转载地址:http://mdyxi.baihongyu.com/