Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #---------------------------------------------------------------------------
- # Variables declaration
- #---------------------------------------------------------------------------
- # Uncomment if broadcast-relaying is necessary
- #bcRelay_ON=1
- wlanCXed=0
- idx=0
- err_ON=0
- scriptPath=""
- #---------------------------------------------------------------------------
- # Local Functions
- #---------------------------------------------------------------------------
- arp_routing(){
- local __iface1=$1
- local __iface2=$2
- parprouted $__iface1 $__iface2 >> "$scriptPath" 2>&1
- if [ "$(echo $?)" -ne 0 ]
- then
- sleep 1
- return 1
- fi
- return 0
- }
- dhcp_relay(){
- local __iface1=$1
- local __iface2=$2
- #local dhcp_server=$(grep -w "dhcp-server" \
- #/var/lib/dhcp/dhclient.$__iface1.leases | tail -n1 | awk '{print $3}' \
- #| cut -d';' -f1)
- local dhcp_server=$(grep -R "offered" \
- /var/log/* | tail -n1 | awk '{print $(NF)}')
- dhcp-helper -s "$dhcp_server" -i "$__iface2" >> "$scriptPath" 2>&1
- if [ "$(echo $?)" -ne 0 ]
- then
- sleep 1
- return 1
- fi
- return 0
- }
- host_ap(){
- hostapd -B /etc/hostapd/hostapd.conf >> "$scriptPath" 2>&1
- if [ "$(echo $?)" -ne 0 ]
- then
- sleep 1
- return 1
- fi
- return 0
- }
- bc_relay(){
- local __iface1=$1
- local __iface2=$2
- bcrelay -d -n -i "$__iface1" -o "$__iface2" >> "$scriptPath" 2>&1
- if [ "$(echo $?)" -ne 0 ]
- then
- sleep 1
- return 1
- fi
- return 0
- }
- kill_process(){
- local __proc2kill=$1
- local CMDresult=$(ps aux | grep -v grep | grep "$__proc2kill")
- if [ -n "$CMDresult" ]
- then
- killall "$__proc2kill" >> "$scriptPath" 2>&1
- #while read line
- #do
- # kill $(echo $line | cut -d' ' -f2) >> "$scriptPath" 2>&1
- #done <<< $CMDresult
- fi
- }
- RST_process(){
- until ( ifconfig wlan1 down >> "$scriptPath" 2>&1 )
- do
- sleep 1
- done
- sleep 1
- until (ip addr flush dev wlan1 >> "$scriptPath" 2>&1)
- do
- sleep 1
- done
- kill_process "parprouted"
- kill_process "dhcp-helper"
- kill_process "hostapd"
- # Executes this section if bcRelay_ON is enabled (not commented)
- if [ -v bcRelay_ON ]
- then
- kill_process "bcrelay"
- fi
- sleep 1
- }
- CX_start(){
- err_ON=0
- RST_process
- wlan0Info=""
- while [ "$wlan0Info" == "" ]
- do
- # Check if wlan0 has an IP. Drops results and logs error messages
- if [ "$(ifconfig wlan0 2>>"$scriptPath" | grep -w "inet" | awk '{print $2}')" != "" ]
- then
- # Using wlan0Info just as a test condition to help exit this loop.
- # wlan0Info is reused later
- wlan0Info="exit"
- else
- sleep 1
- fi
- done
- wlan0Info=$(ip a show wlan0)
- wlan0IP=$(echo "$wlan0Info" | grep -w "inet" | awk '{print $2}' | cut -d'/' -f1)
- wlanIPMask=$(echo "$wlan0Info" | grep -w "inet" | awk '{print $2}' | cut -d'/' -f2)
- wlanbrd=$(echo "$wlan0Info" | grep -w "inet" | awk '{print $4}')
- wlan1IP1=$(echo "$wlanbrd" | cut -d'.' -f1)
- wlan1IP2=$(echo "$wlanbrd" | cut -d'.' -f2)
- wlan1IP3=$(echo "$wlanbrd" | cut -d'.' -f3)
- wlan1IP4=$(($(echo "$wlanbrd" | cut -d'.' -f4) - 1))
- wlan1IP=$(echo "$wlan1IP1"'.'"$wlan1IP2"'.'"$wlan1IP3"'.'"$wlan1IP4")
- until ( ifconfig wlan1 up >> "$scriptPath" 2>&1 )
- do
- sleep 1
- done
- sleep 1
- until (ip addr flush dev wlan1 >> "$scriptPath" 2>&1)
- do
- sleep 1
- done
- until ( ip addr add $(echo "$wlan1IP"'/'"$wlanIPMask") brd + dev wlan1 \
- >> "$scriptPath" 2>&1 )
- do
- sleep 1
- done
- until ( $(arp_routing "wlan0" "wlan1") )
- do
- idx=$(("$idx"+1))
- if [ "$idx" -eq 5 ]
- then
- echo "parprouted failed" >> "$scriptPath" 1>&1
- err_ON=1
- break
- fi
- done
- # echo "here1"
- if [ $err_ON -eq 0 ]
- then
- idx=0
- until ( $(dhcp_relay "wlan0" "wlan1") )
- do
- idx=$(("$idx"+1))
- if [ "$idx" -eq 5 ]
- then
- echo "dhcp-relay failed" >> "$scriptPath" 1>&1
- err_ON=1
- break
- fi
- done
- fi
- if [ $err_ON -eq 0 ]
- then
- idx=0
- until ( $(host_ap) )
- do
- idx=$(("$idx"+1))
- if [ "$idx" -eq 5 ]
- then
- echo "Host AP failed" >> "$scriptPath" 1>&1
- err_ON=1
- break
- fi
- done
- fi
- # Executes this section if bcRelay_ON is enabled (not commented)
- if [ -v bcRelay_ON ]
- then
- for i in $(seq 1 2)
- do
- if [ $err_ON -eq 0 ]
- then
- idx=0
- iface1="wlan0"
- iface2="wlan1"
- if [ $i -eq 2 ]
- then
- iface1="wlan1"
- iface2="wlan0"
- fi
- until ( $(bc_relay "$iface1" "$iface2") )
- do
- idx=$(("$idx"+1))
- if [ "$idx" -eq 5 ]
- then
- echo "BC_Relay failed" >> "$scriptPath" 1>&1
- err_ON=1
- break
- fi
- done
- fi
- done
- fi
- if [ $err_ON -eq 0 ]
- then
- wlanCXed=1
- else
- sleep 10
- fi
- }
- check_processes(){
- if [ "$(ps aux | grep -v grep | grep parprouted)" == "" ]
- then
- return 1
- elif [ "$(ps aux | grep -v grep | grep dhcp-helper)" == "" ]
- then
- return 1
- elif [ "$(ps aux | grep -v grep | grep hostapd)" == "" ]
- then
- return 1
- elif [ -v bcRelay_ON ]
- then
- if [ "$(ps aux | grep -v grep | grep bcrelay)" == "" ]
- then
- return 1
- fi
- fi
- return 0
- }
- CX_check(){
- if [ "$(ifconfig wlan0 | grep inet)" != "" ] && \
- [ $(check_processes; echo $?) -eq 0 ]
- then
- sleep 1m
- else
- RST_process
- wlanCXed=0
- fi
- }
- #---------------------------------------------------------------------------
- # Main
- #---------------------------------------------------------------------------
- scriptPath="$(cd "$(dirname $0)"; pwd -P)""/log_err"
- echo "Start" > "$scriptPath" 1>&1
- # Main Loop. Starts and keep checking on connection to not to break, and
- # all of the packages used to not to be ended. If any of this two conditions
- # happen, kills all processes, wait the connection to the router
- # to be re-stablished and start all processes again
- while true
- do
- if [ $wlanCXed -eq 0 ]
- then
- CX_start
- else
- CX_check
- fi
- done
- exit 0
- #---------------------------------------------------------------------------
- # END
- #---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement