Skip to content

Instantly share code, notes, and snippets.

@ixs
Last active August 4, 2021 12:06
Show Gist options
  • Save ixs/24545ad8715a5c3915e0585418635fed to your computer and use it in GitHub Desktop.
Save ixs/24545ad8715a5c3915e0585418635fed to your computer and use it in GitHub Desktop.
IP Finder als kleine Programmierübung
#!/bin/bash
set -euo pipefail
# IP Finder als kleine Programmierübung
NAMED=/var/named/data
NETZ="${1:-}"
if [ -z "$NETZ" ]; then
echo "$0 [netz/prefix]"
exit 1
fi
function ip2long() {
IP="$1"
echo $(python -c 'import socket; import struct; print(struct.unpack("!L", socket.inet_aton("'$IP'")))[0]')
}
function long2ip() {
LONG="$1"
echo $(python -c 'import socket; import struct; print(socket.inet_ntoa(struct.pack("!L", '$LONG')))')
}
eval $(ipcalc -bn $NETZ)
START=$(ip2long $NETWORK)
END=$(ip2long $BROADCAST)
IP=$((START+1))
while [ $IP -lt $((END-1)) ]; do
IPt=$(long2ip $IP)
regexp=$(echo $IPt | sed 's/\./\\\./g;')
grep -R -E "$regexp\$" "$NAMED" || true
IP=$(($IP + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment