freedombone/src/freedombone-utils-network

102 lines
3.7 KiB
Plaintext
Raw Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Network functions
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# If the system is on an IPv6 network
IPV6_NETWORK='2001:470:26:307'
# Destinations used to get the local IP address of this system
# Google ipv6 DNS 2001:4860:4860::8888
# OpenDNS ipv6 DNS 2620:0:ccc::2
IPV4_ADDRESS_TEST_DESTINATION='85.214.73.63'
IPV6_ADDRESS_TEST_DESTINATION='2620:0:ccc::2'
EXTERNAL_IP_LOOKUP_URL='ifcfg.me'
2016-07-03 17:13:34 +02:00
# The static IP address of the system within the local network
# By default the IP address is dynamic within your LAN
LOCAL_NETWORK_STATIC_IP_ADDRESS=
# IP address of the router (gateway)
ROUTER_IP_ADDRESS="192.168.1.254"
MESH_INSTALL_DIR=/var/lib
function install_static_network {
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2016-10-16 20:50:56 +02:00
return
fi
if [[ $INSTALLING_ON_BBB == "yes" ]]; then
return
fi
if [ ! $LOCAL_NETWORK_STATIC_IP_ADDRESS ]; then
return
fi
2016-07-03 17:13:34 +02:00
echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
echo '# and how to activate them. For more information, see interfaces(5).' >> /etc/network/interfaces
echo 'source /etc/network/interfaces.d/*' >> /etc/network/interfaces
2018-02-26 14:50:40 +01:00
{ echo 'auto eth0';
echo 'iface eth0 inet static';
echo " address $LOCAL_NETWORK_STATIC_IP_ADDRESS";
echo ' netmask 255.255.255.0';
echo " gateway $ROUTER_IP_ADDRESS"; } > /etc/network/interfaces.d/static
2016-07-03 17:13:34 +02:00
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2016-07-03 17:13:34 +02:00
}
function get_external_ipv4_address {
nslookup . $EXTERNAL_IP_LOOKUP_URL | grep Address | tail -n 1 | awk -F ' ' '{print $2}'
}
function get_ipv4_address {
IPv4dev=$(ip route get $IPV4_ADDRESS_TEST_DESTINATION | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
2018-02-26 14:50:40 +01:00
ip -o -f inet addr show dev "$IPv4dev" | awk '{print $4}' | awk 'END {print}' | awk -F '/' '{print $1}'
}
function get_ipv6_address {
retval=$(ip -6 route get $IPV6_ADDRESS_TEST_DESTINATION 2> /dev/null)
2018-02-26 14:50:40 +01:00
echo "$retval" | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }'
}
2017-09-25 18:18:59 +02:00
function update_external_ip {
ip_update_script=/usr/bin/externalipupdate
2018-02-26 14:50:40 +01:00
{ echo '#!/bin/bash';
echo "existing_ip=\$(cat $CONFIGURATION_FILE | grep \"EXTERNAL_IPV4_ADDRESS=\" | head -n 1 | awk -F '=' '{print \$2}')'";
echo "curr_ip=\$(nslookup . $EXTERNAL_IP_LOOKUP_URL | grep Address | tail -n 1 | awk -F ' ' '{print \$2}')";
echo "if [[ \"\$curr_ip\" != \"\$existing_ip\" ]]; then";
echo " sed -i \"s|EXTERNAL_IPV4_ADDRESS=.*|EXTERNAL_IPV4_ADDRESS=\${curr_ip}|g\" $CONFIGURATION_FILE";
echo " echo \"\$(date)\" >> ~/${PROJECT_NAME}-external-ip-changes.txt";
echo 'fi'; } > $ip_update_script
2017-09-25 18:18:59 +02:00
cron_add_mins 10 $ip_update_script
}
2016-07-03 17:13:34 +02:00
# NOTE: deliberately no exit 0