aboutsummaryrefslogtreecommitdiff
path: root/bin/sshi
blob: 80e00a10f625b62f6ef4cea0a88a73f243caadae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
# Print some human-readable information from SSH_CONNECTION

# Check we have an SSH_CONNECTION variable
if [ -z "$SSH_CONNECTION" ] ; then
    printf >&2 'sshi: SSH_CONNECTION appears empty\n'
    exit 1
fi

# Print the two variables into a compound command so we can `read` them
printf '%s\n' "$SSH_CONNECTION" "${SSH_TTY:-unknown}" |
{
    # Read connection details from first line
    read -r ci cp si sp

    # Read TTY from second line
    read -r tty

    # Try to resolve the client and server IPs
    ch=$(dig -x "$ci" +short 2>/dev/null | sed 's/\.$//;1q')
    sh=$(dig -x "$si" +short 2>/dev/null | sed 's/\.$//;1q')

    # Print the results in a human-readable format
    printf "%s:%u -> %s:%u (%s)\n" \
        "${ch:-"$ci"}" "$cp" \
        "${sh:-"$si"}" "$sp" \
        "$tty"
}