You are not logged in.

#1 2011-01-02 23:25:49

Buce
Member
From: Duluth, Minnesota, US
Registered: 2009-03-17
Posts: 23

netman script

Due to an issue I'm having with netcfg (described at https://bbs.archlinux.org/viewtopic.php?id=111054), I dusted off an old script I wrote called 'netman' and spruced it up.

#!/bin/bash
# automanually connect to wireless network
#
# when invoked as 'netman <network>', this script does the following:
# 1. brings up the $IFACE and $WIFACE interfaces if they're down
# 2. parses $CONFDIR/networks.conf for the network alias and name
# 3. attempts to connect to the network
# 3.1 if $CONFDIR/<network>.wpa exists, attempts a WPA connection using
#     wpa_supplicant $WPAOPTS -i $WIFACE -c $CONFDIR/<network>.wpa
# 3.2 if $CONFDIR/<network>.wep exists, attempts a WEP connection using
#     iwconfig $WIFACE essid <network> key <key>,
#     where <key> is the contents of $CONFDIR/<network>.wep
# 3.3 if $CONFDIR/<network>.ext doesn't exist, connects insecurely
# 4. obtains a lease with $DHCPCD $STARTOPTS $WIFACE
#
# return status is the return status of the last failed $DHCP attempt made
# with the <network> or "ethernet" commands, or 0 otherwise
#
# any of the variables above can be set via the $NETMANCONFIG file
# or, if NETMANCONFIG is not in your environment,
# via /usr/local/etc/netman/netman.conf
#
# defaults values:
# CONFDIR=/usr/local/etc/netman
# IFACE=eth0
# WIFACE=wlan0
# DHCP=dhclient
# STARTOPTS=""
# STOPOPTS="-r"
# WPAOPTS="-B -Dwext"

VERSION=1.2

NETMANCONFIG=${NETMANCONFIG-/usr/local/etc/netman/netman.conf}
[ -r "$NETMANCONFIG" ] && . "$NETMANCONFIG"

##### BEGIN CONFIGURABLE SETTINGS #####
# if any of these are in NETMANCONFIG, it uses that value instead

CONFDIR=${CONFDIR-/usr/local/etc/netman}

IFACE=${IFACE-eth0}
WIFACE=${WIFACE-wlan0}

DHCP=${DHCP-dhclient}
STARTOPTS=${STARTOPTS-""}
STOPOPTS=${STOPOPTS-"-r"}
WPAOPTS=${WPAOPTS-"-B -Dwext"}

##### END CONFIGURABLE SETTINGS #####

CONFFILE=${CONFDIR}/networks.conf

EXIT_VALUE=0

# run as root
if [ "`whoami`" != "root" ]; then
    echo Only root can run this script
    exit 1
fi

# bring up any down interfaces
for iface in $IFACE $WIFACE
do
    if ! ifconfig | grep -q $iface
    then
        echo Bringing up interface $iface...
        ifconfig $iface up
    fi
done

# let's roll
while [ $# -gt 0 ]
do
    case "$1" in
      eth|ethernet)
        echo Connecting via ethernet...
        $DHCP $STARTOPTS $IFACE || EXIT_VALUE=$?
      ;;
      stop)
        echo Stopping all internet connections...
        for iface in $IFACE $WIFACE
        do
            $DHCP $STOPOPTS $iface
        done
      ;;
      version)
        echo `basename $0` v$VERSION
        echo Written by Nate Woodward
        exit $EXIT_VALUE
      ;;
      help)
        cat <<EOF
Usage: $0 [ <command> [ <command> [ ... ] ] ]

<command> is one of:
    <network>   connect to <network> defined in $CONFFILE
    ethernet    connect via ethernet
    stop        disconnect from all networks
    version     print version and exit
    help        print this help and exit
EOF
        exit $EXIT_VALUE
      ;;
      *)
        if ! grep -q "^$1" ${CONFFILE}
        then
            echo Network configuration not found for $1
            EXIT_VALUE=1
            shift
            continue
        else
            LINE=`grep "^$1" ${CONFFILE} | head -n1`
        fi

        ALIAS=${LINE%% *}
        ESSID=${LINE#* }

        echo -n Connecting to ${ESSID}
        PASSFILE="${CONFDIR}/${ALIAS}"
        if [ -f "${PASSFILE}.wpa" ]; then
            echo " using WPA authentication..."
            wpa_supplicant ${WPAOPTS} -i ${WIFACE} -c ${PASSFILE}.wpa
        elif [ -f "${PASSFILE}.wep" ]; then
            echo " using WEP authentication..."
            iwconfig ${WIFACE} essid ${ESSID} key `cat ${PASSFILE}.wep`
        else
            echo " without any authentication..."
            iwconfig ${WIFACE} essid ${ESSID}
        fi

        $DHCP $STARTOPTS $WIFACE || EXIT_VALUE=$?
      ;;
    esac
    shift
done

exit $EXIT_VALUE

The comments at the top describe how it works and a few variables it uses. Assuming a WPA network with an ESSID of "TheMatrix" that you want to reference by the alias "matrix", the minimum amount of configuration you should need to do is (don't just execute these commands willy-nilly, look at what's going on):

# mkdir -p /usr/local/etc/netman
# cat >/usr/local/etc/netman/netman.conf <<EOF
WIFACE=wlan0 # or whatever your wireless interface is
DHCP=dhcpcd
STOPOPTS="-k"
WPAOPTS="-B -Dwext" # this is the default, change if you need to
EOF
# cat >/usr/local/etc/netman/networks.conf <<EOF
matrix TheMatrix
EOF
# wpa_passphrase TheMatrix "secretpassphrase" >/usr/local/etc/netman/matrix.wpa

If you've got a WEP network instead, replace the last command with

# echo "secretpassphrase" >/usr/local/etc/netman/matrix.wep

Then, if the script is in your PATH and your wireless card and driver aren't screwey, "netman matrix" (or "netman mat" or "netman m") should hopefully get you a connection.

Offline

Board footer

Powered by FluxBB