linux2--expect

Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。

Expect需要Tcl编程语言的支持,要在系统上运行Expect必须首先安装Tcl。

主要shell命令:

#!/usr/bin/expect: 告诉os用什么脚本

*set *: 设置参数

spawn:spawn是进入expect环境后才可以执行的expect内部命令

*expect *:是期待返回什么

send:发送命令

exit:退出

示例:一段传输文件的shell
#!/usr/bin/expect

set src_file [lindex $argv 0]  
set username [lindex $argv 1]  
set host_ip [lindex $argv 2]  
set dest_file [lindex $argv 3]  
set password [lindex $argv 4]

spawn scp  $src_file $username@$host_ip:$dest_file  
expect {  
    "(yes/no)?"  
    {  
            send "yes\n"  
            expect "*assword:" {send "$password\n"}  
    }  
    "*assword:"  
    {  
            send "$password\n"  
    }  
}  
expect "100%"  
expect eof  

参考:

http://www.cnblogs.com/iloveyoucc/archive/2012/05/11/2496433.html

你可能感兴趣的:(linux2--expect)