【问题标题】:Stale file descriptor with /dev/stdin/dev/stdin 的陈旧文件描述符
【发布时间】:2014-08-25 05:39:54
【问题描述】:

我正在尝试编写一个脚本来循环遍历.ssh/authorized_keys 中的条目并对它们进行处理,即打印它们的指纹并将它们附加到一个新位置。这是我目前所拥有的:

echo "$SSH_KEYS" | while read key ; do 
    ssh-keygen -lf /dev/stdin <<< "$key"
    echo "$key" >> newplace
done

不幸的是,这给了我以下错误:

/dev/stdin: Stale file handle

我在 Ubuntu 14.04 内核 3.13.0-24-generic 上运行 Bash 4.3.11。

在运行 Bash 4.3.8 的同一个内核上,它运行良好。在这一点上,更改我的 Bash 版本似乎不是一个选项,这是一个用于生产中的自动脚本。

我在another question here on StackOverflow 中找到了这个解决方案,但它似乎不适用于这个更高版本的 Bash。

【问题讨论】:

    标签: bash


    【解决方案1】:

    我认为你在正确的轨道上,但你想要这样的东西:

    while read key; do
        ssh-keygen -lf /dev/stdin <<< "$key"
        echo "$key" >> newplace
    done < .ssh/authorized_keys
    

    相对于:

    echo "$SSH_KEYS" | while read key ; do 
        ssh-keygen -lf /dev/stdin <<< "$key"
        echo "$key" >> newplace
    done 
    

    请注意,不要将echo 的输出通过管道传输,只需将文件直接输入while 循环的stdin

    这对我有用:

    $ bash --version
    GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    

    我也在使用 Ubuntu 14.04,但似乎有人也可能看到过这个问题:https://github.com/docker/docker/issues/2067

    解决方法是将每个密钥写入临时文件,对其进行处理,然后删除该文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 2014-04-25
      • 1970-01-01
      相关资源
      最近更新 更多