linux下的expect的安裝和使用
阿新 • • 發佈:2019-02-11
1、安裝expect
首先安裝tcl(下載地址:http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz)解壓
tar -zxvf tcl8.4.11-src.tar.gz
cd tcl8.4.11/unix/
./configure
make && make install
安裝expect(下載地址:http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download)
tar -zxvf expect5.45.tar.gz
cd expect5.45
./configure --with-tclinclude=/tmp/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/
make && make install
檢查安裝情況
[[email protected] expect5.45]# expect
expect1.1>
expect1.1>
安裝完成.
2、寫一個可以遠端備份的指令碼
vi scp.exp
#!/usr/local/bin/expect -f
spawn scp -r /opt/test / [email protected]:/home/
set timeout 10
expect {
"yes/no" {send "yes\r";exp_continue}
}
expect "[email protected] 's password:"
exec sleep 1
send "123\n" #密碼
interact
在crontab -e中加入定時任務
0 23 * * * /tmp/scp.exp #每天23點執行任務
如果出現報錯:/bin/sh: /tmp/scp.exp: /usr/bin/expect: bad interpreter: No such file or directory
將指令碼中的標頭檔案
#!/usr/bin/expect 改成#!/usr/local/bin/expect