1

In Linux and (Windows through gitbash) if is executed the command:

ssh-keyscan 192.168.1.X | ssh-keygen -lf -

Works as expected, but in macOS (El Capitan) arises

-: No such file or directory
# 192.168.1.X SSH-2.0-OpenSSH .....
# 192.168.1.X SSH-2.0-OpenSSH .....
# 192.168.1.X SSH-2.0-OpenSSH .....

How fix this?

Note The bash version in my macOS is 3.2.57(1)-release and if is executed ssh -V it returns OpenSSH_6.9p1, LibreSSL 2.1.8

7
  • 1
    In macOS Monterey the command above works, so it seems the Openssh version included with El Capitan doesn't support reading the key file from standard input (note that Apple has its own fork of openssh, see opensource.apple.com/releases). As a workaround, you could use process substitution as follows ssh-keygen -lf <(ssh-keyscan 192.168.1.X). Could you try it and report back?
    – jaume
    Oct 17, 2022 at 17:36
  • 1
    Thank you for testing, too bad it didn't work. You will have to save the key file first and then pass it to ssh-keygen, for example: ssh-keyscan 192.168.1.X > /tmp/keyfile; ssh-keygen -lf /tmp/keyfile; rm /tmp/keyfile. You could save that command as a shell function to speed up the process (if you need help with that, let me know and I'll post an answer).
    – jaume
    Oct 18, 2022 at 7:14
  • 1
    I'm glad it works. I prefer to use ; because cmdA && cmdB describes a conditional relationship, that is, cmdA is executed only if cmdB was successful, which could prevent that the temporary file gets deleted in the last step if an error occurs. ;, on the other side, is just a separator so that all commands are executed, so that I know the temporary file will always be deleted. Should I post an answer, or do you prefer to do it?
    – jaume
    Oct 19, 2022 at 14:38
  • 1
    Pls, post your answer - your solution works and of course is valid. So it deserve be marked as an answer. Consider to indicate the ; & difference Oct 19, 2022 at 15:38
  • 1
    Thanks for your feedback, I've added an answer with the one-liner above and also with a shell function, which may be more convenient to use. Check it out.
    – jaume
    Oct 19, 2022 at 16:17

1 Answer 1

1

The Openssh version included with El Capitan doesn't support reading the key file from standard input and according to your tests, doesn't suport process substitution (ssh-keygen -lf <(ssh-keyscan 192.168.1.X) either.

That means that you will have to save the key file first and then pass it to ssh-keygen.

You can use:

  • a function (change skg to a name of your liking):

    function skg() {
        if [[ $1 == "" ]]; then
            echo "$FUNCNAME: Specify an IP address or host name to read an SSH key from";
            return 1;
        else
            ip_address=$1;
        fi;
        tmp_file=$(mktemp);
        ssh-keyscan $ip_address > $tmp_file;
        ssh-keygen -lf $tmp_file
        rm $tmp_file
    }
    

    where:

    • mktemp is a command that creates a temporary file with a unique name that is only readable by you.
    • $FUNCNAME is a shell variable that returns the function name

    Add the function to your .bashrc file, open a Terminal window and execute it as follows:

    skg 192.168.1.X

  • a one-liner:

    ssh-keyscan 192.168.1.X > /tmp/keyfile; ssh-keygen -lf /tmp/keyfile; rm /tmp/keyfile

    (Note that I use ; to separate commands instead of && because the latter is a conditional operator, that is, in a construct like this: commandA && commandB, commandB will only be executed if commandA is successful, which could prevent that the temporary file gets deleted in the last step if an error occurs. ;, on the other hand, is just a separator so that all commands are executed, so the temporary file will always be deleted.)

1
  • 1
    Thanks for the extra function code - BTW the correct order is commandB is only executed if commandA was successful - I suggested the edition Oct 19, 2022 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .