linux下带密码的scp,linux下带密码的scp

from linux下带密码的scp (2014-03-10 12:35:46)

背景: 需要将一台机器的数据传输到另外一台机器上,两台机器没有信任关系,没有数据源一方没有ftp或者http,无法进行wget 解决方法: 在其中一台机器安装expect,在程序中写上密码等信息 准备: 需要root权限 进行expect安装 expect安装步骤: 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

3.安装配置

cd tcl8.4.11/unix

./configure --prefix=/usr/tcl --enable-shared

make

make install

安装完毕以后,进入tcl源代码的根目录,把子目录unix下面的tclUnixPort.h copy到子目录generic中。

暂时不要删除tcl源代码,因为expect的安装过程还需要用。

B. expect 安装 (需Tcl的库)

主页: 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

3.安装配置

cd expect5.45

./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.11/generic

make

make install

ln -s /usr/tcl/bin/expect /usr/expect/bin/expect

expect脚本:

#!/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

#!/bin/sh

list_file=$1

src_file=$2

dest_file=$3

cat $list_file | while read line

do

host_ip=`echo $line | awk '{print $1}'`

username=`echo $line | awk '{print $2}'`

password=`echo $line | awk '{print $3}'`

echo "$host_ip"

./expect_scp $host_ip $username $password $src_file $dest_file

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值