From 4d398a0e6cec847311c6f883dd05bdad67070d8c Mon Sep 17 00:00:00 2001 From: Abhay Rana Date: Sun, 15 Mar 2015 17:21:52 +0530 Subject: [PATCH] Updates the ap-hotspot script --- ap-hotspot | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/ap-hotspot b/ap-hotspot index 22dd2a3..f47e717 100755 --- a/ap-hotspot +++ b/ap-hotspot @@ -2,8 +2,8 @@ # Script to set up a WiFi Hotspot in Ubuntu that works with # Android and Windows Phone. # -# Copyright (C) 2013 Alin Andrei -# Copyright (C) 2013 Satyajit sahoo +# Copyright (C) 2014 Alin Andrei +# Copyright (C) 2014 Satyajit sahoo # # This scipt is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published @@ -21,9 +21,11 @@ # Global variables logfile="/tmp/hostapd.log" pidfile="/tmp/hotspot.pid" -hotspotconfig="/etc/hostapd-hotspot.conf" -dnsmasqconfig="/etc/dnsmasq.conf" -user=$(who | grep $(echo "$DISPLAY" | cut -d '.' -f 1) | cut -d ' ' -f 1 | tail -1) +hotspotconfig="/etc/ap-hotspot.conf" +dnsmasqconfig="/etc/dnsmasq.d/ap-hotspot.rules" +user=$(who | awk '{print $1}' | sed '/^root$/d' | uniq) +WMPID=$(ps -u $user | tail -n 1 | awk '{print $1}') +DBUS=$(egrep -z 'DBUS_SESSION_BUS_ADDRESS|DISPLAY' /proc/${WMPID}/environ | sed -r -e 's/(.)DBUS_/\1 DBUS_/' -e 's/(.)DISPLAY/\1 DISPLAY/') show_msg() { echo -e "$@" @@ -48,7 +50,7 @@ done } show_notify() { -sudo -u "$user" notify-send -h int:transient:1 -i "network-wireless" "$@" +su $user -s /bin/bash -c "${DBUS} notify-send -h int:transient:1 -i \"network-wireless\" \"$@\"" } check_root() { @@ -62,7 +64,7 @@ fi check_supported() { # Check if the wireless card supports Access Point mode. This script won't work if it doesn't support it if [[ ! $(iw list 2>&1 | grep -A6 "Supported interface modes" | grep AP$) ]]; then - show_err "Your wireless card does not support Access Point mode" + show_err "Your wireless card or driver does not support Access Point mode" exit 1 fi } @@ -102,17 +104,14 @@ check_root check_supported # Reset config rm -f "$hotspotconfig" -sed -i "s/# Bind to only one interface//g" "$dnsmasqconfig" -sed -i "s/bind-interfaces//g" "$dnsmasqconfig" -sed -i "s/# Choose interface for binding//g" "$dnsmasqconfig" -sed -i "s/^interface=.*//g" "$dnsmasqconfig" -sed -i "s/# Specify range of IP addresses for DHCP leasses//g" "$dnsmasqconfig" -sed -i "s/dhcp-range=192.168.150.2.*//g" "$dnsmasqconfig" -sed -i "s/#INTERFACE_NET=.*//g" "$dnsmasqconfig" -cat -s "$dnsmasqconfig" | tee "$dnsmasqconfig" > /dev/null 2>&1 +rm -f "$dnsmasqconfig" +# Detect configuration +show_msg "Detecting configuration..." +INTERFACE_NET=$(route | grep -iw "default" | awk '{printf "%s ", $NF}' | sed -s 's/ $//') +INTERFACE_WLAN=$(iwconfig 2>&1 | grep "^wlan" | sed -e 's/ .*//g' | tail -n 1) +SSID="myhotspot" +WPAPASS="qwerty0987" # Network interface connected to the Internet -show_msg "Detecting main network interface..." -INTERFACE_NET=$(route | grep -iw "default" | awk '{print $NF}') if [[ ! $INTERFACE_NET ]]; then show_warn "Failed to detect the network interface connected to the Internet. Please enter your network interface (e.g.- eth1):" else @@ -121,7 +120,6 @@ fi read interface_net [[ "$interface_net" ]] && INTERFACE_NET="$interface_net" # WiFi interface -INTERFACE_WLAN=$(iwconfig 2>&1 | grep "^wlan" | sed -e 's/ .*//g') if [[ ! $INTERFACE_WLAN ]]; then show_warn "Failed to detect the WiFi interface. Please enter your WiFi interface (e.g.- wlan0):" else @@ -130,12 +128,10 @@ fi read interface_wlan [[ "$interface_wlan" ]] && INTERFACE_WLAN="$interface_wlan" # Hotspot SSID -SSID="myhotspot" show_msg "Enter the desired Access Point name or press ENTER to use the default one ($SSID):" read ssid [[ "$ssid" ]] && SSID="$ssid" # WPA Passphrase -WPAPASS="qwerty0987" show_msg "Enter the desired WPA Passphrase below or press ENTER to use the default one ($WPAPASS):" read wpapass [[ "$wpapass" ]] && WPAPASS="$wpapass" @@ -152,15 +148,15 @@ channel=1 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 -wpa=3 +wpa=2 wpa_passphrase=$WPAPASS wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP EOF # Add the required bits to the dnsmasq config file -if [[ ! $(grep "Bind to only one interface" "$dnsmasqconfig") ]]; then -cat < /dev/null 2>&1 +if [[ ! $(grep "Bind to only one interface" "$dnsmasqconfig" > /dev/null 2>&1) ]]; then +cat < /dev/null 2>&1 # Bind to only one interface bind-interfaces # Choose interface for binding @@ -169,6 +165,7 @@ interface=$INTERFACE_WLAN dhcp-range=192.168.150.2,192.168.150.10,12h #INTERFACE_NET=$INTERFACE_NET EOF +chmod +x "$dnsmasqconfig" fi } @@ -250,7 +247,10 @@ sysctl net.ipv4.ip_forward=0 2>&1 | show_debug service hostapd stop 2>&1 | show_debug service dnsmasq stop 2>&1 | show_debug # Restart WiFi and disable newly created mon.WLAN network -ifconfig "mon.$INTERFACE_WLAN" down +if [[ ! -z $(ifconfig | grep "mon.$INTERFACE_WLAN") ]]; then +# Check if the hotspot is active + ifconfig "mon.$INTERFACE_WLAN" down +fi ifconfig "$INTERFACE_WLAN" down ifconfig "$INTERFACE_WLAN" up }