日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/21 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.sh4 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.gzcd 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.gzcd 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/genericmake 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.sh4 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

總結

以上是生活随笔為你收集整理的linux下scp远程拷贝文件无需输入密码工具之expect的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。