Chinaunix首页 | 论坛 | 博客
  • 博客访问: 722480
  • 博文数量: 66
  • 博客积分: 2418
  • 博客等级: 大尉
  • 技术积分: 1659
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-28 10:04
个人简介

keep moving

文章分类

全部博文(66)

文章存档

2015年(2)

2014年(6)

2013年(7)

2011年(7)

2010年(42)

2009年(2)

分类: 嵌入式

2015-01-15 20:00:32

原文:移植自动交互工具expect5.45到arm linux(tcl8.6.3移植)

参考:

Linux shell 交互式编程、TCL/TK 和 Expect 编译与安装、expect 编程
Expect+TCL8.6交叉编译 arm-linux


工作需要telnet/ssh自动登录远程设备并进行一些固定操作。上网搜了一下,刚好有个自动交互工具expect。
我的设备都是基于arm的。网上对expert移植到arm的文章特少,特此记录,以备不时之需。
由于expect是基于tcl语言的,所以移植expect前,需先交叉编译tcl
1、下载地址
tcl:
expect:
2、交叉编译tcl
lingd@ubuntu:~/arm/download$ tar -zxf tcl8.6.3-src.tar.gz 
lingd@ubuntu:~/arm/download$ cd tcl8.6.3/unix/
lingd@ubuntu:~/arm/download/tcl8.6.3/unix$ export ac_cv_func_strtod=yes 
lingd@ubuntu:~/arm/download/tcl8.6.3/unix$ export tcl_cv_strtod_buggy=1
lingd@ubuntu:~/arm/download/tcl8.6.3/unix$ ./configure --prefix=/home/lingd/arm/install/tcl --host=arm-linux
lingd@ubuntu:~/arm/download/tcl8.6.3/unix$ make
lingd@ubuntu:~/arm/download/tcl8.6.3/unix$ make install
lingd@ubuntu:~/arm/download/tcl8.6.3/unix$ ls /home/lingd/arm/install/tcl/
bin  include  lib  man  share
lingd@ubuntu:~/arm/download/tcl8.6.3/unix$ ls /home/lingd/arm/install/tcl/bin/
tclsh8.6
lingd@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
lingd@ubuntu:~/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
lingd@ubuntu:~/arm/download$ tar -zxf expect5.45.tar.gz 
lingd@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路径
lingd@ubuntu:~/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/include
lingd@ubuntu:~/arm/download/expect5.45$ vim Makefile
将Makefile中
CC = gcc
改为
CC = arm-linux-gcc
lingd@ubuntu:~/arm/download/expect5.45$ make
lingd@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
4、expect应用
先看一个export脚本范例:通过指定端口,自动telnet登录到指定主机,并将指定目录包含的文件名输出到主机/tmp/dir文件中
#指明脚本解析程序为/app/bin/expect
#!/app/bin/expect
#判断输入参数
if {$argc != 3} {
send_user "usage: $argv0 ip port directory \n"
exit
}


#设置变量 
set IP [lindex $argv 0]
set PORT [lindex $argv 1]
set DIR [lindex $argv 2]
#设置expect命令等待时间
set timeout 30


#telnet远程主机
spawn telnet $IP $PORT


#telnet用户登录
expect "*login*"
send "root\n"
expect "Password:"
send "globaltech\n"
#shell交互
expect "*# "
send "cd /tmp/ \n"
expect "*# "
send "ls $DIR > dir \n"
expect "*# "
send "exit \n"
send_user "\n$argv0 finish! \n"


从上例可以看出,expect自动交互最常用3个命令:spawn,expect和send
1)expert命令可以接收一个字符串参数,也可以接收正则表达式参数,用于等待一个进程的反馈。如果进程反馈信息中包含这个字符串或正则表达式,则立即返回;否则,等待一段时间后返回,等待时间由timeout变量决定
2)send命令接收一个字符串参数,并将该参数发送给一个进程
3)spawn命令接收一个shell命令(通常为telnet,ssh,ftp等),并启动一个新进程来运行这个shell命令。spawn后的send和expect命令都是和spawn打开的进程进行交互的,spawn前的send和expect命令是和标准输入输出进行交互
4)send_user命令接收一个字符串参数,并将该参数输出到标准输出
以上命令都是实现自动交互的,如果需要人为干预可以用interact命令

expect用户指南:

原文:移植自动交互工具expect5.45到arm linux(tcl8.6.3移植)


阅读(4903) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~