Expect可以在Linux下用于交互式处理,比如利用scp的时候,需要输入目标机器的密码,解压jdk时需要按下回车等操作,这些利用Expect都可以自动的完成,不需要人为参与。
自己写了一个简单的expect实现scp不用手动输入目标机器密码的小脚本,其中包含了接收命令行参数。
#!/usr/bin/expect
set username [lindex $argv 0]
#destination file path
set despath [lindex $argv 1]
#destination host password
set passwd [lindex $argv 2]
#file need to be transfer
set filepath [lindex $argv 3]
spawn scp $filepath $username@$despath
expect "password:"
send "$passwd\n"
expect eof
#call script
./test.sh root ip:/usr/local abc /usr/local/test.txt