shell远程执行脚本、传输数据

1.远程无密码拷贝数据

适用于未做SSH或避免做SSH的异集群。
底层采用 except 脚本。

vim ./remote_scp.exp


#!/usr/bin/expect

# 设置2小时超时时间
set timeout 7200
# 获取参数,从0开始
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
# except指脚本的预期,比如在识别到屏幕输出为‘password’时候自动发送密码
expect {
#     "(yes/no)?"
#         {
#             send "yes\n"
#             expect "*assword:" { send "$password\n"}
#         }
     "*assword:"
         {
             send "$password\n"
         }
     }
expect "100%"
expect eof

外层封装shell脚本


#!/bin/sh

. /etc/profile
. ~/.bash_profile

SCRIPT_NAME=$0
echo ${SCRIPT_NAME}

# SCP 无密码登录
# 底层调用Except脚本
# 注意: 输入参数的时候,包含特殊字符的需要进行转义处理
#        如:sh ./scp.sh 192.168.1.162 dg-hadoop douguo2017\!\@\# ./test.log /opt/DATA/goldmine/src/hbase/res/testexceptshell.log
# author zhangjianfei
# since 1.0.0

# 1. set workdir
WORK_DIR=`dirname ${SCRIPT_NAME}`
echo ${WORK_DIR}
cd ${WORK_DIR}

# 2. args check
if [ $# -eq 5 ]
 then
        host=$1
        username=$2
        password=$3
        src_file=$4
        dest_file=$5
 else
        echo "the args is wrong ,you should give it like 'dg_user'"
        exit 1;
fi

# 3. body
echo "$host $username $password $src_file $dest_file"

/opt/DATA/goldmine/src/hbase/utils/remote_scp.exp $host $username $password $src_file $dest_file

2.远程调用脚本

和SCP一样,不过这边使用了SSH

vim  ./remote_ssh.exp


#!/usr/bin/expect

set timeout 7200
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]

#ssh dg-hadoop@192.168.1.162 "sh /opt/DATA/goldmine/src/hbase/testssh.sh"
spawn ssh $username@$host "$command"
expect {
     "*assword:"
         {
             send "$password\n"
         }
     }
#expect "100%"
expect eof

外层shell封装

vim ./remote_ssh_util.sh


#!/bin/sh

. /etc/profile
. ~/.bash_profile

SCRIPT_NAME=$0
echo ${SCRIPT_NAME}

# SSH 无密码执行远程脚本
# 底层调用Except脚本
# 注意: 输入参数的时候,包含特殊字符的需要进行转义处理
#        如:sh ./scp.sh 192.168.1.162 dg-hadoop douguo2017\!\@\# ./test.log /opt/DATA/goldmine/src/hbase/res/testexceptshell.log
# author zhangjianfei
# since 1.0.0

# 1. set workdir
WORK_DIR=`dirname ${SCRIPT_NAME}`
echo ${WORK_DIR}
cd ${WORK_DIR}

echo "args num: $#"

# 2. args check
if [ $# -eq 4 ]
 then
        host=$1
        username=$2
        password=$3
        command=$4
 else
        echo "the args is wrong ,you should give it like 'dg_user'"
        exit 1;
fi

# 3. body
echo "$host $username $password $command"

/opt/DATA/goldmine/src/hbase/utils/remote_ssh.exp "$host" "$username" "$password" "$command"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值