Thursday, May 16, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 628  / 1 Year ago, thu, january 12, 2023, 1:52:07

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?


More From » command-line

 Answers
2

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

[#920] Thursday, January 12, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
memorrappin

Total Points: 325
Total Questions: 122
Total Answers: 100

Location: Armenia
Member since Sat, Sep 12, 2020
4 Years ago
memorrappin questions
Tue, Aug 23, 22, 13:34, 2 Years ago
Fri, Aug 12, 22, 14:45, 2 Years ago
Fri, Sep 9, 22, 05:27, 2 Years ago
Sun, Apr 3, 22, 16:14, 2 Years ago
;