scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

commit 7e520eb42b63614759a22612cd3ac57e9538e1d9
parent 30e2d7e5553bcf4c1b0e1d7aeca418735551c7f6
Author: clamiax <smoppy@gmail.com>
Date:   Sun, 26 Oct 2014 19:39:35 +0100

Add the iwpick script

Diffstat:
Aiwpick | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 90 insertions(+), 0 deletions(-)

diff --git a/iwpick b/iwpick @@ -0,0 +1,90 @@ +#!/bin/bash +# Connects to a network +# Requires: wpa_supplicant, dhclient, iwconfig + +connect() { + iface=$1 + essid=$2 + key=$3 + + echo "Connecting to ${essid}..." + + ifconfig $iface up + iwconfig $iface essid $essid + + if [ -n "$key" ]; then + tmp=$(mktemp) + echo "network={ + ssid=\"$essid\" + psk=\"$key\" + key_mgmt=WPA-EAP WPA-PSK + proto=RSN WPA + pairwise=CCMP TKIP + }" >> $tmp + wpa_supplicant -Dwext -i $iface -c "$tmp" -dd -B 2>&1 > /dev/null + rm $tmp + fi + + dhclient $iface +} + +die() { + echo "$0: $@" + exit 1 +} + +usage() { +echo -n "Usage: $0 [-hg] [-i <iface>] [-e <essid>] [-k <psk>] [-a <alias>] +-i <iface> Network interface +-a <alias> Use the specified alias to connect +-e <essid> ESSID of the network to connect +-k <key> Key of the network to connect +-f <file> Networks file +-h Show this helps +" +exit 1 +} + +main() { + iface=wlan0 + file=~/.networks + alias= + essid= + key= + + while getopts "ina:e:k:f:h" o; do + case $o in + i) iface=$OPTARG;; + a) alias=$OPTARG;; + e) essid=$OPTARG;; + k) key=$OPTARG;; + f) file=$OPTARG;; + h) usage;; + esac + done + + [ "$(id -u)" -ne 0 ] && die "You must be root" + [ -z "$alias" -a -z "$essid" ] && usage + + if [ -n "$alias" ]; then + s="$(sed -n /^${alias}:/p $file |cut -d: -f2-3)" + essid="$(echo $s |cut -d: -f1)" + key="$(echo $s |cut -sd: -f2)" + else + # If explicitly specified, check if the ESSID do exists + valid="$(iwlist wlan0 scan |grep ESSID |grep -wc $essid)" + [ $valid -eq 0 ] && die "essid '$essid' is not valid" + fi + + [ -z "$essid" ] && die "alias '${alias}' not found" + + apps=(wpa_supplicant dhclient) + for a in ${apps[@]} + do + pkill -f "$a" + done + + connect "$iface" "$essid" "$key" +} + +main "$@"