View Single Post
  #7   (View Single Post)  
Old 4th October 2011
puffy
-Guest-
 
Posts: n/a
Wink

The OP does not give us the output of `ifconfig`, which would be useful to determine whether the rum0 interface has to be marked 'up'. My assumption would be that he does NOT have an /etc/hostname.rum0 file that runs on startup because he is typing in

# ifconfig rum0 nwid mcdonalds

If he does have an /etc/hostname.rum0 file, then he should do something like:

# pgrep dhclient >/dev/null && sudo pkill dhclient; sudo dhclient rum0

Heck that could even be an alias... At any rate, I hope the following script automates your next wifi connection:
HTML Code:
#!/bin/sh
#
# BSD license and disclaimers apply.
#
# Do not change to zsh;  it will break. There are subtle differences.
#
# This script should be installed with execute permissions, and
# be invoked by name.
#
#-------------------------------------------------------------------------
#
#  helper functions
#
#-------------------------------------------------------------------------

function parseit
{
    local therest

    shift 1
    wifiname=$1
    shift 1
    therest=$*
    IFS=" "
    set $therest
    shift 4
    sigstrength=$1
}

function readprobe
{
#
# this was not as easy to write as it looks
#
    local input

    while read input
    do
       IFS=" "
       if echo $input | grep -q \"; then
          IFS="\""
       fi
       parseit $input
       ap[nwifi]=$wifiname
       decibel[nwifi]=${sigstrength%dB}
       nwifi=$(($nwifi+1))
    done
}

function sortandprint
{
    typeset tempnm
    typeset tempst
    integer i
    integer j
    integer inc
    integer n
    integer s

    s=$1
    n=$2
# s is offset in arrays where sort starts
# n is number of items to sort
# In other words, sort elements $s to $s + $n
#
# Implement a Shell sort.  In ksh.  Painful.  All the write-only jive-notation
# of perl, none of the functionality.
#

    if [ $n -eq 0 ]; then
       echo $MSG10
       return
    fi

    inc=$(($n/2))
    while [ $inc -gt 0 ]
    do
       i=$inc
       while [ $i -lt $n ]
       do
          j=$i
          tempst=${decibel[$(($i+$s))]}
          tempnm=${ap[$(($i+$s))]}
#
# to change the sense of the sort, change the second test.
# use -lt for biggest first, -gt for smallest first.
#
          while [[ $j -ge $inc && ${decibel[$(($j+$s-$inc))]} -lt $tempst ]]
          do
             decibel[$(($j+$s))]=${decibel[$(($j+$s-$inc))]}
             ap[$(($j+$s))]=${ap[$(($j+$s-$inc))]}
             j=$(($j-$inc))
          done
          decibel[$(($j+$s))]=$tempst
          ap[$(($j+$s))]=$tempnm
          i=$(($i+1))
       done

       if [ $inc -eq 2 ]; then
          inc=1
       else
          inc=$(($inc/2))
       fi
    done

    i=$s
    while [ $i -lt $(($n+$s)) ]
    do
       printf "%4d   %-32s\t%3d%s\n" $(($i+1)) "${ap[$i]}" ${decibel[$i]} "dB"
       i=$(($i+1))
    done
}

function cleanup
{
    if [ -t 0 -a -t 1 ]; then
       stty sane
    fi
    rm -f $TMPPROBE
    exit
}

#-------------------------------------------------------------------------
#
# "main" part of the script
#
#-------------------------------------------------------------------------

progname=${0##*/}
ifname=`ifconfig |
    awk '{lines[NR]=$0} /IEEE802.11/ {print substr(lines[NR-4],1,4)}'`
MSG1="usage: $progname"
MSG2="$progname:  Wireless access point selection for device:"
MSG3="Available public wifi . . . . . . . . . score"
MSG4="Available secured wifi . . . . . . . . .score"
MSG5="$progname: no wireless access points found"
MSG6="choice out of range"
MSG7="try again"
MSG8="public wifi selected"
MSG9="$progname: not interactive and no public wifi"
MSG10="none probed"
CHOOSEPROMPT="Select wifi> "
PASSPROMPT="Password for"
PROBECMD="sudo ifconfig $ifname scan"

if [ $# -gt 0 ]; then
    echo $MSG1 \\nextra arguments: $@ >&2
    exit 1
fi

TMPPROBE=`mktemp -t $ifname.XXXXXXXX` || exit 1

if pgrep dhclient >/dev/null; then
    sudo pkill dhclient
fi

trap cleanup EXIT INT PIPE

integer nwifi=0
integer private=0

if [ -t 0 -a -t 1 ]; then
    mypager=$PAGER
else
    mypager=cat
fi

# generate a menu
# Do this even for batch use, to log results during, say, boot/rc
#
# if this script is used in another script and output is not
# wanted, then invoke it with </dev/null >/dev/null and perhaps
# 2>/dev/null
#

echo $MSG2 $ifname
echo
echo $MSG3
echo "-----------------------------------------------------------------"
eval \$PROBECMD \| sed -n \'/privacy/!s/^[[:space:]]*//\;/^nwid/p\' \> \$TMPPROBE
readprobe < $TMPPROBE

sortandprint 0 $nwifi > $TMPPROBE
eval \$mypager \$TMPPROBE

# for obscure reasons of scoping, do not "simplify" the preceding
# two lines by discarding the temp file and putting readprobe on the
# end of the pipe.
#
# Put no function in a pipe (wrecks scope)
#

# the dividing point between public and private:

private=$nwifi

echo
echo $MSG4
echo "-----------------------------------------------------------------"
eval \$PROBECMD \| sed -n \'/privacy/s/^[[:space:]]*//\;/^nwid/p\' \> \$TMPPROBE
readprobe < $TMPPROBE

sortandprint $private $(($nwifi-$private)) > $TMPPROBE
eval \$mypager \$TMPPROBE

# the "if" establishes whether stdin and stdout are ttys.
# This is not flawless, of course, but tends to guess whether
# an operator is present.
# It would be better if a variable asserting batchness were present
# on the command line or in the environment.

integer choice=-1

if [ $nwifi -eq 0 ]; then
    echo $MSG5 >&2
    cleanup
fi

if [ -t 0 -a -t 1 ]; then
    while [ 0 ]
    do
	read choice?"$CHOOSEPROMPT"
	if [ $choice -eq 0 ]; then
            break
	elif [ $choice -gt $nwifi ]; then
            echo $MSG6 $choice
            echo $MSG7
	else
            break
	fi
    done

   if [ $choice -eq 0 ] ; then
	cleanup
   fi
#
# decrement choice to accommodate zero-based arrays
#
    choice=$(($choice-1))
    if [ $choice -ge $private ]; then
	stty -echo
	read pass?"$PASSPROMPT ${ap[$choice]}> "
	stty echo
	echo
	wpakey="wpakey $pass"
    else
	echo $MSG8 ${ap[$choice]}
    fi
else		# non-interactive
    if [ $private -eq 0 ]; then
	echo $MSG9 >&2
	cleanup
    else	# configure the first (strongest) public wifi
	choice=1
    fi
fi
eval sudo ifconfig \$ifname nwid \"${ap[$choice]}\" \$wpakey up
sudo dhclient $ifname

cleanup

Last edited by puffy; 18th April 2012 at 12:13 PM.
Reply With Quote