#!/bin/bash -x # Niels Elgaard Larsen # script to use eeePC 701 with USB 3g modem as accesspoint # requires iptables, udhcpd, hostapd # License: GPL or BSD. # usage netshare my_network shrared_as # I.e. the first argument is how you connect to the internet # the second argument is how others connect through you. # networks are: 3g, wifi, ether, e.g: # netshare 3g wifi # netshare 3g wired # netshare wifi wired # netshare wired wifi if [ -z "$1" ]; then echo usage netshare my_network shrared_as echo networks are: 3g, wifi, ether, e.g: echo netshare 3g wifi echo netshare 3g wired echo netshare wifi wired echo netshare wired wifi exit 0 fi ESSID="itpol freedom" #FORCEGDEV=ppp0 #FORCEGDEV=hso0 CHANNEL=11 HWMODE=g #FORCEAPDRIVER=nl80211 echo 1 > /proc/sys/net/ipv4/ip_forward if [ "xwired" = "x$1" ] || [ "xwired" = "x$2" ]; then WIREDDEV=${FORCEWIRED-$(grep eth /proc/net/dev|cut -d: -f1)} fi if [ "x3g" = "x$1" ] || [ "x3g" = "x$2" ]; then GDEV=${FORCEGDEV-$(grep -e '\(hso\|ppp\)' /proc/net/dev|cut -d: -f1)} fi if [ "xwifi" = "x$2" ]; then # set FORCEWIFIDEV if you do not want the first device APDEV=${FORCEWIFIDEV-$(iw dev|grep Interface|grep -v 'mon\.'|head -1|awk '{print $2}')} ifconfig ${APDEV} down if [ -z "$1" ]; then echo did not find device for WWIFI access point fi APMODE=$(iwconfig $APDEV|grep IEEE|awk '{print $3}') if [ ${APMODE} = "802.1b" ]; then HWMODE=b fi echo using AP device ${APDEV} APDRIVER=${FORCEAPDRVER} if [ -z "$FORCEAPDRIVER" ]; then APDRIVER=${FORCEAPDRVER-hostap} APDRIVER=hostap if (lsmod|grep mac80211); then echo saw mac80211 module, guessing on nl80211 driver for devive $APDEV echo set FORCEAPDRIVER if necessary APDRIVER=nl80211 fi fi echo stop dhcp clients /etc/init.d/wicd stop killall -9 wpa_supplicant killall hostapd TFILE=$(mktemp) cat > "$TFILE" << ZZZ interface=$APDEV driver=${APDRIVER} ssid=${ESSID} hw_mode=${HWMODE} channel=${CHANNEL} ZZZ hostapd $TFILE & fi case "$1" in "3g" ) FROMDEV=${GDEV} ;; "wired" ) FROMDEV=${WIREDDEV} ;; "wifi" ) FROMDEV=${FORCEWIFIDEV-$(iw dev|grep Interface|grep -v 'mon\.'|head -1|awk '{print $2}')} esac echo sharing ${FROMDEV} case "$2" in "x3g" ) echo can not share on 3g exit 3 ;; "wired" ) TODEV=${WIREDDEV} ;; "wifi" ) TODEV=${APDEV} ;; esac echo sharing on ${TODEV} killall udhcpd ifconfig $TODEV down UDHCPCONFIG=$(mktemp) cat > "$UDHCPCONFIG" << ZZZ start 192.168.42.20 end 192.168.42.254 interface ${TODEV} opt router 192.168.42.1 opt dns 212.60.120.244 ZZZ iptables -t nat -F; iptables -t mangle -F #ifconfig $TODEV up ifconfig $TODEV 192.168.42.1 netmask 255.255.255.0 up iptables -t nat -A POSTROUTING -s 192.168.42.0/24 -o ${FROMDEV} -j MASQUERADE /sbin/iptables -A FORWARD -i ${TODEV} -o ${FROMDEV} -j ACCEPT /sbin/iptables -A FORWARD -i ${FROMDEV} -o ${TODEV} -j ACCEPT udhcpd $UDHCPCONFIG echo make sure your GPRS connection is up