一、有时需要在脚本中登录需要密码验证的机器或者使用脚本ssh scp登录 复制文件时,需要输入密码,使用expect就能对不同输入进行交互。
通过简单脚本就可以实现自动输入密码了:
expect -c ”
set timeout 5;
spawn /usr/bin/scp -r /home/dev/log [email protected]:/home/dev/
expect {
\”yes/no\” {send \”yes\r\”; exp_continue}
\”password\” {send \”yourpassword\r\”;} #修改成远程主机用户的密码
}
expect eof;”
上面脚本如果 timeout不设定就是为10秒。
由于expect是基于tcl语言的,所以移植expect前,需先交叉编译tcl
1、下载地址
tcl:http://www.tcl.tk/software/tcltk/download.html
expect:http://sourceforge.net/projects/expect/files/latest/download?source=files
2、交叉编译tcl
[email protected]:~/arm/download tar−zxftcl8.6.3−src.tar.gzlingd@ubuntu: /arm/download cd tcl8.6.3/unix/
[email protected]:~/arm/download/tcl8.6.3/unix exportaccvfuncstrtod=yeslingd@ubuntu: /arm/download/tcl8.6.3/unix export tcl_cv_strtod_buggy=1
[email protected]:~/arm/download/tcl8.6.3/unix ./configure–prefix=/home/lingd/arm/install/tcl–host=arm−linuxlingd@ubuntu: /arm/download/tcl8.6.3/unix make
[email protected]:~/arm/download/tcl8.6.3/unix makeinstalllingd@ubuntu: /arm/download/tcl8.6.3/unix ls /home/lingd/arm/install/tcl/
bin include lib man share
[email protected]:~/arm/download/tcl8.6.3/unix ls/home/lingd/arm/install/tcl/bin/tclsh8.6lingd@ubuntu: /arm/download/tcl8.6.3/unix ls /home/lingd/arm/install/tcl/lib/
itcl4.0.2 libtcl8.6.so libtclstub8.6.a pkgconfig sqlite3.8.7.1 tcl8 tcl8.6 tclConfig.sh tclooConfig.sh tdbc1.0.2 tdbcmysql1.0.2 tdbcodbc1.0.2 tdbcpostgres1.0.2 thread2.7.1
注意,/configure …… 之前应该先:
export tcl_cv_strtod_buggy=1
export ac_cv_func_strtod=yes
否则,交叉编译tcl时会出现以下错误(普通编译无此错误)
fixstrtod.o: In function
fixstrtod':
fixstrtod.c:(.text+0x0): multiple definition of
fixstrtod’
strtod.o:strtod.c:(.text+0x0): first defined here
http://blog.163.com/kesic2004_com/blog/static/1655294792013018104555314/
解决方法:
3、移植expect
[email protected]:~/arm/download/tcl8.6.3/unix cd../..lingd@ubuntu: /arm/download ls
expect5.45.tar.gz hello hello.c tcl8.6.1-src.tar.gz tcl8.6.3 tcl8.6.3-src.tar.gz type9
[email protected]:~/arm/download tar−zxfexpect5.45.tar.gzlingd@ubuntu: /arm/download cd expect5.45/
注意,expect不支持交叉编译,所以在configure,我没有用–host=arm-linux,而是先用默认的X86 gcc,等configure后再修改Makefile
configure配置参数:
1)–prefix=/home/lingd/arm/install/expect:指定安装目录
2)–with-tcl=/home/lingd/arm/install/tcl/lib:指定tcl所在目录,通常指定为tcl安装目录下的lib路径
3)–with-x=no: 告诉配置脚本,不要查找 tk (tcl 的 GUI 组件) 或 X 窗口系统库
4)–with-tclinclude=/home/lingd/arm/install/tcl/include:指定tcl头文件所在目录,通常指定为tcl安装目录下的include路径
[email protected]buntu:~/arm/download/expect5.45 ./configure–prefix=/home/lingd/arm/install/expect–with−tcl=/home/lingd/arm/install/tcl/lib–with−x=no–with−tclinclude=/home/lingd/arm/install/tcl/includelingd@ubuntu: /arm/download/expect5.45 vim Makefile
将Makefile中
CC = gcc
改为
CC = arm-linux-gcc
[email protected]:~/arm/download/expect5.45 makelingd@ubuntu: /arm/download/expect5.45 make install
make install会出现错误,可以不管,只要能正常编译生成expect和libexpect5.45.so即可
3、安装
在expect源码目录下可找到expect和libexpect5.45.so
在tcl安装目录下可找到libtcl8.6.so和tcl8.6目录
将libexpect5.45.so、libtcl8.6.so和tcl8.6目录拷贝到开发板/lib目录下
将expect拷贝到/bin目录下
如果开发板无libz.so.1库,还需要拷贝libz.so.1.2.8到开发板,并创建软链接libz.so.1