【问题标题】:Why does ssh abort my bash script?为什么 ssh 会中止我的 bash 脚本?
【发布时间】:2013-11-04 16:00:13
【问题描述】:

我正在自动将身份验证密钥上传到多个 ssh 主机,并且我想避免每次执行脚本时都将密钥添加到 authorized_keys,因此我在上传密钥之前检查 ssh 是否可以连接密钥身份验证。

问题是脚本在用户已经拥有密钥的第一台服务器上停止循环。

脚本根据bash/ssh test for public key authentication 的建议尝试密钥身份验证

while read SERVER; do
    CONN="$USER@$SERVER"
    echo "$CONN: "
    ssh -q -o "BatchMode yes" $CONN 'true'
    RC=$?

    if [[ $RC -ne 0 ]]
    then
        echo "key auth did not succeed, trying to uploading key:"
        ../ssh-uploadkeys/ssh-uploadkeys $CONN
    else
        echo "key auth ok, no need to upload key"
    fi
done < servers.txt

这个输出:

myusername@the.host.com: 
key auth ok, no need to upload key

servers.txt:

the.host.com
another.host.com
the.ghost.com

我的脚本使用的 ssh-uploadkeys 脚本由 Tero Karvinen 编写:http://terokarvinen.com/ssh-uploadkeys.html

【问题讨论】:

    标签: bash ssh while-loop return-code


    【解决方案1】:

    -n 选项添加到ssh 以防止它从标准输入读取。发生的事情是 ssh 正在使用您的 servers.txt 文件,因此 while 循环在第一行之后终止,因为没有任何内容可供读取。

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2011-10-28
      • 2016-08-03
      • 1970-01-01
      • 2013-04-09
      • 2014-03-07
      • 2021-04-11
      • 2013-08-30
      • 1970-01-01
      相关资源
      最近更新 更多