怎么写脚本实现自动输入密码

上周领导让我同步数据,就是从ftp服务器获取文件,然后将dat数据文件同步到数据库中,最初我是用java编写的,但是后来发现数据量太大,根本没法在规定的时间同步过来,后来想到了用Oracle自带的sqlldr命令同步数据,但是发现服务器之间没有建立信任关系,那在连接的时候就必须输入密码,这仅仅通过shell是实现不了的,通过上网查找,需要安装expect才能实现自动输入密码。在此记录下来。

1、安装expect

首先安装tcl(下载地址:http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz)

解压

tar -zxvf tcl8.4.11-src.tar.gz

cd tcl8.4.11/unix/

./configure

make && make install

安装expect(下载地址:http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download)

tar -zxvf expect5.45.tar.gz

cd expect5.45

./configure –with-tclinclude=/tmp/tcl8.4.11/generic –with-tclconfig=/usr/local/lib/

make && make install

注:这里的tcl是在/tmp/目录下的。

检查安装情况

[root@node1 expect5.45]# expect
expect1.1>
expect1.1>

安装完成.

2.写一个远程脚本

vi scp.exp

!/usr/local/bin/expect

spawn rm -rf /home/crmDate/date/crmDate

set timeout 3000

spawn scp -r [email protected]:/home/ftptest/ldx/crmDate /home/crmDate/date

set timeout 10

expect {

assword:” {

set timeout 1000

set passwderror 1

send “root\r”

exp_continue

}

es/no)?” {

send “yes\r”

exp_continue

}

timeout {

puts “connect is timeout”

exit 3

}

}

这样一个备份脚本就写好了。

3.在写一个ctl脚本。

vi CRM_INFO.ctl

load data

CHARACTERSET AL32UTF8

Infile ‘/home/crmDate/date/crmDate/CRM_V_P_CUSTINFO.dat’

truncate

into table CRM_INFO

fields terminated by ‘^D’ optionally enclosed by ‘”’ trailing nullcols

(

CUST_NO “TRIM(:CUST_NO )”,

(这里是对应的字段)

DATE “to_date(:DATE,’YYYYMMDD’ )”

)

这样就能将备份过来的dat文件导入到数据库中了。

本文借鉴出处

https://blog.csdn.net/silenceray/article/details/54582345

你可能感兴趣的:(自动输入密码)