Debian Codegraph @

pkg/systemd/overview-cards/ssh-list-host-keys.sh

raw
1 #!/bin/sh
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 set -eu
5 port="22"
6 host="localhost"
7
8 parse_addr() {
9 if [ -n "$1" ]; then
10 case "$1" in
11 *:*)
12 t="$1"
13 p=${t##*:}
14 h=${t%:*}
15 ;;
16 *)
17 p="$1"
18 h="localhost"
19 ;;
20 esac
21
22 # checks that port is an integer
23 if [ "$p" -eq "$p" ] 2>/dev/null; then
24 port="$p"
25 host="$h"
26 if [ "$host" = "[::]" ]; then
27 host="::"
28 fi
29 fi
30 fi
31 }
32
33 # Try to find where sshd might be listening
34
35 # Check sshd_config, only works for root
36 config=$(sshd -T | grep "listenaddress " | cut -d' ' -f2-)
37 echo "$config" | while IFS='\n' read line; do
38 parse_addr "$line"
39 done
40
41 # Check with systemd
42 systemd=$(systemctl show --property=Listen sshd.socket || systemctl show --property=Listen ssh.socket || true)
43 echo "$systemd" | while IFS='=' read -r name value; do
44 if [ "$name" = "ListenStream" ]; then
45 parse_addr "$value"
46 fi
47 done
48
49 keys=$(ssh-keyscan -t ecdsa,ed25519,rsa -p "$port" "$host" || true)
50 if [ -n "$keys" ]; then
51 # Some versions of ssh-keygen don't support -f reading from stdin
52 # so write a tmpfile
53 tmp=$(mktemp)
54 echo "$keys" > "$tmp"
55
56 # Not all ssh-keygen version support -E in those cases just output the default
57 (ssh-keygen -l -f "$tmp" -E md5 && ssh-keygen -l -f "$tmp" -E sha256) || ssh-keygen -l -f "$tmp"
58 rm "$tmp"
59 fi