Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 17720196a167486f45fafad66982cf255bf011f0 0 parents
@pbrisbin authored
Showing with 217 additions and 0 deletions.
  1. +50 −0 README.md
  2. +46 −0 menu.xml
  3. +121 −0 wifi-pipe
50 README.md
@@ -0,0 +1,50 @@
+# Wifi pipe
+
+Scan for available networks and present them (along with strength and
+security info) directly in your openbox menu. Click to connect.
+
+### Method
+
+This script is called once when the menu is initiated (`sudo wifi-pipe
+<interface>`); it parses the output of `iwlist <interface> scan` and
+formats the xml structure required to display the lines in your menu.
+
+Each menu entry gets assigned a command which will recall this script
+(`sudo wifi-pipe <interface> connect <essid>`); when clicked, that
+command will do the required profile setup and network connection via
+netcfg.
+
+You can test these commands individually in a terminal if you find
+something's not working.
+
+### Requirements
+
+* netcfg
+
+For doing the actual connecting.
+
+* zenity
+
+For entering passwords when required.
+
+* A `NOPASSWD` entry for this script
+
+Since the script requires root but is called directly from the window
+manager's environment using sudo, you'll have no chance to enter a
+password.
+
+ username ALL=(ALL) NOPASSWD: /path/to/wifi-pipe
+
+* An entry for it in your menu.xml
+
+An example menu.xml is included in this repo.
+
+### Notes
+
+I no longer use this; it may or may not work. If you actively use it and
+need something fixed or changed, please open an issue here. I'll try to
+get to it when I can -- be prepared to help me test fixes.
+
+Also, if anyone could send me some screenshots of this in action, I'd
+love to include them in this README. I'm not motivated enough to start
+up an openbox instance to get my own :).
46 menu.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<openbox_menu xmlns="http://openbox.org/3.4/menu">
+ <menu id="root-menu" label="Openbox 3">
+ <item label="System Monitor">
+ <action name="Execute">
+ <command>urxvtc -g 120x40 -e htop</command>
+ </action>
+ </item>
+ <item label="Run...">
+ <action name="Execute">
+ <command>gmrun</command>
+ <startupnotify>
+ <enabled>yes</enabled>
+ </startupnotify>
+ </action>
+ </item>
+ <item label="Mixer">
+ <action name="Execute">
+ <command>ossxmix</command>
+ <startupnotify>
+ <enabled>yes</enabled>
+ </startupnotify>
+ </action>
+ </item>
+ <item label="Chat">
+ <action name="Execute">
+ <command>pidgin</command>
+ <startupnotify>
+ <enabled>yes</enabled>
+ </startupnotify>
+ </action>
+ </item>
+ <separator />
+
+ <!--
+
+ better to place it on the bottom, since the scan is expensive. if
+ you roll your mouse over this item on your way elsewhere, you'll
+ have to wait.
+
+ note: interface is usually wlan0.
+
+ -->
+ <menu id="pipe-wifi" label="Wifi" execute="sudo /path/to/wifi-pipe interface"/>
+ </menu>
+</openbox_menu>
121 wifi-pipe
@@ -0,0 +1,121 @@
+#!/bin/bash
+#
+# https://github.com/pbrisbin/wifi-pipe
+#
+###
+
+. /usr/lib/network/network
+[[ -z "$PROFILE_DIR" ]] && PROFILE_DIR='/etc/network.d/'
+
+errorout() {
+ echo '<openbox_pipe_menu>'
+ echo "<item label=\"$1\" />"
+ echo '</openbox_pipe_menu>'
+ exit 1
+}
+
+create_profile() {
+ local essid="$1" interface="$2" security="$3" key="$4"
+ local profile_file="$PROFILE_DIR/$essid"
+
+ cat > "$profile_file" << EOF
+CONNECTION="wireless"
+ESSID="$essid"
+INTERFACE="$interface"
+DESCRIPTION="Automatically generated profile"
+SCAN="yes"
+IP="dhcp"
+TIMEOUT="10"
+SECURITY="$security"
+EOF
+
+ if [[ -n "$key" ]]; then
+ echo "KEY=\"$key\"" >> "$profile_file"
+ chmod 600 "$profile_file"
+ else
+ chmod 644 "$profile_file"
+ fi
+}
+
+# prints the actual xml based on the scan
+print_menu() {
+ local essid security quality
+
+ local awkscript='BEGIN { FS=":"; OFS="="; }
+ /\<Cell/ { if (essid) print essid, security, quality[2]/quality[3]*100; security="none" }
+ /\<ESSID:/ { essid=substr($2, 2, length($2) - 2) } # discard quotes
+ /\<Quality=/ { split($1, quality, "[=/]") }
+ /\<Encryption key:on/ { security="wep" }
+ /\<IE:.*WPA.*/ { security="wpa" }
+ END { if (essid) print essid, security, quality[2]/quality[3]*100 }'
+
+ # scan for networks
+ iwlist $interface scan 2>/dev/null | awk "$parser" | sort -t= -nrk3 > /tmp/networks.tmp
+
+ # exit if none found
+ if [[ ! -s /tmp/networks.tmp ]]; then
+ rm /tmp/networks.tmp
+ errorout 'no networks found.'
+ fi
+
+ # otherwise print the menu
+ echo '<openbox_pipe_menu>'
+ while IFS='=' read -r essid security quality; do
+ echo "<item label=\"$essid ($security) ${quality/.*/}%\">" # trim decimals
+ echo ' <action name=\"Execute\">'
+ echo " <command>sudo $0 $interface connect \"$essid\"</command>"
+ echo ' </action>'
+ echo '</item>'
+ done < /tmp/networks.tmp
+ echo '</openbox_pipe_menu>'
+}
+
+connect() {
+ local essid="$1" profile_file profile security key
+
+ # check for an existing profile
+ profile_file="$(grep -REl "ESSID=[\"']?$essid[\"']?" "$PROFILE_DIR" | grep -v '~$' | head -1)"
+
+ if [[ -n "$profile_file" ]]; then
+ profile="$(basename "$profile_file")"
+ else
+ profile="$essid"
+ security="$(awk -F '=' "/$essid/"'{print $2}' /tmp/networks.tmp | head -n 1)"
+
+ # ask for the security key if needed
+ if [[ "$security" != 'none' ]]; then
+ key="$(zenity --entry --title="Authentication" --text="Please enter $security key for $essid" --hide-text)"
+ fi
+
+ # create the new profile
+ create_profile "$essid" "$interface" "$security" "$key"
+ fi
+
+ # connect
+ /usr/bin/netcfg "$profile" > /tmp/output.tmp
+
+ # if failed, ask about removal of created profile
+ if [[ $? -ne 0 ]]; then
+ zenity --question \
+ --title="Connection failed" \
+ --text="$(grep -Eo "[\-\>]\ .*$" /tmp/output.tmp) \n Remove $profile_file?" \
+ --ok-label="Remove profile"
+
+ [[ $? -eq 0 ]] && rm "$profile_file"
+ fi
+
+ rm /tmp/output.tmp
+ rm /tmp/networks.tmp
+}
+
+[[ $(id -u) -ne 0 ]] && errorout 'root access required'
+[[ -z "$1" ]] && errorout 'usage: wifi-pipe <interface> [ connect <essid> ]'
+
+interface="$1"; shift
+
+if ! ifconfig | grep -q $interface; then
+ ifconfig $interface up &>/dev/null || errorout "$interface not up"
+ while ! ifconfig | grep -q $interface; do sleep 1; done
+fi
+
+[[ "$1" = 'connect' ]] && connect "$2" || print_menu
Please sign in to comment.
Something went wrong with that request. Please try again.