Skip to content

Instantly share code, notes, and snippets.

@nexus166
Last active March 17, 2020 14:23
Show Gist options
  • Save nexus166/8865614c8e103499b0a2a15fea8e9fcd to your computer and use it in GitHub Desktop.
Save nexus166/8865614c8e103499b0a2a15fea8e9fcd to your computer and use it in GitHub Desktop.
fastest ping scan of the World Wide West
#!/bin/sh -e
SUBNET="${1}";
TIMEOUT="${2:-2}";
pingw8="-w";
uname | grep -i darwin && export pingw8="-W";
ping_cmd() { for octet in $(seq 1 254); do ( ping "$1.$octet" -c 1 "$pingw8" "$TIMEOUT" > /dev/null 2> /dev/null && printf "%s.%s\n" "$1" "$octet" ) & done | sort -V; };
if [ ! -t 0 ]; then
for SUBNET in $(tee | grep -Eo '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+/[0-9]+' | sort -uV | cut -d'.' -f1-3); do
printf '\nTARGET:\t%s.0\n' "$SUBNET";
ping_cmd "$SUBNET";
done;
printf '\n';
else
if [ "$SUBNET+diff" = '+diff' ]; then
printf '\n\tPlease provide IPv4 range(s) to scan, either by piping or as first argument ($1).\n\tNB: you can also specify the max ping timeout with $2 in both modes.\n\n\t$> sping 192.168.1.0/24 5\n\n\t$> ip r | sping - 3\n\n';
exit 1;
else
if [ "$(printf '%s' "$SUBNET" | tr '.' '\n' | wc -l)" > 2 ]; then
SUBNET=$(printf '%s' "$SUBNET" | cut -d'.' -f1-3);
printf '\nTARGET:\t%s.0\n' "$SUBNET";
ping_cmd "$SUBNET";
fi;
fi;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment