freedombone-utils-network 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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-2016 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. # The static IP address of the system within the local network
  33. # By default the IP address is dynamic within your LAN
  34. LOCAL_NETWORK_STATIC_IP_ADDRESS=
  35. # IP address of the router (gateway)
  36. ROUTER_IP_ADDRESS="192.168.1.254"
  37. MESH_INSTALL_DIR=/var/lib
  38. function install_static_network {
  39. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  40. return
  41. fi
  42. if [[ $INSTALLING_ON_BBB == "yes" ]]; then
  43. return
  44. fi
  45. if [ ! $LOCAL_NETWORK_STATIC_IP_ADDRESS ]; then
  46. return
  47. fi
  48. echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
  49. echo '# and how to activate them. For more information, see interfaces(5).' >> /etc/network/interfaces
  50. echo 'source /etc/network/interfaces.d/*' >> /etc/network/interfaces
  51. echo 'auto eth0' > /etc/network/interfaces.d/static
  52. echo 'iface eth0 inet static' >> /etc/network/interfaces.d/static
  53. echo " address $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/network/interfaces.d/static
  54. echo ' netmask 255.255.255.0' >> /etc/network/interfaces.d/static
  55. echo " gateway $ROUTER_IP_ADDRESS" >> /etc/network/interfaces.d/static
  56. mark_completed $FUNCNAME
  57. }
  58. function get_ipv4_address {
  59. IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
  60. echo $(ip -o -f inet addr show dev "$IPv4dev" | awk '{print $4}' | awk 'END {print}' | awk -F '/' '{print $1}')
  61. }
  62. function get_ipv6_address {
  63. echo $(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }')
  64. }
  65. # NOTE: deliberately no exit 0