blackhole.sh 3.0 KB

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