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

前端之家收集整理的这篇文章主要介绍了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"

猜你在找的Bash相关文章