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.

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