Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
wconsole / usr / bin / wconsole_switcher
Size: Mime:
#!/bin/bash

#
# wconsole_switcher - script to switch wconsole on/off
#                     (usually called from the WLANPi menu system)    
#
# Written by Nigel Bowden <wifinigel@gmail.com>.
#
# History:
#
# v0.01 - 28th June 2019 - Added more ports for various serial speeds
# v0.02 - 29th June 2019 - Changed name to wconsole
# v0.03 - 30th June 2019 - Updated to add/remove FW rules at switch time 
#         (not during install)
# v0.04 - 25th July 2019 - Updated to remove install option
#                        - Added file checks before file copy/del operations
# v0.05 - no notes
# v0.06 - no notes
# v0.07 - 26th January 2020 - added check for WLAN NIC AP mode support
# v0.08 - 8th Feb 2020 - added support for Cisco USB cable
# v0.09 - 10th May 2020 - added support for new mode state file

set -e

NAME=wconsole_switcher
DESC="Script to switch wconsole on/off"
VERSION=0.07
STATUS_FILE="/etc/wlanpi-state"

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

###############################################################################
#
# Activate wconsole:
#
# 1. Backup various existing files to allow restoration when Wi-Fi console
#    deactivated
# 2. Remove a number of existing files that need to be replaced
# 3. Create links from deleted file locations to Wi-Fi console config files
# 4. Update ufw to allow tcp ports through that may be used for access
# 5. Create status file to indicate Wi-Fi console is active
# 6. Reboot the wlanpi to ensure clean activation
#
###############################################################################
wconsole_on () {

  # check what state the WLAN Pi is in
  PI_STATUS=`cat $STATUS_FILE | grep 'classic'` || true
  if  [ -z "$PI_STATUS" ]; then
     echo "Failed - WLAN Pi is not in classic mode."
     exit 1
  fi


  # check if the WLAN NIC supports AP mode before switching
  # iw list | awk '/Supported interface modes/, /Band/' | grep '\* AP'
   AP_SUPPORT=`iw list | awk '/Supported interface modes/, /Band/' | grep '\* AP'` || true
   if  [ -z "$AP_SUPPORT" ]; then
     echo "Failed - AP Mode not supported by adapter."
     exit 1
  fi

  
  echo "Enabling Wi-Fi console..."
  # Backup files
  if [ -e /etc/default/isc-dhcp-server ]; then
    cp /etc/default/isc-dhcp-server /etc/default/isc-dhcp-server.wcon
  fi
  if [ -e /etc/dhcp/dhcpd.conf ]; then
  cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.wcon
  fi
  if [ -e /etc/network/interfaces ]; then
    cp /etc/network/interfaces /etc/network/interfaces.wcon
  fi
  if [ -e /etc/sysctl.conf ]; then
    cp /etc/sysctl.conf /etc/sysctl.conf.wcon
  fi
  if [ -e /etc/default/ufw ]; then
    cp /etc/default/ufw /etc/default/ufw.wcon
  fi
  if [ -e /etc/ufw/before.rules ]; then
  cp /etc/ufw/before.rules /etc/ufw/before.rules.wcon
  fi
  # These files may or may not exist
  if [ -e /etc/ser2net.conf ]; then
    cp /etc/ser2net.conf /etc/ser2net.conf.wcon
  fi
  if [ -e /etc/hostapd.conf ]; then
    cp /etc/hostapd.conf /etc/hostapd.conf.wcon
  fi

  # Remove existing files
  rm -f /etc/default/isc-dhcp-server
  rm -f /etc/dhcp/dhcpd.conf
  rm -f /etc/network/interfaces
  rm -f /etc/sysctl.conf
  rm -f /etc/default/ufw
  rm -f /etc/ufw/before.rules

  # These files may or may not exist
  if [ -e /etc/ser2net.conf ]; then
    rm /etc/ser2net.conf
  fi
  if [ -e /etc/hostapd.conf ]; then
    rm /etc/hostapd.conf
  fi

  # Link to files in wconsole
  ln -s /etc/wconsole/default/isc-dhcp-server /etc/default/isc-dhcp-server 
  ln -s /etc/wconsole/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf
  ln -s /etc/wconsole/network/interfaces /etc/network/interfaces
  ln -s /etc/wconsole/conf/ser2net.conf /etc/ser2net.conf
  ln -s /etc/wconsole/conf/hostapd.conf /etc/hostapd.conf
  ln -s /etc/wconsole/sysctl/sysctl.conf /etc/sysctl.conf
  ln -s /etc/wconsole/default/ufw /etc/default/ufw
  ln -s /etc/wconsole/ufw/before.rules /etc/ufw/before.rules

  # Open up console ports on FW 
  ufw allow 2400:2408/tcp
  ufw allow 4800:4808/tcp
  ufw allow 9600:9608/tcp
  ufw allow 19200:19208/tcp
  ufw allow 38400:38408/tcp
  ufw allow 11520:11528/tcp
  ufw allow 2000:2008/tcp

  # Signal that wconsole active
  echo "wconsole" > $STATUS_FILE
  echo "WLAN Pi will now reboot"
  sleep 1
  reboot
}

