Skip to content

Instantly share code, notes, and snippets.

@nihilismus
Created May 16, 2015 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nihilismus/8dc8deed43486d6c7979 to your computer and use it in GitHub Desktop.
Save nihilismus/8dc8deed43486d6c7979 to your computer and use it in GitHub Desktop.
Converts http://www.nirsoft.net/countryip/mx.csv into a rules of tcpdump
#!/bin/sh
# Converts http://www.nirsoft.net/countryip/mx.csv
# into a rules of tcpdump
#
# Copyright © 2015 Antonio Hernández Blas <hba.nihilismus@gmail.com>
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
function generate_network_for() {
ipcalc ${1} - ${2} | tail -1
}
function get_ip_address() {
echo ${1} | cut -d ',' -f ${2} | xargs
}
function print_list() {
cat mx.csv | while read line; do
from_ip_address=$(get_ip_address ${line} 1)
to_ip_address=$(get_ip_address ${line} 2)
if [ -z ${from_ip_address} ]; then
continue
fi
generate_network_for ${from_ip_address} ${to_ip_address}
done
}
function print_tcpdump_list() {
networks="$(print_list)"
is_the_first=true
echo "("
for network in ${networks}; do
if ${is_the_first}; then
echo " srt net ${network}"
is_the_first=false
else
echo " ${1} src net ${network}"
fi
done
echo ")"
}
case $# in
1)
print_tcpdump_list "${@}"
;;
*)
echo "Error, you need to indicate the logical operation for rules"
echo "mxcsv2tcpdump.sh or|and"
exit 1
;;
esac
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment