#!/bin/bash ##################################################################################################### # DinGo script "ftp-upgrade-networking.sh" # # Applies to DinGo Pi # # This script should be placed in /opt/GoIoT/DinGo/bin # # The script must be made executable like this: # chmod +x /opt/GoIoT/DinGo/bin/ftp-upgrade-networking.sh # # The purpose of this script, is to install or upgrade the networking setup for the DINGO-stack. # # Note: The networking setup for the DINGO-stack has some dependencies that should be manually solved. # This script will not try to install these dependencies. The dependencies are these: wvdial, hostapd, # udhcpd, iptables, wireless-tools, dnsutils, tzdata, firmware-ralink, usb-modeswitch, ppp, wpasupplicant, # iw, nfs-common, adjtimex and bridge-utils. # # This script takes one parameter. If it equals "install", then the script will try to install # the networking setup. If the parameter is empty or something else, then the networking setup will be upgraded. # If using install-mode, when the networking setup is already installed, then the script will switch # into upgrade-mode automatically. # # When installing or upgrading, this script will download files from a remote site. # The remote directory structure starts with a platform e.g. RPI for Raspberry Pi. It then has a # directory called "networking", which contains the files that should be copied onto the local filesystem. # Under the "networking", we have two directories: one called "configfiles", which contains files that # should only be copied once to the filesystem, and one called "exec", which basicly contains all # the other files. # # Usage: This script should be called when installing or upgrading the networking setup for the DINGO-stack. # # Debug: # # Written by Danjal S. Adlersson @ Go-IoT # Created 12. Mar. 2018 # Copyright (c) Go-IoT ##################################################################################################### # Causes the shell to exit if any subcommand or pipeline returns a non-zero status. set -e # FTP settings for the remote downloading username="dingo" password="dingdong" server="82.221.35.195" #username="ftp" #password="ftp" #server="192.168.1.22" platform="RPI" # Color settings for echo messages blue='\e[1;36m' # Blue color green='\e[1;32m' # Green color yellow='\e[1;33m' # Yellow color red='\e[1;31m' # Red color NC='\e[0m' # No color # Function definitinos. These functions are called in the script function GetFiles { # wget # -m (mirror) uses recursion and timestamping. It will not delete any files. # -nv turns off verboseness, without being quit. # -nH does not create host directories. # --cut-dirs ignores a number of parent remote directories. echo -e "${blue}GoIoT: Get executables and other...${NC}" # etc cd /etc sudo wget -m -nv -nH --cut-dirs=4 ftp://$username:$password@$server/$platform/networking/exec/etc # opt cd /opt sudo wget -m -nv -nH --cut-dirs=4 ftp://$username:$password@$server/$platform/networking/exec/opt # usr cd /usr sudo wget -m -nv -nH --cut-dirs=4 ftp://$username:$password@$server/$platform/networking/exec/usr } function SetPermissions { echo -e "${blue}GoIoT: Set permissions...${NC}" # Executables sudo chmod +x /etc/ppp/ip-down.d/rn_ppp_down sudo chmod +x /etc/ppp/ip-up.d/rn_ppp_up sudo chmod +x /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/rc.local sudo chmod +x /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/init.d/hostapd sudo chmod +x /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/init.d/udhcpd } # MAIN SCRIPT STARTS HERE # Set default mode, install or upgrade mode="upgrade" # Check "mode" parameter if [ "$1" = "" ]; then # Parameter is empty, so we use default value echo -e "${blue}GoIoT: No parameter. Using default mode.${NC}" else if [ "$1" = "install" ]; then mode=$1 # Check if networking setup is already installed if [ -d "/opt/GoIoT/apt/dingo-stack-networking_1.0-1" ]; then # Directory exists - so this is a upgrade echo -e "${blue}GoIoT: Switching to upgrade mode.${NC}" mode="upgrade" fi fi fi # -- INSTALL --- if [ "$mode" = "install" ]; then echo -e "${blue}GoIoT: Start installing...${NC}" # 1. RUN PRE-SCRIPTS # When we install for the first time, we need to take backup of # certain files before we copy our files to the rootfs. if [ -f /etc/default/hostapd ]; then cp -p /etc/default/hostapd /etc/default/hostapd.`date +%Y%m%d%H%M%S` fi if [ -f /etc/hostapd/hostapd.conf ]; then cp -p /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.`date +%Y%m%d%H%M%S` fi if [ ! -d /opt/GoIoT/backup ]; then mkdir -p /opt/GoIoT/backup fi if [ -f /etc/init.d/hostapd ]; then cp -p /etc/init.d/hostapd /opt/GoIoT/backup/hostapd.`date +%Y%m%d%H%M%S` fi if [ -f /etc/init.d/udhcpd ]; then cp -p /etc/init.d/udhcpd /opt/GoIoT/backup/udhcpd.`date +%Y%m%d%H%M%S` fi if [ -f /etc/network/interfaces ]; then cp -p /etc/network/interfaces /etc/network/interfaces.`date +%Y%m%d%H%M%S` fi if [ -f /etc/ssh/sshd_config ]; then cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +%Y%m%d%H%M%S` fi if [ -f /etc/crontab ]; then cp -p /etc/crontab /etc/crontab.`date +%Y%m%d%H%M%S` fi if [ -f /etc/modules ]; then cp -p /etc/modules /etc/modules.`date +%Y%m%d%H%M%S` fi if [ -f /etc/rc.local ]; then cp -p /etc/rc.local /etc/rc.local.`date +%Y%m%d%H%M%S` fi if [ -f /etc/udhcpd.conf ]; then cp -p /etc/udhcpd.conf /etc/udhcpd.conf.`date +%Y%m%d%H%M%S` fi if [ -f /etc/wvdial.conf ]; then cp -p /etc/wvdial.conf /etc/wvdial.conf.`date +%Y%m%d%H%M%S` fi # Add line so that it is possible to connect via SSH over WiFi grep -q -F 'IPQoS cs0 cs0' /etc/ssh/sshd_config || echo 'IPQoS cs0 cs0' >> /etc/ssh/sshd_config # Add line to check ppp grep -q -F '#*/1 * * * * root /opt/GoIoT/DinGo/bin/modem/rn_check_ppp0.sh' /etc/crontab || echo '#*/1 * * * * root /opt/GoIoT/DinGo/bin/modem/rn_check_ppp0.sh' >> /etc/crontab # Some edits to modules grep -q -F 'i2c-dev' /etc/modules || echo 'i2c-dev' >> /etc/modules #grep -q -F 'rtc-ds1339' /etc/modules || echo 'rtc-ds1339' >> /etc/modules grep -q -F 'usbserial' /etc/modules || echo 'usbserial' >> /etc/modules grep -q -F 'crc-ccitt' /etc/modules || echo 'crc-ccitt' >> /etc/modules grep -q -F 'ppp_generic' /etc/modules || echo 'ppp_generic' >> /etc/modules grep -q -F 'ppp_async' /etc/modules || echo 'ppp_async' >> /etc/modules grep -q -F 'slhc' /etc/modules || echo 'slhc' >> /etc/modules grep -q -F 'option' /etc/modules || echo 'option' >> /etc/modules grep -q -F 'usb_storage' /etc/modules || echo 'usb_storage' >> /etc/modules grep -q -F 'mac80211' /etc/modules || echo 'mac80211' >> /etc/modules # Enable port-forwarding ipforward=`cat /proc/sys/net/ipv4/ip_forward` if [ "$ipforward" = "0" ]; then echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sysctl -p /etc/sysctl.conf fi # 2. GET EXECUTABLES AND OTHER GetFiles # 3. SET PERMISSIONS ON EXECUTABLES SetPermissions # 4. RUN POST-SCRIPTS # We are now going to cpoy some files to etc and overwrite them. cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/default/hostapd /etc/default/hostapd cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/init.d/hostapd /etc/init.d/hostapd cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/init.d/udhcpd /etc/init.d/udhcpd cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/network/interfaces /etc/network/interfaces cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/rc.local /etc/rc.local cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/udhcpd.conf /etc/udhcpd.conf cp -p /opt/GoIoT/apt/dingo-stack-networking_1.0-1/etc/wvdial.conf /etc/wvdial.conf echo -e "${blue}GoIoT: Install finished.${NC}" exit 0 fi # -- UPGRADE -- if [ "$mode" = "upgrade" ]; then echo -e "${blue}GoIoT: Start upgrading...${NC}" # 1. RUN PRE-SCRIPTS # 2. GET EXECUTABLES AND OTHER GetFiles # 3. SET PERMISSIONS ON EXECUTABLES SetPermissions # 4. RUN POST-SCRIPTS echo -e "${blue}GoIoT: Finished upgrading.${NC}" exit 0 fi