未能避免使用 scp 多次输入密码,并且循环不起作用



我编写了以下脚本,该脚本循环访问csv文件中的名称并将特定文件复制到远程计算机:

list_filepath=/home/bella/lists
destination_path=/home/pome/Documents/newfiles
NS_samples_source_path=/home/bella/final/acs/pa/1_ns
th_samples_source_path=/home/bella/final/acs/pa/4_th
ad_samples_source_path=/home/bella/final/new2018/ad
cat $list_filepath/new_list.csv | while read line
do
  if [[ $line == AD* ]]
     then
     echo $line
     ssh pome@203.177.32.11 mkdir -p $destination_path/$line
     cd $ad_samples_source_path/$line
     find $ad_samples_source_path/$line/ -regex '.*.(csv|xml|gz)$' -exec scp {} pome@203.177.32.11:$destination_path/$line ;
  elif [[ $line == NS* ]]
     then
     echo $line
     ssh pome@203.177.32.11 "mkdir -p $destination_path/$line"
     cd $NS_samples_source/$line
     find $NS_samples_source/$line/ -regex '.*.(csv|xml|gz)$' -exec scp {} pome@203.177.32.11:$destination_path/$line ;
  else
     echo $line
     ssh pome@203.177.32.11 "mkdir -p $destination_path/$line"
     cd $th_samples_source_path/$line
     find $th_samples_source_path/$line/ -regex '.*.(csv|xml|gz)$' -exec scp {} pome@203.177.32.11:$destination_path/$line ;
  fi
done

我有两个主要问题:

  1. 必须多次输入遥控器的密码,即使我设置了一个无密码密钥,如下所示:

    ssh-keygen
    ssh-copy-id -i pome@203.177.32.11
    

    它仍然要求为其复制的每个文件提供密码。

  2. 脚本在使用复制的文件创建一个目录后结束,似乎无法移动到 csv 文件中的下一行。

有人想如何解决这些问题吗?

谢谢!

第 1 点是因为本地主机上没有 SSH 代理或远程主机未配置为接受密钥,第 2 点是因为 SSH 将吞下您希望发送给read的标准输入。相关常见问题条目:

  • 我想自动化 ssh(或 scp,或 sftp(连接,但我不知道如何发送密码....
  • 我正在逐行读取文件并运行 ssh 或 ffmpeg,只处理第一行!

请检查rsync以获取您的解决方案。

此外,使用 SSH 密钥文件进行 SSH 身份验证更适合自动化。

最新更新