Score:2

Filtering content of command with grep

kn flag

I am trying to filter out a the output of ssh-keyscan. The goal of this is to filter the output so I can use it in my python code to identify hosts connected to my VPN. Normally I would use grep to filter, one of my greps is filtering properly, but the other is not. The first grep is working to get just the ed25519 ID, but not sure why I am getting the SSH-2.0... lines also. The command I ran along with the output is below:

user@host# ssh-keyscan 10.xx.xx.xx | grep ed25519 | grep -v "#"
  # 10.xx.xx.xx:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3
  # 10.xx.xx.xx:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3
  # 10.xx.xx.xx:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3
  # 10.xx.xx.xx:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3
  # 10.xx.xx.xx:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3
  10.xx.xx.xx ssh-ed25519 <host key>

I need it to print only the ed25519 line. I have also tried AWK to filter, but still not getting the output I need.

Any idea how to filter only the ed25519 line?

hr flag
The `#` lines are likely going to stderr not stdout - try `ssh-keyscan 10.xx.xx.xx 2>&1 | grep ed25519 | grep -v "#"`
Tim R avatar
kn flag
You were absolutely correct on that. If you want to put it in as answer so I can give you credit for it as the solution. Thank you so much. Didn't need the second grep with that either since the 2>&1 corrected it
hr flag
OK done - I also made some suggestions to eliminate the grep(s)
Score:3
hr flag

The lines starting with # are going to stderr (standard error) rather than standard output (stdout). To get grep -v to exclude them, you need to redirect stderr to stdout:

ssh-keyscan 10.xx.xx.xx 2>&1 | grep ed25519 | grep -v "#"

Note that you can eliminate the first grep by adding a type filter to the ssh-keyscan command itself:

ssh-keyscan -t ed25519 10.xx.xx.xx 2>&1 | grep -v "#"

If you prefer, you could redirect all error to null instead of filtering it:

ssh-keyscan -t ed25519 10.xx.xx.xx 2>/dev/null
Score:0
us flag

Well if you know the output is going to include "ssh-ed25519" you should grep on that.

Or pipe to that.

Or if you know it will be the last line of the output, pipe to the appropriate "tail" command.

Have you looked at the sed command?

in flag
Welcome to AskUbuntu. When suggesting a person do A or B to fix a problem, it's usually beneficial to include examples of the solution. Fortunately, these already exist with the accepted answer
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.