|
2011-04-02
, 16:36
|
Posts: 166 |
Thanked: 74 times |
Joined on May 2010
|
#122
|
|
2011-04-03
, 01:26
|
Posts: 1,425 |
Thanked: 983 times |
Joined on May 2010
@ Hong Kong
|
#123
|
|
2011-04-03
, 09:22
|
Posts: 166 |
Thanked: 74 times |
Joined on May 2010
|
#124
|
#!/bin/sh # # /opt/tether/tether.sh # # Written by jschan @ http://talk.maemo.org # # Code snippets and inspiration from: # - http://wiki.maemo.org/DbusScripts # - http://talk.maemo.org/showthread.php?t=58570 # - http://code.google.com/p/n900ipv6/source/browse/share/pan # # Enables tethering over USB network and Bluetooth PAN. # # usage: ./tether.sh start|stop [internal_interface] # # Examples: # - "./tether.sh start" - Enables tethering on a single PAN client when an Internet connection is active # - "./tether.sh stop" - Disables tethering on a single PAN client (assumes the client is on bnep0) # - "./tether.sh start bnep1" - Enables tethering on a 2nd PAN client when an Internet connection is active # - "./tether.sh stop bnep1" - Disables tethering on the 2nd PAN client, without interfering with the 1st # # Notes: # - Tested to work with multiple Bluetooth PAN clients including a WiFi-only XOOM # - Tested to work in conjuction with a dbus-scripts to automate tethering setups # - Doesn't currently have support for multiple concurrent Internet interfaces # such as an IPv4 grps connection and a seperate IPv6 grps connection. # - IPv6 isn't supported yet... (Waiting for T-Mobile IPv6 activation) # DEFAULT_INTERFACE="bnep0" PAN_NETWORK_PREFIX="10.20" USB_NETWORK_PREFIX="10.30" INTERFACE_OCTET="88" DHCP_MIN_IP_OCTET="100" DHCP_MAX_IP_OCTET="127" DHCP_MAX_LEASE_TIME="1h" PRIMARY_DNS="8.8.8.8" SECONDARY_DNS="8.8.4.4" if [ $# -lt 1 ] || [ $# -gt 2 ]; then echo "usage: $0 start|stop [internal_interface]" exit 1 fi if [ -z "$2" ]; then INTERFACE="$DEFAULT_INTERFACE" else INTERFACE="$2" fi print_message () { echo "$(date) $*" run-standalone.sh dbus-send \ --type=method_call \ --dest=org.freedesktop.Notifications \ /org/freedesktop/Notifications \ org.freedesktop.Notifications.SystemNoteInfoprint \ string:"$*" } #set -x INTERFACE_TYPE="$(echo $INTERFACE | sed 's/\([a-z]*\)[0-9]*/\1/')" INTERFACE_NUMBER="$(echo $INTERFACE | sed 's/[a-z]*\([0-9]*\)/\1/')" case $INTERFACE_TYPE in bnep ) NETWORK_PREFIX="$PAN_NETWORK_PREFIX.$INTERFACE_NUMBER" ;; usb ) NETWORK_PREFIX="$USB_NETWORK_PREFIX.$INTERFACE_NUMBER" ;; * ) # Notify no active Internet connection print_message "Unsupported interface type: $INTERFACE_TYPE" exit 1 ;; esac INTERFACE_IP="$NETWORK_PREFIX.$INTERFACE_OCTET" NETMASK="255.255.255.0" EXTERNAL_INTERFACE="$(ifconfig | awk '/(gprs|wlan)/ {print $1}' | head -1)" DHCP_MIN_IP="$NETWORK_PREFIX.$DHCP_MIN_IP_OCTET" DHCP_MAX_IP="$NETWORK_PREFIX.$DHCP_MAX_IP_OCTET" DHCP_GATEWAY="$INTERFACE_IP" MESSAGE="Tethering from $INTERFACE to $EXTERNAL_INTERFACE" ACTIVATION_MSG="$MESSAGE activated" DEACTIVATION_MSG="$MESSAGE deactivated" DNSMASQ_PID_NAME="dnsmasq.$INTERFACE" DNSMASQ_PID_FILE="/var/run/$DNSMASQ_PID_NAME.pid" if [ "$1" == "start" ]; then INTERFACE_EXISTANCE_CHECK="$(ifconfig -a | grep $INTERFACE | awk '{print $1}')" if [ -z "$INTERFACE_EXISTANCE_CHECK" ]; then # Notify no active Internet connection print_message "$INTERFACE does not exist" exit 1 fi if [ -z "$EXTERNAL_INTERFACE" ]; then # Notify no active Internet connection print_message "Tethering request from $INTERFACE recieved" exit 1 fi # Setup interface ifconfig $INTERFACE down 2> /dev/null ifconfig $INTERFACE up ifconfig $INTERFACE $INTERFACE_IP netmask $NETMASK # Load modules modprobe ipt_MASQUERADE # flush old iptables rules iptables --flush iptables --table nat --flush # Setup NAT iptables --out-interface $EXTERNAL_INTERFACE \ --table nat \ --append POSTROUTING \ --jump MASQUERADE # Enable IP forwarding #echo 1 > /proc/sys/net/ipv4/ip_forward echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding echo 1 > /proc/sys/net/ipv4/conf/$EXTERNAL_INTERFACE/forwarding # Setup DNS and DHCP start-stop-daemon \ --exec /usr/sbin/dnsmasq \ --pidfile $DNSMASQ_PID_FILE \ --start -- \ --interface=$INTERFACE \ --listen-address=$INTERFACE_IP \ --except-interface=lo \ --bind-interfaces \ --pid-file=$DNSMASQ_PID_FILE \ --dhcp-range=$DHCP_MIN_IP,$DHCP_MAX_IP,$DHCP_MAX_LEASE_TIME \ --dhcp-option=1,$NETMASK \ --dhcp-option=3,$DHCP_GATEWAY \ --dhcp-option=6,$PRIMARY_DNS,$SECONDARY_DNS # Notify user bluetooth tethering is active print_message "$ACTIVATION_MSG" elif [ "$1" == "stop" ]; then start-stop-daemon \ --exec /usr/sbin/dnsmasq \ --pidfile $DNSMASQ_PID_FILE \ --stop # Bring down the interface ifconfig $INTERFACE down 2> /dev/null # Disable IP forwarding for tethered interface if [ -f "/proc/sys/net/ipv4/conf/$INTERFACE/forwarding" ]; then echo 0 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding 2> /dev/null fi # Notify user bluetooth tethering is inactive print_message "$DEACTIVATION_MSG" # Disable external interface IP forwarding if appropriate DNSMASQ_CHECK="$(ps -eaf | grep 'dnsmasq' | grep -v -e '127\.0\.0\.1' -e 'grep')" if [ -z "$DNSMASQ_CHECK" ]; then echo "$(date) No more clients tethered--disabling all forwarding" echo 0 > /proc/sys/net/ipv4/conf/$EXTERNAL_INTERFACE/forwarding echo 0 > /proc/sys/net/ipv4/ip_forward fi else echo "unknown argument: $1" echo "usage: $0 start|stop [internal_interface]" exit 1 fi
The Following 9 Users Say Thank You to jschan For This Useful Post: | ||
|
2011-04-03
, 11:16
|
Posts: 1,425 |
Thanked: 983 times |
Joined on May 2010
@ Hong Kong
|
#125
|
|
2011-04-08
, 08:47
|
Posts: 166 |
Thanked: 74 times |
Joined on May 2010
|
#126
|
DEFAULT_IAP_ID="***SEE DOCS ABOVE***" ... EXTERNAL_INTERFACE="**CHECK***" if [ -z "$EXTERNAL_INTERFACE" ]; then print_message "Establishing Default Connection" dbus-send --system --type=method_call \ --dest=com.nokia.icd /com/nokia/icd \ com.nokia.icd.connect \ string:$DEFAULT_IAP_ID uint32:0 EXTERNAL_INTERFACE="**CHECK AGAIN***" if [ -z "$EXTERNAL_INTERFACE" ]; then # Notify no active Internet connection print_message "Unable to establish tether" exit 1 fi fi ...
|
2011-04-08
, 16:18
|
Posts: 1,425 |
Thanked: 983 times |
Joined on May 2010
@ Hong Kong
|
#127
|
found some more docs about calls on the dbus: http://wiki.maemo.org/Phone_control#...ved_connection
apparently, if you don't already have a data connection open, you can open one. unfortunately, it looks like it requires user specific data, though i'm sure a gconftool call along with some combination of grep and awk could probably generalize it.
anyhow, i updated my script with it so that it makes the connection without having the prompt for a connection. the important parts are as follows:
cheersCode:DEFAULT_IAP_ID="***SEE DOCS ABOVE***" ... EXTERNAL_INTERFACE="**CHECK***" if [ -z "$EXTERNAL_INTERFACE" ]; then print_message "Establishing Default Connection" dbus-send --system --type=method_call \ --dest=com.nokia.icd /com/nokia/icd \ com.nokia.icd.connect \ string:$DEFAULT_IAP_ID uint32:0 EXTERNAL_INTERFACE="**CHECK AGAIN***" if [ -z "$EXTERNAL_INTERFACE" ]; then # Notify no active Internet connection print_message "Unable to establish tether" exit 1 fi fi ...
for i in `gconftool -R /system/osso/connectivity/IAP | grep /system/osso/connectivity/IAP/ | sed -e "s:\(.*\)\:\(.*\):\1:"`; do if [ "`gconftool -g $i/type`" == "GPRS" ]; then IAP=$i; fi; done
|
2011-04-21
, 23:58
|
|
Posts: 552 |
Thanked: 183 times |
Joined on Oct 2009
@ Not decided
|
#128
|
|
2011-04-29
, 09:17
|
Posts: 6 |
Thanked: 3 times |
Joined on Apr 2011
|
#129
|
|
2011-04-29
, 09:43
|
Posts: 1,425 |
Thanked: 983 times |
Joined on May 2010
@ Hong Kong
|
#130
|
Tags |
bluetooth-pan, tethering |
Thread Tools | |
|
I've updated the script on first page. Note that I change the network from 192.168.1 to 192.168.3 to avoid conflicting with WiFi. Please try again.
Last edited by 9000; 2011-04-02 at 06:48.