diff --git a/bin/oref0-online.sh b/bin/oref0-online.sh index 3ccc14303..056e88df2 100755 --- a/bin/oref0-online.sh +++ b/bin/oref0-online.sh @@ -4,6 +4,13 @@ main() { MACs=$@ HostAPDIP='10.29.29.1' echo; echo Starting oref0-online at $(date). + #Need to add "presistent_btpan": true to the end of the preferences.json file to enable PersistentBT Pan + if cat preferences.json | jq -e .persistent_btpan > /dev/null; then + PersistentBTPAN="true" + echo "At $(date) PersistentBT Pan is enabled" + else + PersistentBTPAN="false" + fi # if we are connected to wifi but don't have an IP, try to get one if iwgetid -r wlan0 | egrep -q "[A-Za-z0-9_]+"; then if ! ip route | grep default | grep -q wlan0; then @@ -12,26 +19,37 @@ main() { ls -la /tmp/bad_wifi else echo Attempting to renew wlan0 IP + ip route del 0/0 + sudo dhclient wlan0 -r -q sudo dhclient wlan0 fi fi fi + echo "At $(date) my gateway is: $(ip route | awk '/default/ {print $3}') via interface $(ip route | awk '/default/ {print $5}')" if ifconfig | egrep -q "wlan0" >/dev/null; then - #if [[ $(ip -4 -o addr show dev wlan0 | awk '{split($4,a,"/");print a[1]}') = $(print_local_ip wlan0) ]]; then - print_wifi_name + #if [[ $(ip -4 -o addr show dev wlan0 | awk '{split($4,a,"/");print a[1]}') = $(print_local_ip wlan0) ]]; then + print_wifi_name echo -n "At $(date) my local wifi IP is: " print_local_ip wlan0 - fi - if ifconfig | egrep -q "bnep0" >/dev/null; then - #if [[ $(ip -4 -o addr show dev bnep0 | awk '{split($4,a,"/");print a[1]}') = $(print_local_ip bnep0) ]]; then - print_bluetooth_name - #fi - echo -n "At $(date) my local Bluetooth IP is: " - print_local_ip bnep0 - else - echo "At $(date) my Bluetooth PAN is not connected" - fi - echo -n "At $(date) my public IP is: " + fi + if ifconfig | egrep -q "bnep0" >/dev/null; then + #if [[ $(ip -4 -o addr show dev bnep0 | awk '{split($4,a,"/");print a[1]}') = $(print_local_ip bnep0) ]]; then + print_bluetooth_name + #fi + if ! has_ip bnep0; then + sudo dhclient bnep0 + fi + echo -n "At $(date) my local Bluetooth IP is: " + print_local_ip bnep0 + else + if [ "$PersistentBTPAN" = "true" ] ; then + echo "At $(date) my Bluetooth PAN is not connected, trying to connect now" + bt_connect $MACs + else + echo "At $(date) my Bluetooth PAN is not connected" + fi + fi + echo -n "At $(date) my public IP is: " if check_ip; then stop_hotspot if has_ip wlan0 && has_ip bnep0; then @@ -40,10 +58,12 @@ main() { ifdown wlan0; ifup wlan0 fi fi - # if online via wifi, disconnect BT + # if online via wifi, disconnect BT if no persistent PAN if has_ip wlan0 && ifconfig | egrep -q "bnep0" >/dev/null; then - bt_disconnect $MACs - #wifi_dhcp_renew + if [ "$PersistentBTPAN" != "true" ] ; then + bt_disconnect $MACs + #wifi_dhcp_renew + fi fi else echo @@ -51,16 +71,24 @@ main() { if ! has_ip wlan0; then wifi_dhcp_renew fi - if ! check_ip >/dev/null; then + if has_ip bnep0; then + #check if default route is using BT PAN, if not remove all gateways and release/renew bt dhcp lease + if ! ip route get 8.8.8.8 | egrep -q "bnep0" >/dev/null; then + ip route del 0/0 + sudo dhclient bnep0 -r -q + sudo dhclient bnep0 + fi + else bt_connect $MACs fi + #print_wifi_name if check_ip >/dev/null; then # if we're online after activating bluetooth, shut down any local-access hotspot we're running stop_hotspot - if ! print_local_ip wlan0 | egrep -q "[A-Za-z0-9_]+" >/dev/null; then - wifi_dhcp_renew - fi + if ! print_local_ip wlan0 | egrep -q "[A-Za-z0-9_]+" >/dev/null; then + wifi_dhcp_renew + fi else # if we can't connect via BT, might as well try previously bad wifi networks again rm /tmp/bad_wifi @@ -114,33 +142,56 @@ function bt_connect { # loop over as many MACs as are provided as arguments for MAC; do #echo -n "At $(date) my public IP is: " - if ! check_ip >/dev/null; then + if [ "$PersistentBTPAN" == "true" ]; then + if has_ip bnep0; then + break + fi + echo; echo "No BT PAN detected, attempting to connect BT to $MAC" + else + if check_ip > /dev/null; then + break + fi echo; echo "No Internet access detected, attempting to connect BT to $MAC" - oref0-bluetoothup - sudo bt-pan client $MAC -d - sudo bt-pan client $MAC && sudo dhclient bnep0 - if ifconfig | egrep -q "bnep0" >/dev/null; then - echo -n "Connected to Bluetooth with IP: " - print_local_ip bnep0 + fi + oref0-bluetoothup + sudo bt-pan client $MAC -d + sudo bt-pan client $MAC && sudo dhclient bnep0 + if ifconfig | egrep -q "bnep0" >/dev/null; then + if ! has_ip bnep0; then + sudo dhclient bnep0 fi - # if we couldn't reach the Internet over wifi, but (now) have a bnep0 IP, release the wifi IP/route - if has_ip wlan0 && has_ip bnep0 && ! grep -q $HostAPDIP /etc/network/interfaces; then - # release the wifi IP/route but *don't* renew it, in case it's not working + echo -n "Connected to Bluetooth with IP: " + print_local_ip bnep0 + fi + # if we couldn't reach the Internet over wifi, but (now) have a bnep0 IP, release the wifi IP/route + if has_ip wlan0 && has_ip bnep0 && ! grep -q $HostAPDIP /etc/network/interfaces; then + #check if Persistent Pan + if [ "$PersistentBTPAN" == "true" ] ; then + #check if BT is routing traffic + if ip route get 8.8.8.8 | egrep -q "bnep0" >/dev/null; then + # release the wifi IP/route but *don't* renew it, in case it's not working + sudo dhclient wlan0 -r + iwgetid -r wlan0 >> /tmp/bad_wifi + fi + else sudo dhclient wlan0 -r - iwgetid -r wlan0 >> /tmp/bad_wifi + iwgetid -r wlan0 >> /tmp/bad_wifi fi - #echo fi + #echo done } function bt_disconnect { - echo "Disconnecting BT $MAC" - ifdown bnep0 - # loop over as many MACs as are provided as arguments - for MAC; do - sudo bt-pan client $MAC -d - done + #check if Persistent Pan + if [ "$PersistentBTPAN" != "true" ] ; then + echo "Disconnecting BT $MAC" + ifdown bnep0 + # loop over as many MACs as are provided as arguments + for MAC; do + sudo bt-pan client $MAC -d + done + fi } function wifi_dhcp_renew { @@ -150,7 +201,7 @@ function wifi_dhcp_renew { else echo; echo "Getting new wlan0 IP" ps aux | grep -v grep | grep -q "dhclient wlan0" && sudo killall dhclient - sudo dhclient wlan0 -r + sudo dhclient wlan0 -r -q sudo dhclient wlan0 fi }