blackhole.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/local/bin/bash
  2. #
  3. # Author:: Sebastien Badia (<seb@sebian.fr>)
  4. # Date:: 2014-02-13 04:01:56 +0100
  5. if [[ "x${DEBUG}" == "x1" ]]; then
  6. set -e
  7. set -x
  8. fi
  9. hostname=$(hostname -s)
  10. case $hostname in
  11. "zoulou")
  12. BGPNEIGHBOR="212.85.148.109"
  13. ;;
  14. "yankee")
  15. BGPNEIGHBOR="80.231.79.69"
  16. ;;
  17. "x-ray")
  18. BGPNEIGHBOR="79.143.245.137"
  19. ;;
  20. "grimoire")
  21. BGPNEIGHBOR="1.1.1.1"
  22. ;;
  23. *)
  24. echo "Unknow router"
  25. exit 1
  26. esac
  27. show_usage() {
  28. cat <<EOF
  29. Usage: $0 [OPTION]
  30. Blackhole script
  31. -a [add|del|list] Action à effectuer.
  32. -i [a.b.c.d/n] Petite victime.
  33. -h (help) Affiche cette aide.
  34. EOF
  35. exit 1
  36. }
  37. while getopts "a:i:h" opt; do
  38. case $opt in
  39. a)
  40. case $OPTARG in
  41. "add")
  42. action="add"
  43. ;;
  44. "del")
  45. action="del"
  46. ;;
  47. "list")
  48. action="list"
  49. ;;
  50. *)
  51. echo "Not a action (add/del/list)"
  52. ;;
  53. esac
  54. ;;
  55. i)
  56. case $OPTARG in
  57. *.*.*.*/* )
  58. net=$OPTARG
  59. ;;
  60. * )
  61. echo "Not a valid ip (a.b.c.d/n), ($OPTARG)"
  62. exit 1
  63. esac
  64. ;;
  65. h)
  66. show_usage
  67. exit 2
  68. ;;
  69. *)
  70. echo "Bad params"
  71. exit 1
  72. ;;
  73. esac
  74. done
  75. if [[ "$action" == "add" ]]; then
  76. mask=`echo $net | cut -d / -f 2`
  77. if [ $mask -le 24 -o $mask -gt 32 ]; then
  78. echo To blackhole a whole /$mask is not reasonable
  79. exit 1
  80. fi
  81. echo "Adding $net to blackhole:"
  82. # then to the router blackhole :
  83. vtysh -d bgpd -c "conf t" -c "router bgp 20766" -c "network $net route-map blackhole"
  84. # then clear the out announce to our transit :
  85. vtysh -d bgpd -c "clear ip bgp $BGPNEIGHBOR soft out"
  86. #vtysh -d zebra -c "conf t" -c "ip route $net 127.0.0.1 blackhole"
  87. route add $net 127.0.0.1 -blackhole
  88. elif [[ "$action" == "del" ]]; then
  89. echo "Removing $net from blackhole:"
  90. # then to the router blackhole :
  91. vtysh -d bgpd -c "conf t" -c "router bgp 20766" -c "no network $net"
  92. # then clear the out announce to our transit :
  93. vtysh -d bgpd -c "clear ip bgp $BGPNEIGHBOR soft out"
  94. #vtysh -d zebra -c "conf t" -c "no ip route $net 127.0.0.1 blackhole"
  95. route del $net 127.0.0.1 -blackhole
  96. elif [[ "$action" == "list" ]]; then
  97. echo Those networks are black-holed by BGPD:
  98. vtysh -d bgpd -c 'show run' | grep 'network .* route-map blackhole' | awk '{print $2}'
  99. echo Those networks are black-holed by ZEBRA via kernel:
  100. vtysh -c 'sh ip route kernel' | grep 'lo0, bh' | awk '{print $2}'
  101. vtysh -c 'sh ip route static' | grep 'lo0, bh' | awk '{print $2}'
  102. else
  103. echo "Action problem"
  104. exit 1
  105. fi