Skip to content

Instantly share code, notes, and snippets.

@rlaager
Forked from dotysan/spamhaus2quagga.sh
Last active November 17, 2023 21:28
Show Gist options
  • Save rlaager/9715387 to your computer and use it in GitHub Desktop.
Save rlaager/9715387 to your computer and use it in GitHub Desktop.
This fork does BGP announcements in a manner intended to be compatible with SpamHaus's BGPf BGP feed. The router you're connecting to should set the next-hop to a null-routed IP (e.g. 192.0.2.1).
hostname HOSTNAME
log syslog
router bgp 65190
bgp router-id IP
neighbor ROUTER remote-as AS
neighbor ROUTER password PASSWORD
neighbor ROUTER ebgp-multihop 255
neighbor ROUTER prefix-list deny-all in
ip prefix-list deny-all seq 10 deny 0.0.0.0/0 le 32
route-map spamhaus-drop permit 10
set community 65190:1000 no-export
route-map spamhaus-edrop permit 10
set community 65190:2000 no-export
line vty
#!/bin/bash -eu
# This really needs bash, because of the regex matching.
# vim: ai ts=4 sts=4 et sw=4
#
# This is heavily based on:
# https://gist.github.com/dotysan/8463112
mkdir -p /var/lib/spamhaus-quagga
cd /var/lib/spamhaus-quagga
wget -q -nv -N http://www.spamhaus.org/drop/{,e}drop.txt
re1='^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+) '
re2='^([-+])([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+)$'
# The if block is just to batch up the output for vtysh.
if true
then
echo config t
echo router bgp 65190
for bl in drop edrop
do
re3="^network ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+) route-map spamhaus-$bl\$"
live=$(mktemp)
vtysh -d bgpd -c 'show run' |
while read l
do
if [[ $l =~ $re3 ]]
then
echo ${BASH_REMATCH[1]}
fi
done > "$live"
list=$(mktemp)
while read l
do
if [[ $l =~ $re1 ]]
then
echo ${BASH_REMATCH[1]}
fi
done < $bl.txt > "$list"
diff -wU0 "$live" "$list" |
while read dl
do
if [[ $dl =~ $re2 ]]
then
case ${BASH_REMATCH[1]} in
+)
echo " network ${BASH_REMATCH[2]} route-map spamhaus-$bl"
;;
-)
echo " no network ${BASH_REMATCH[2]}"
;;
esac
fi
done
rm "$live" "$list"
done
echo end
echo write mem
fi |
vtysh -d bgpd > /dev/null

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment