expect

参考 https://github.com/tzlwin/my-scripts/tree/master/expect_scripts
http://inguza.com/document/expect-and-tcl-mini-reference-manual expect-manual
http://bbs.chinaunix.net/thread-3566066-1-1.html FAQ
http://bbs.chinaunix.net/thread-639081-1-1.html FAQ
http://bbs.chinaunix.net/thread-594417-1-1.html FAQ
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=674903&page=1#pid4559521
http://linux.chinaunix.net/techdoc/beginner/2009/03/02/1100264.shtml zh_cn manual
http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html TCL-GOV
!/usr/bin/expect
su ssh deploy multideploy
exp_internal 1 调试信息

1 命令参数

http://www.startos.com/linux/tips/2011010720609.html
参数 -d 调试 -b 逐条执行
expect可以让你使用“-c”选项,直接在命令行中执行它,如下所示:
$ expect -c ‘expect “\n” {send “pressed enter\n”} 按下回车后,打印出press…
用spawn来执行一条shell命令,不然你的输出都没用。 -noecho 参数表示没有回显
send_user 相当于echo 让界面显示的内容

#

2 接收输入参数

set a [lindex args0]....... argc 表明参数个数
set arg [append arg arg[lindex argv $i] ” “] 参数拼接

3 计算

[expr 2-1]

4 控制语句

5 函数

proc do_login {login pass} { # 这个{不能换行
…………
}

5 调用shell变量

!/usr/bin/expect -f
set password 123456
set date [exec date "+%Y-%m-%d"]
spawn /usr/bin/scp root@1.1.1.1://backup/test-file-$date /test/backup/
expect "assword:"
send -- "$password\r"
expect "assword:"
send -- "$password\r"
expect eof

返回结果给shell

expect 返回值给bash,expect中 system “echo a”

expect 详解

这样是并行匹配,也是比较推荐的一种
exp_continue 是继续循环匹配

expect {
    "assword" {
        send_user "sudo now\n"
        send "$passwd\n"
        exp_continue
    }
    eof
    {
        send_user "eof\n"
    }
}

expectout(0,string),re使 expect_out(buffer) 匹配到的信息包括在此之前未被匹配到的,但是后面没有匹配到的不保留

命令匹配以$ $ 来匹配所有内容
字符串操作

文件操作
打开文件 set file [open tools.ret w+]
写入文件 puts file “success”  
读取文件   
if {[file isfile
file]!=1}
{
send_user “ argv0:file file not found.

exit }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值