summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGordon GECOS <u@adam>2021-03-03 23:08:58 -0500
committerGordon GECOS <u@adam>2021-03-03 23:34:26 -0500
commitba705c909d98c181e126c952c97e2d56d8c7a24e (patch)
treed046d16d1079236ac3d07a9099ecb69da67c15ab
parent1ab3ef99a3d24dd8470eb56845c66e870fc9560d (diff)
improve penme
-rwxr-xr-xpenme38
1 files changed, 21 insertions, 17 deletions
diff --git a/penme b/penme
index 06356ab..4dbe2e8 100755
--- a/penme
+++ b/penme
@@ -1,36 +1,40 @@
1#!/bin/sh 1#!/bin/sh
2set -e
2if [ "$(id -u)" -ne 0 ] 3if [ "$(id -u)" -ne 0 ]
3then 4then
4 exec sudo -- "$0" "$@" 5 exec sudo -- "$0" "$@"
5fi 6fi
6 7
8vprintf() { [ ! "$VERBOSE" ] || printf "$@" >&2; }
9
10write()
11{
12 printf '%s\n' "$line" >> /root/.ssh/authorized_keys
13 vprintf 'New root authorization: %s\n' "$line"
14 [ "$VERBOSE" ] || printf '%s\n' "$line" >&2
15}
16
7scan_network() 17scan_network()
8{ 18{
9 ip -oneline addr | 19 arp-scan --localnet |
10 sed -ne 's/.*inet \([^ ]*\) .*/\1/p' | 20 while read ip junk
11 while read line
12 do
13 case "$line" in
14 127.*) continue ;;
15 esac
16 arp-scan "$line" 2>/dev/null
17 done | while read ip junk
18 do 21 do
19 case "$ip" in 22 case "$ip" in
20 *.*.*.*) echo $ip ;; 23 *.*.*.*) ;;
21 *) continue ;; 24 *) continue ;;
22 esac 25 esac
26 vprintf 'ARP scan found IP: %s\n' "$ip"
27 ( grep -q " penme host key @ $ip\$" /root/.ssh/authorized_keys ) || echo $ip
23 done 28 done
24} 29}
25 30
26ips=$(scan_network)
27
28mkdir -p /root/.ssh 31mkdir -p /root/.ssh
29touch /root/.ssh/authorized_keys 32[ -e /root/.ssh/authorized_keys ] || touch /root/.ssh/authorized_keys
30 33
31ssh-keyscan - $ips | while read ip key 34ssh-keyscan - $(scan_network) 2>/dev/null |
35while read ip key
32do 36do
33 line=$(printf '%s host key @ %s\n' "$key" "$ip") 37 line=$(printf '%s penme host key @ %s\n' "$key" "$ip")
34 grep -Fx "$line" /root/.ssh/authorized_keys || 38 grep -q -Fx "$line" /root/.ssh/authorized_keys && vprintf 'Already authorized: %s\n' "$line" ||
35 echo "$line" >> /root/.ssh/authorized_keys 39 write "$line" /root/.ssh/authorized_keys
36done 40done