#!/bin/bash ## Azilink Tether application launch script ## This script will initiate all necessary ## processes on your computer to enable the ## tether between your G1 and your PC. ## Tripp Holden - August 13, 2009 ## --Updated - October 22, 2009-- ## Variables ## Optional, and depends if you've added the ## Android SDK to your $PATH. ANDROIDSDK=/apps/android_sdk_1.6 VPNCONF=/apps/tether/azilink.ovpn RESOLVCONF=/etc/resolvconf/run/resolv.conf ## Functions start_wrapper () { adb_service_start vpn_service_start add_ns i=1 VPNSTATUS=`grep "Initialization Sequence Completed" /tmp/azilink.out | awk '{ print $8 }'` while [[ "$VPNSTATUS" != Completed ]] && [[ "$i" -le 6 ]]; do VPNSTATUS=`grep "Initialization Sequence Completed" /tmp/azilink.out | awk '{ print $8 }'` if [ "$i" -eq 6 ]; then echo "===============================================================================================" echo "##WARNING:## VPN did not start successfully, check /tmp/azilink.out." echo "Ensure Azilink service is started on your G1 and that it is connected to your computer via USB." echo "===============================================================================================" ## Call stop functions adb_service_stop vpn_service_stop remove_ns exit 0 fi sleep 3 (( i++ )) done } stop_wrapper () { adb_service_stop vpn_service_stop remove_ns } adb_service_start () { ## Check for the adb server and make its process a variable. SRVPS=`ps -ef | grep "adb fork-server server" | grep -v grep | awk '{ print $2 }'` ## if the process does not exist, start the server if [ -z $SRVPS ]; then $ANDROIDSDK/tools/adb devices 1>/tmp/azilink.out & else ## if the process exists, kill it and re-start the server kill -9 $SRVPS $ANDRIODSDK/tools/adb devices 1>>/tmp/azilink.out & fi ## wait for the adb server to initialize sleep 5 ## Forward the necessary local port to the G1 phone for VPN communication $ANDROIDSDK/tools/adb forward tcp:41927 tcp:41927 1>>/tmp/azilink.out & echo "VPN Port Forwarded." echo "" echo "ENABLE AZILINK TETHER SERVICE ON YOUR G1 PHONE NOW!" echo "" ## This is usually instant, but allow time for the user to enable the azilink application on the phone sleep 10 } vpn_service_start () { echo "VPN Starting." ## Check for the vpn process if it is running and make the process a variable. VPNPS=`ps -ef | grep "$VPNCONF" | grep -v grep | awk '{ print $2 }'` ## if the process does not exist, start the server if [ -z $VPNPS ]; then openvpn --config "$VPNCONF" 1>>/tmp/azilink.out & else ## if the process exists, kill it and re-start the process kill -9 $VPNPS openvpn --config "$VPNCONF" 1>>/tmp/azilink.out & fi echo "Waiting for VPN to start..." } add_ns () { echo "Adding nameserver to $RESOLVCONF." ## check if nameserver entry exists and create a variable to test against NSENTRY=`grep 192.168.56.1 "$RESOLVCONF" | awk '{ print $1 }'` ## if the entry does not exist, add it if [ -z $NSENTRY ]; then echo "nameserver 192.168.56.1" >> "$RESOLVCONF" ## if the entry exists, exit fi } adb_service_stop () { ## stop all processes for the tether echo "Stopping the tether service..." ## make the adb server process a variable SRVPS=`ps -ef | grep "adb fork-server server" | grep -v grep | awk '{ print $2 }'` ## if the server is not running, skip and move to the next process otherwise kill it if [ -z $SRVPS ]; then echo "ADB not running." else echo "Stopping adb server, process $SRVPS." kill -9 $SRVPS fi } vpn_service_stop () { VPNPS=`ps -ef | grep "$VPNCONF" | grep -v grep | awk '{ print $2 }'` ## if the vpn is not running, skip and move to the next process otherwise kill it if [ -z $VPNPS ]; then echo "VPN not running." else echo "Shutting down VPN, process $VPNPS." kill -9 $VPNPS fi } remove_ns () { echo "Removing nameserver entry if it exists." NSENTRY=`grep 192.168.56.1 "$RESOLVCONF" | awk '{ print $1 }'` ## Remove the namserver entry if it exists, otherwise exit if [ -z $NSENTRY ]; then echo "Nameserver entry does not exist." else cat "$RESOLVCONF" | sed "/192.168.56.1/d" > /tmp/resolv.conf mv /tmp/resolv.conf "$RESOLVCONF" fi } ## Script case "$1" in 'start') echo "Starting the tether service..." start_wrapper ;; 'stop') stop_wrapper echo "Exiting, you may shutdown Azilink tether service on your G1 now." ;; esac