#!/bin/sh if [ "$(id -u)" -ne 0 ] then exec sudo -- "$0" "$@" fi scan_network() { ip -oneline addr | sed -ne 's/.*inet \([^ ]*\) .*/\1/p' | while read line do case "$line" in 127.*) continue ;; esac arp-scan "$line" 2>/dev/null done | while read ip junk do case "$ip" in *.*.*.*) echo $ip ;; *) continue ;; esac done } ips=$(scan_network) mkdir -p /root/.ssh touch /root/.ssh/authorized_keys ssh-keyscan - $ips | while read ip key do line=$(printf '%s host key @ %s\n' "$key" "$ip") grep -Fx "$line" /root/.ssh/authorized_keys || echo "$line" >> /root/.ssh/authorized_keys done