expect是什么?
expect是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预。说白了,expect就是一套用来实现自动交互功能的软件。
在实际工作中,我们运行命令、脚本或程序时,这些命令、脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行。而利用expect,则可以根据程序的提示,模拟标准输入提供给程序,从而实现自动化交互执行。这就是expect!!!
tcl8.6.4-src.tar.gz
wget http://iweb.dl.sourceforge.net/project/tcl/Tcl/8.6.4/tcl8.6.4-src.tar.gz
expect5.45.4.tar.gz
wget https://downloads.sourceforge.net/expect/expect5.45.4.tar.gz
2.1 先export 几个常量
export tcl_cv_type_64bit="long long"
export ac_cv_c_bigendian=no
export tcl_cv_strtod_buggy=1
export ac_cv_func_strstr=yes
export ac_cv_func_opendir=yes
2.2解压
mkdir compile
cd compile
tar -zxvf tcl8.6.4-src.tar.gz
2.3.生成Makefile
cd tcl8.6.4/unix/
./configure --prefix=$PWD/tmp --host=mipsel-openwrt-linux --build=i686-linux
--prefix=$PWD/tmp:编译后的结果保存在当前目录的tmp文件夹里
--host=mipsel-openwrt-linux: MT7628的编译器
--build=i686-linux:编译主机
2.4.修改Makefile
vim Makefile
/LIBS
修改Makefile,在LIBS 中增加 -lpthread
LIBS = -lpthread -ldl -lm
2.5 make
2.6 make install
最后生成 libtcl8.6.so 和init.tcl 是expect要用到的。
3.1 解压
tar expect5.45.4.tar.gz
3.2 生成Makefile
tar expect5.45.4.tar.gz
cd expect5.45.4
./configure --prefix=$PWD/tmp --with-tcl=/home/xxxx/tmp/expect/compile/tcl8.6.4/unix/tmp/lib --with-tclinclude=/home/xxxx/tmp/expect/compile/tcl8.6.4/unix/tmp/include
--with-tcl:指定tcl库
--with-tclinclude:指定tcl头文件夹
3.3 修改Makefile
vim Makefile //编辑Makefile
/gcc //查找gcc
将原来的
CC = gcc
修改为
CC = mipsel-openwrt-linux-gcc
/-ldl -lm //查找-ldl -lm
在同行添加
-lpthread -lutil
/-lieee //查找ieee选项
去除
原来为:
LIBS = -lutil -lieee -lm -Wl,-rpath,${LIB_RUNTIME_DIR}
改为:
LIBS = -lutil -lm -Wl,-rpath,${LIB_RUNTIME_DIR}
3.4 修改 exp_command.c
vim exp_command.c
去除 #include
增加 #include
3.5 make
此时 生成 expect 和 libexpect5.45.4.so 即完成编译。
将以下文件copy到板子相应的目录,即可使用expect了,我用来自动SSH登陆并实现通过外网访问内网的web
expect -->/bin
init.tcl-->/lib/tcl8.6
libexpect5.45.4.so libtcl8.6.so-->/lib
示例脚本:
#!/bin/ash
remotehost=$1
remoteport=$2
remoteuser=$3
passwd=$4
localhost=$5
/bin/expect <<-EOF
set timeout 300
spawn ssh -Nf -R 0.0.0.0:$remoteport:$localhost:80 $remoteuser@$remotehost
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$passwd\r" }
}
expect eof
EOF
不想编译的同学可以直接下载已经编译好的库
https://download.csdn.net/download/wxhjk/12058459
本文参考以下网文
https://blog.csdn.net/aa120515692/article/details/47122773
https://blog.csdn.net/aa120515692/article/details/47122773
https://blog.csdn.net/aysa_bear/article/details/51313321
https://www.jianshu.com/p/2fcdf764f464