2

I'm attempting to write a script to loop over entries in .ssh/authorized_keys and do things with them, namely print their fingerprint and append them to a new place. This is what I have so far:

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

This unfortunately gives me the following error:

/dev/stdin: Stale file handle

I'm running Bash 4.3.11 on Ubuntu 14.04 kernel 3.13.0-24-generic.

On the same kernel running Bash 4.3.8, it works fine. Changing my version of Bash doesn't look to be an option at this point, this is an automated script for something in production.

I found this solution in another question here on StackOverflow but it seems to not work with this later version of Bash.

Community
  • 1
  • 1
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

1 Answers1

1

I think you're on the right track, but you want something like:

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

As opposed to:

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

Note that instead of piping the output of echo, simply feed the file directly into the stdin of the while loop.

This worked for me on:

$ 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>

I'm also on Ubuntu 14.04, but it seems that someone has also maybe seen this problem: https://github.com/docker/docker/issues/2067

A work-around is to write each key to a tempfile, process it, then remove the file.

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
zerodiff
  • 1,690
  • 1
  • 18
  • 23