scp shell脚本无需密码输入

一,安装expect

1、yum install expect

 

二、实例

查看下面脚本,可以传输本地的目录到远程节点目录,而不需要交互输入密码。

#!/bin/bash

# Function : transfer directory from localhost to remote node
# args 		 : ip password local_dir remote_dir
# Author   : firefoxbug
# Date     : 2013/06/25

if [ $# -ne 4 ]
then
  echo "[ERROR] usage : ./send_dir ip password source_dir dst_dir"
  exit 1
fi
ip=$1
port="22"
passwd=$2
local_dir=$3
remote_dir=$4

/bin/rpm -qa|/bin/grep -q expect
if [ $? -ne 0 ];then
  echo "please install expect"
  exit
fi

expect -c "
  spawn scp -r -P $port $local_dir root@$ip:$remote_dir
  expect {
    \"*assword\" {set timeout 300; send \"$passwd\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
  }
  expect eof"

脚本执行

./send_dir ip password source_dir dst_dir

对于普通的 ssh ,返回下面的结果

[root@firefoxbug volcano]# ssh 223.4.238.138
reverse mapping checking getaddrinfo for ip223.hichina.com [223.4.238.138] failed - POSSIBLE BREAK-IN ATTEMPT!
root@223.4.238.138's password:

如果未经验证则返回下面结果。

[root@firefoxbug volcano]# ssh 223.4.238.138
The authenticity of host '223.4.238.138 (223.4.238.138)' can't be established.
RSA key fingerprint is 7b:e5:f3:61:07:01:93:2c:01:6c:2e:77:39:88:a5:bb.
Are you sure you want to continue connecting (yes/no)?

expect 就能根据正则匹配来自动输入。

转载于:https://my.oschina.net/hycx227/blog/693098

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值