You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

4 years ago
  1. #!/bin/sh
  2. # TODO :
  3. # - Add detection of VPN Interface to remove error message
  4. DEBUG() {
  5. [ "$_DEBUG" = "on" ] && $@
  6. }
  7. # Define VPN interface name
  8. VPNIF="vpnaltinea"
  9. if ! [ -x "$(command -v sendEmail)" ]; then
  10. echo "sendEmail could not be found, please install it with :"
  11. echo "apt install sendemail"
  12. exit 99;
  13. fi
  14. if ! [ -x "$(command -v jq)" ]; then
  15. echo "jq could not be found, please install it with :"
  16. echo "apt install jq"
  17. exit 99;
  18. fi
  19. # Get public IP addresses
  20. EXTIP4=`curl -s https://checkipv4.altinea.fr/getIP.php`
  21. EXTIP6=`curl -s https://checkipv6.altinea.fr/getIP.php`
  22. # Testing if returned IPs are empty and setting
  23. # a dummy value for later comparison
  24. if [ -z "$EXTIP4" ]; then
  25. EXTIP4='dummy'
  26. fi
  27. if [ -z "$EXTIP6" ]; then
  28. EXTIP6='dummy'
  29. fi
  30. # Check if found IPv4 address is configured on this server
  31. if [ $(hostname -I |grep -c $EXTIP4) -ne 0 ]; then
  32. IP4=$EXTIP4
  33. else
  34. # Let see if we can find an ip address on the configured VPN interface
  35. DEBUG echo "Couldn't find a public IPv4 address on this host"
  36. DEBUG echo "Let see if we find a $VPNIF wireguard interface"
  37. VPNIP4=`ip -j addr show dev $VPNIF | jq -r '.[0].addr_info | map(select(.family == "inet"))[0].local'`
  38. if [ -z "$VPNIP4" ]; then
  39. DEBUG echo "Can't find an IPv4 address on $VPNIF interface"
  40. else
  41. DEBUG echo "Found VPN IPv4 : $VPNIP4. Using it for monitoring purposes"
  42. IP4=$VPNIP4
  43. fi
  44. fi
  45. # Same routine for IPv6
  46. if [ $(hostname -I |grep -c $EXTIP6) -ne 0 ]; then
  47. IP6=$EXTIP6
  48. else
  49. DEBUG echo "Couldn't find a public IPv6 address on this host"
  50. DEBUG echo "Let see if we find an IPv6 on $VPNIF interface"
  51. VPNIP6=`ip -j addr show dev $VPNIF | jq -r '.[0].addr_info | map(select(.family == "inet6"))[0].local'`
  52. if [ -z "$VPNIP6" ]; then
  53. DEBUG echo "Can't find an IPv6 address on $VPNIF interface"
  54. else
  55. DEBUG echo "Found VPN IPv6 : $VPNIP6. Using it for monitoring purposes"
  56. IP6=$VPNIP6
  57. fi
  58. fi
  59. # Generate first part on the command
  60. COMMAND="sendEmail -f noc@altinea.fr -u Nouvelle demande de monitoring -t support@altinea.fr -m Nouvelle IP à monitorer :\n"`hostname -f`"\n"
  61. # Add IPv4 if found
  62. if [ -z "$IP4" ]; then
  63. DEBUG echo "Sorry we're unable to find a routable IPv4 address, sorry !"
  64. else
  65. COMMAND="$COMMAND IPv4 : $IP4\n"
  66. fi
  67. # Add IPv6 if found
  68. if [ -z "$IP6" ]; then
  69. DEBUG echo "Sorry we're unable to find a routable IPv6 address, sorry !"
  70. else
  71. COMMAND="$COMMAND IPv6 : $IP6\n"
  72. fi
  73. COMMAND="$COMMAND -o tls=no -o message-charset=UTF-8 -s zpush.altinea.fr"
  74. # And run the request by mail
  75. if [ "$_DRYRUN" = "on" ]; then
  76. echo $COMMAND
  77. else
  78. $COMMAND
  79. fi