微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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"

原文地址:https://www.jb51.cc/bash/388941.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