加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 20:02:29 所属栏目:安全 来源:网络整理
导读: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 pa

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 "yesn"
# expect "*assword:" { send "$passwordn"}
# }
     "*assword:"
         {
             send "$passwordn"
         }
     }
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 "$passwordn"
         }
     }
#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"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读