使用expect可以省去手动交互的过程,比如scp的密码
曾遇到这样一段代码:(Bash脚本)
#!/usr/bin/expect -f
set HOST "192.168.102.1"
set USER "codefor"
set PASS "codefor"
set BOOKFILE "/home/codefor"
#upload
spawn "scp *.zip $USER@$HOST:$BOOKFILE"
expect {
"*password*" {send "$PASS\r";}
}
expect eof;
这样的话,会报不存在*.zip的错误,即不能解析通配符*
在命令前加上bash -c即可。
如下:
#!/usr/bin/expect -f
set HOST "192.168.102.1"
set USER "codefor"
set PASS "codefor"
set BOOKFILE "/home/codefor"
#upload
spawn bash -c "scp *.zip $USER@$HOST:$BOOKFILE"
expect {
"*password*" {send "$PASS\r";}
}
expect eof;
the End.