sshpass scp自动上传文件

交叉编译 sshpass

该命令可以用于脚本文件免去输入密码的命令

首先下载源码包

解压 tar -xvf sshpass-1.08.tar.gz

进入目录 cd sshpass-1.08

配置交叉编译器:

./configure --prefix=/home/bwton/project/sshpass-1.08/install_arm --host=arm-linux-gnueabihf CC=/opt/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-gcc

编译:

make -j8

会提示编译出错“undefined reference to 'rpl_malloc'”,这是因为config.h 有一个宏并没有定义到.

vim config.h

注释掉"#define malloc rpl_malloc"

再次make -j8

编译完成会在当前目录会生产该可执行文件文件sshpass

上传脚本

deploy.sh

#!/bin/sh

#set -x

src_file=./8-Scan_Qr_NFC.wav
dest_dir=/home/bwton/sounds/
ip_file="./ip.txt"
username=root
password=123456

# 目录不存在,退出
if [ ! -f $src_file ]; then
  echo "8-Scan_Qr.wav not exit"
  exit 0
fi

rm -rf "./result.txt"

while read -r line
do
   echo "./sshpass -p $password scp $src_file $username@$line:$dest_dir"
   ./sshpass -p $password scp $src_file $username@$line:$dest_dir
   if [ "x$?" = "x0" ];then
           echo "$line pass" >> "./result.txt"
   else
           echo "$line fail" >> "./result.txt"
   fi
done < $ip_file

echo "end"

ip.txt

root@DESKTOP-UJNM808:/home/lu/project/scp_demo$ cat ip.txt
192.168.24.10
192.168.24.11

问题

对于scp连接新的设备,经常会弹出“Are you sure you want to continue connecting (yes/no)?”

  1. 这个是ssh的一个RSA安全认证,必须选择yes才能连接,第一次选择yes后,会询问是否永久把这个新设备的RSA认证加入本地,选择yes后,以后不会再出现提醒。每次登陆输入密码即可。这种太麻烦需要人工干预,无法自动上传到多台设备。

  1. 也可以不用输入yes,但是需要修改本机配置。

vim /etc/ssh/ssh_config中的
#StrictHostKeyChecking ask 改成
StrictHostKeyChecking no

如果不按照2修改,使用sshpass scp指令自动上传不会弹出该提示,但会上传失败。

添加SSH登陆

找到/etc/ssh/sshd_config文件,找到“#PermitRootLogin prohibit-password”改为“PermitRootLogin yes”。

你可能感兴趣的:(sshpass,scp,shell)