linux下scp远程拷贝文件无需输入密码工具之expect

0.前言

expect是一个用来处理交互命令的工具。借助expect,我们可以将交互过程写在一个脚本上,使之自动化完成。比如ssh登录,scp远程拷贝,ftp登录等都是交互命令。

在有外网的情况下可以使用命令安装,这里的目标是将编译后的程序作为一个绿色化的工具,可以直接携带到部署服务器上使用,所以这里使用源码编译。

1.下载源码

由于expect依赖tcl库,所以编译expect之前先下载tcl的源码,编译依赖动态库。

A. Tcl

主页: http://www.tcl.tk

下载地址: http://www.tcl.tk/software/tcltk/downloadnow84.tml

1.下载源码包

wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz

2.解压缩源码包

tar xfvz tcl8.4.11-src.tar.gz


B. expect

主页: http://expect.nist.gov/

1.下载源码包

wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download

2.解压缩源码包

tar xzvf expect5.45.tar.gz

完成上述步骤后,目录结构如下:

$ tree -L 1
.
├── bin
├── build-expect.sh
├── build-tcl.sh
├── expect5.45
├── expect5.45.tar.gz
├── expect-tool
├── tcl8.4.11
├── tcl8.4.11-src.tar.gz
└── test.sh

4 directories, 5 files

里面的build-expect和build-tcl分别是生成exepct和tcl的脚本,expect-tool是tcl和expect安装目录,bin是拷贝出来的执行程序和lib库,test是一个测试脚本。

2.编译

由于使用configure命令生成的安装目录中只能是绝对路径,而且生成的makefile里面使用了rpath选项,导致依赖了具体的路径,在其他的地方使用会出现找不到依赖库。所以这里先使用脚本生成makefile,然后修改一下生成的makefile,修改安装目录为相对路径,去掉rpath选项,依赖动态库路径。

tcl编译脚本:

$ cat build-tcl.sh 
#!/bin/bash

#tar xfvz tcl8.4.11-src.tar.gz

cd tcl8.4.11/unix
./configure --prefix=. --enable-shared --enable-static
make
make install

Makefile2为修改后的makefile

$ diff Makefile Makefile2
30,31c30,31
< prefix			= .
< exec_prefix		= .
---
> prefix			= ../../expect-tool/tcl
> exec_prefix		= ../../expect-tool/tcl
33c33
< libdir			= ./lib
---
> libdir			= ../../expect-tool/tcl/lib
89c89
< TCL_PACKAGE_PATH	= ./lib
---
> TCL_PACKAGE_PATH	= ../../expect-tool/tcl/lib
220,221c220,221
< CC_SEARCH_FLAGS	= -Wl,-rpath,${LIB_RUNTIME_DIR}
< LD_SEARCH_FLAGS	= -Wl,-rpath,${LIB_RUNTIME_DIR}
---
> CC_SEARCH_FLAGS	= #-Wl,-rpath,${LIB_RUNTIME_DIR}
> LD_SEARCH_FLAGS	= #-Wl,-rpath,${LIB_RUNTIME_DIR}

expect编译脚本:

$ cat build-expect.sh 
#!/bin/bash

#tar xzvf expect5.45.tar.gz

cd expect5.45
home_path=/home/lsx/scripts-coders/shell/expect-dir
./configure --prefix=$home_path/expect-tool/expect --exec-prefix=$home_path/expect-tool/tcl \
    --enable-static --with-tcl=../expect-tool/tcl/lib --with-tclinclude=../tcl8.4.11/generic

make
make install
ln -s $home_path/expect-tool/tcl/bin/expect $home_path/expect-tool/expect/bin/expect

Makefile2为修改后的makefile

$ diff Makefile Makefile2
112,113c112,113
< prefix		= /home/lsx/scripts-coders/shell/expect-tool/expect
< exec_prefix	= /home/lsx/scripts-coders/shell/expect-tool/tcl
---
> prefix		= ../expect-tool/expect
> exec_prefix	= ../expect-tool/tcl
152c152
< SHLIB_LD_LIBS	= ${LIBS} -L../../expect-tool/tcl/lib -ltclstub8.4
---
> SHLIB_LD_LIBS	= ${LIBS} -L../expect-tool/tcl/lib -ltclstub8.4
155,156c155,156
< TCL_BIN_DIR	= /home/lsx/scripts-coders/shell/expect-tool/tcl/lib
< TCL_SRC_DIR	= /home/lsx/scripts-coders/shell/tcl8.4.11
---
> TCL_BIN_DIR	= ../expect-tool/tcl/lib
> TCL_SRC_DIR	= ../tcl8.4.11
172c172
< TCLSH_PROG	= /home/lsx/scripts-coders/shell/expect-tool/tcl/bin/tclsh8.4
---
> TCLSH_PROG	= ../expect-tool/tcl/bin/tclsh8.4
176c176
< INCLUDES	=  -I. -I"." -I"/home/lsx/scripts-coders/shell/tcl8.4.11/generic" -I"/home/lsx/scripts-coders/shell/tcl8.4.11/unix"
---
> INCLUDES	=  -I. -I"." -I"../tcl8.4.11/generic" -I"../tcl8.4.11/unix"
191c191,192
< LIBS		=    -lutil -lieee -lm -Wl,-rpath,${LIB_RUNTIME_DIR}
---
> #LIBS		=    -lutil -lieee -lm -Wl,-rpath,${LIB_RUNTIME_DIR}
> LIBS		=    -lutil -lieee -lm
395,399c396,400
< 		-L/home/lsx/scripts-coders/shell/expect5.45 -lexpect5.45 \
< 		-L../../expect-tool/tcl/lib -ltcl8.4 \
< 		-ldl  -lieee -lm \
< 		-Wl,-rpath,${LIB_RUNTIME_DIR} \
< 		-Wl,-rpath,${LIB_RUNTIME_DIR}/${PACKAGE_NAME}${PACKAGE_VERSION}:../../expect-tool/tcl/lib
---
> 		-L../expect5.45 -lexpect5.45 \
> 		-L../expect-tool/tcl/lib -ltcl8.4 \
> 		-ldl  -lieee -lm
> #		-Wl,-rpath,${LIB_RUNTIME_DIR} \
> #		-Wl,-rpath,${LIB_RUNTIME_DIR}/${PACKAGE_NAME}${PACKAGE_VERSION}:../expect-tool/tcl/lib

3.绿化处理

一共要拷贝4个东西到绿化工具包中,expect主程序,两个依赖库libexpect,libtcl,还有一个tcl使用的配置文件,将tcl生成后的lib目录整个拷贝出来就行。

上述完成后,expect工具的目录结构如下:

$ tree -L 2 autoupdate/tools
autoupdate/tools
├── bin
│   ├── expect
│   ├── libexpect5.45.so
│   └── libtcl8.4.so
├── lib
│   └── tcl8.4
└── sbin
    └── docker.sh

4 directories, 4 files

其中lib目录里面就是tcl的配置文件了。

4.脚本测试

$ cat test.sh 
#!/usr/bin/expect
set timeout 10
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn scp $src_file $username@$host:$dest_file
expect {
"(yes/no)?"
{
    send "yes\n"
    expect "*assword:" { send "$password\n"}
}
"*assword:"
{
    send "$password\n"
}
}
expect "100%"
expect eof

5.参考资料

http://blog.csdn.net/jgmydsai/article/details/39858357

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值