Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aachyee/c94938e4dc635e737ca380fa64c9be37 to your computer and use it in GitHub Desktop.

Select an option

Save aachyee/c94938e4dc635e737ca380fa64c9be37 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