需求:A机器上定时抓取了数据需要push到B机器上然后提交到hdfs,因为B机器每次必须需要输入用户名和密码,所以想到了expect
A机器上执行
先安装expect
yum install -y expect
#以下内容保持为hdfs_sync.sh
#!/usr/bin/expect -f
set ip B机器IP
set password 登陆B机器的密码
set timeout -1 #设置没有超时时间
set cc [lindex $argv 0] #设置传递$1 参数给 cc 变量
spawn ssh B_name@$ip #登陆B机器
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$password\r" }
}
expect "$*"
send -- "$cc\r" #执行cc命令
send -- "exit\r\r"
expect eof
#这样我们可以在另外的shell脚本里调用这个方法
#!/bin/sh
#代码
/usr/bin/expect /data/scripts/hdfs_sync.sh XXX #其中XXX就是传递的$1参数,是B机器上我们写好的put数据到hdfs的脚本