Skip to content

Instantly share code, notes, and snippets.

@Jcpetrucci
Created October 16, 2018 11:02
Show Gist options
  • Save Jcpetrucci/65a66bb1e2cb6e66697675db7083a932 to your computer and use it in GitHub Desktop.
Save Jcpetrucci/65a66bb1e2cb6e66697675db7083a932 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Create: 2014-03-12 John C. Petrucci
# Modify: 2014-03-13 John C. Petrucci
# http://johncpetrucci.com
# Purpose: See usage()
# Usage: See usage()
usage (){
cat <<EOF
$(basename $0) - Converts hexadecimal to IP addresses.
Accepts a file as first argument or as stdin.
Examples:
./$(basename $0) file.log
cat file1.log file2.log | ./$(basename $0)
EOF
}
# one arg && is file OR no args OR print error message, usage, and quit
( [[ "$#" == 1 && -f "$1" ]] || [[ "$#" == 0 ]] ) || { printf '%s: %s\n' "File not valid" "${1:-(stdin)}" >&2; usage >&2; exit 1; }
while IFS= read -r line
do
declare -a iparray
iparray=("${iparray[@]}" "$((grep -Eoi "([a-f0-9]{8})" <<<$line | fold -w2) | while read a; read b; read c; read d; do printf ' [%d.%d.%d.%d]' "0x$a" "0x$b" "0x$c" "0x$d"; done)" )
printf "%s%s\n" \
"$(cat <<<$line)" \
"${iparray[*]}"
unset iparray
done < "${1:-/proc/self/fd/0}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment