-
-
Save kei-sato/b5ecda9c6c03a9bf7880 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# reverse arp | |
USAGE=' | |
Example: | |
rarp 00:0a:95:9d:68:16 | |
=> 192.168.0.253 | |
' | |
abort() { | |
[[ $# -gt 0 ]] && echo "$@" | |
echo "$USAGE" 1>&2 | |
exit 1 | |
} | |
IS_VERBOSE=false | |
while getopts hxv option; do | |
case $option in | |
x) set -x;; | |
v) IS_VERBOSE=true;; | |
h|\?) abort;; | |
esac | |
done | |
[[ $OPTIND -gt 1 ]] && shift $((OPTIND - 1)) | |
[[ $# -lt 1 ]] && abort "MAC address not found" | |
[[ $(echo "$1" | tr ":" "\n" | wc -l) -ne 6 ]] && abort "invalid MAC address: $1" | |
MAC=$(sed -e "s/^0//" -e "s/:0/:/g" <<< "$1") | |
if OUTPUT=$(arp -an | grep "$MAC"); then | |
if $IS_VERBOSE; then | |
echo "$OUTPUT" | |
else | |
awk '{print $2}' <<< "$OUTPUT" | tr -d "()" | |
fi | |
else | |
echo "not found ip address of $MAC" 2>&1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment