freedombone-utils-network 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Network functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # If the system is on an IPv6 network
  31. IPV6_NETWORK='2001:470:26:307'
  32. # Destinations used to get the local IP address of this system
  33. # Google ipv6 DNS 2001:4860:4860::8888
  34. # OpenDNS ipv6 DNS 2620:0:ccc::2
  35. IPV4_ADDRESS_TEST_DESTINATION='85.214.73.63'
  36. IPV6_ADDRESS_TEST_DESTINATION='2620:0:ccc::2'
  37. EXTERNAL_IP_LOOKUP_URL='ifcfg.me'
  38. # The static IP address of the system within the local network
  39. # By default the IP address is dynamic within your LAN
  40. LOCAL_NETWORK_STATIC_IP_ADDRESS=
  41. # IP address of the router (gateway)
  42. ROUTER_IP_ADDRESS="192.168.1.254"
  43. MESH_INSTALL_DIR=/var/lib
  44. function install_static_network {
  45. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  46. return
  47. fi
  48. if [[ $INSTALLING_ON_BBB == "yes" ]]; then
  49. return
  50. fi
  51. echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
  52. echo '# and how to activate them. For more information, see interfaces(5).' >> /etc/network/interfaces
  53. echo 'source /etc/network/interfaces.d/*' >> /etc/network/interfaces
  54. if [ ! $LOCAL_NETWORK_STATIC_IP_ADDRESS ]; then
  55. { echo 'auto eth0';
  56. echo 'iface eth0 inet dhcp'; } > /etc/network/interfaces.d/dynamic
  57. if [ -f /etc/network/interfaces.d/static ]; then
  58. rm /etc/network/interfaces.d/static
  59. fi
  60. mark_completed "${FUNCNAME[0]}"
  61. return
  62. fi
  63. { echo 'auto eth0';
  64. echo 'iface eth0 inet static';
  65. echo " address $LOCAL_NETWORK_STATIC_IP_ADDRESS";
  66. echo ' netmask 255.255.255.0';
  67. echo " gateway $ROUTER_IP_ADDRESS"; } > /etc/network/interfaces.d/static
  68. if [ -f /etc/network/interfaces.d/dynamic ]; then
  69. rm /etc/network/interfaces.d/dynamic
  70. fi
  71. }
  72. function get_external_ipv4_address {
  73. nslookup . $EXTERNAL_IP_LOOKUP_URL | grep Address | tail -n 1 | awk -F ' ' '{print $2}'
  74. }
  75. function get_ipv4_address {
  76. IPv4dev=$(ip route get $IPV4_ADDRESS_TEST_DESTINATION | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
  77. ip -o -f inet addr show dev "$IPv4dev" | awk '{print $4}' | awk 'END {print}' | awk -F '/' '{print $1}'
  78. }
  79. function get_ipv6_address {
  80. retval=$(ip -6 route get $IPV6_ADDRESS_TEST_DESTINATION 2> /dev/null)
  81. echo "$retval" | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }'
  82. }
  83. function update_external_ip {
  84. ip_update_script=/usr/bin/externalipupdate
  85. { echo '#!/bin/bash';
  86. echo "existing_ip=\$(cat $CONFIGURATION_FILE | grep \"EXTERNAL_IPV4_ADDRESS=\" | head -n 1 | awk -F '=' '{print \$2}')'";
  87. echo "curr_ip=\$(nslookup . $EXTERNAL_IP_LOOKUP_URL | grep Address | tail -n 1 | awk -F ' ' '{print \$2}')";
  88. echo "if [[ \"\$curr_ip\" != \"\$existing_ip\" ]]; then";
  89. echo " sed -i \"s|EXTERNAL_IPV4_ADDRESS=.*|EXTERNAL_IPV4_ADDRESS=\${curr_ip}|g\" $CONFIGURATION_FILE";
  90. echo " echo \"\$(date)\" >> ~/${PROJECT_NAME}-external-ip-changes.txt";
  91. echo 'fi'; } > $ip_update_script
  92. cron_add_mins 10 $ip_update_script
  93. }
  94. # NOTE: deliberately no exit 0