【问题标题】:Failed to avoid entering password multiple times using scp, and looping not working无法避免使用 scp 多次输入密码,并且循环不起作用
【发布时间】:2019-10-14 18:58:38
【问题描述】:

我编写了以下脚本,循环遍历 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 文件中的下一行。

知道如何解决这些问题吗?

谢谢!

【问题讨论】:

    标签: bash loops ssh


    【解决方案1】:

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

    【讨论】:

    • 关于第 1 点,您是对的。我需要将我的密钥添加到 ssh-agent:ssh-add ~/.ssh/id_rsa。关于第2点,我还在想办法克服它
    • 第 2 点也通过在 ssh 命令中添加 -n 得到解决。谢谢!
    【解决方案2】:

    请查看rsync 以获得您的解决方案。

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

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 2020-08-01
      • 2018-09-26
      • 2011-07-12
      • 2019-11-28
      • 2019-02-15
      • 2013-07-26
      • 2020-01-20
      • 2023-03-27
      相关资源
      最近更新 更多