###############################################################################
#
# Deactivate wconsole:
#
# 1. Remove links created during activation
# 2. Restore config files backed up during activation
# 3. Remove firewall rules added during activation
# 4. Remove status file to indicate Wi-Fi console no longer active
# 5. Reboot wlanpi to provide clean restoration of services
#
###############################################################################
wconsole_off () {

  # check what state the WLAN Pi is in
  PI_STATUS=`cat $STATUS_FILE | grep 'wconsole'` || true
  if  [ -z "$PI_STATUS" ]; then
     echo "Failed - WLAN Pi is not in wconsole mode."
     exit 1
  fi

  echo "Disabling Wi-Fi console..."
  # Remove sym links to wconsole
  unlink /etc/default/isc-dhcp-server
  unlink /etc/dhcp/dhcpd.conf
  unlink /etc/network/interfaces
  unlink /etc/ser2net.conf
  unlink /etc/hostapd.conf
  unlink /etc/sysctl.conf
  unlink /etc/default/ufw
  unlink /etc/ufw/before.rules

  # Restore old files
  cp /etc/default/isc-dhcp-server.wcon /etc/default/isc-dhcp-server
  cp /etc/dhcp/dhcpd.conf.wcon /etc/dhcp/dhcpd.conf
  cp /etc/network/interfaces.wcon /etc/network/interfaces
  cp /etc/sysctl.conf.wcon /etc/sysctl.conf
  cp /etc/default/ufw.wcon /etc/default/ufw
  cp /etc/ufw/before.rules.wcon /etc/ufw/before.rules
  # These files may or may not exist
  if [ -e /etc/ser2net.conf.wcon ]; then
    cp /etc/ser2net.conf.wcon /etc/ser2net.conf
  fi
  if [ -e /etc/hostapd.conf.wcon ]; then
    cp /etc/hostapd.conf.wcon /etc/hostapd.conf
  fi
  # Close ports on FW
  ufw delete allow 2400:2408/tcp
  ufw delete allow 4800:4808/tcp
  ufw delete allow 9600:9608/tcp
  ufw delete allow 19200:19208/tcp
  ufw delete allow 38400:38408/tcp
  ufw delete allow 2000:2008/tcp
  ufw delete allow 11520:11528/tcp
  echo "WLAN Pi will now reboot"
  echo "classic" > $STATUS_FILE
  sleep 1
  reboot
}

status () {
  PI_STATUS=`cat $STATUS_FILE | grep 'wconsole'` || true
  if  [ -z "$PI_STATUS" ]; then
    echo "wconsole is currently disabled"
    exit 0
  else
    echo "wconsole is currently enabled"
    exit 0
  fi

}

version () {
    N=/etc/wconsole/$NAME
        echo "Version: $N $VERSION" >&2
        exit 1

}

case "$1" in
  on)
        wconsole_on
        ;;
  off)
        wconsole_off
        ;;
  status)
        status
        ;;
  version)
        version;;
  *)
        N=/etc/wconsole/$NAME
        echo "Usage: $N {on|off|status|version}" >&2
        exit 1
        ;;
esac

exit 0