Skip to content

Instantly share code, notes, and snippets.

@flashvoid
Created March 6, 2017 22:36
Show Gist options
  • Save flashvoid/0373f7289bb04d9d73f9b8871a1fa3bf to your computer and use it in GitHub Desktop.
Save flashvoid/0373f7289bb04d9d73f9b8871a1fa3bf to your computer and use it in GitHub Desktop.
tw-ip-source-conv1
#!/bin/bash
ip2dec () {
local a b c d ip=$@
IFS=. read -r a b c d <<< "$ip"
printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
}
die () {
echo $1
exit $2
}
chop () {
echo $@ | sed "s/\"//g" | cut -d "." -f 6
}
test -r $1 || die "No input file" 1
while read line; do
words=$(wc -w <<<$line)
if (( $words == 5 )); then
read -r a b c d e <<< $line
ipSource=$(ip2dec $c)
ipDest=$(ip2dec $d)
dc=$(chop $e)
echo "$ipSource,$ipDest,*,$dc,*,*,*"
elif (( $words == 3 )); then
read -r a b dc <<< $line
ipSource=$(ip2dec $a)
ipDest=$(ip2dec $b)
echo "$ipSource,$ipDest,*,$dc,*,*,*"
else
die "Can't recognize input file format"
fi
done < $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment