blackhole.sh 3.0 KB

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