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.

79 lines
2.3 KiB

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