File: testappassive

package info (click to toggle)
whereami 0.3.37
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: sh: 1,094; perl: 524; makefile: 24
file content (129 lines) | stat: -rwxr-xr-x 3,287 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
#
# $Id: testappassive,v 1.1 2005/02/12 23:28:24 andrew Exp $
#
# by Andrew McMillan, Catalyst IT Ltd, (c) 2004 licensed
# for use under the GPL version 2
# Altered by Matthew Grant <grantma@anathoth.gen.nz>, (c) 2004
#
# This script uses "iwlist ${INTERFACE} scan" to see if we can connect to
# a useful local WLAN.
#
###############################################
# NOTE:  This script is something of a work in progress.  I can't have experience
# with all possible WLAN hardware, so if you need help getting it working with what
# you happen to use, feel free to call upon me.
###############################################
#
# Parameters:
#   -  first form: [<interface>,]scan
#   - second form: <ESSID pattern>
#   -  third form: [<interface>,]<ESSID pattern>
#
# <interface> 	Interface to test.  If omitted, use INTERFACE variable 
#		(from detect.conf)
#
# scan       	Normally we just use the last scan details (if they're less 
#		than 10 secs old), but sometimes we may want to force a scan.
#
# <ESSID pattern>  A pattern that should match the ESSID we are looking for.
#
# Environment variables:
#
# INTERFACE	Default interface name to use if not specified
# DEBUGWHEREAMI Turn on debugging output (0 for debug msgs, 1 for execution tracing)
# 
# Examples:
#     testapscan eth1,scan
#     set INTERFACE eth1
#     testapscan scan
#     testapscan eth1,othercap
#     testapscan friendap
#     set INTERFACE ath0
#     testapscan mywlan
#

STATEDIR=${STATEDIR:-"/var/lib/whereami"}
WHEREAMILOCK=$LOCKDIR/whereami.started

######################################################
# Perform the actual scan, which gets all nearby AP
# details into a file we will use for subsequent calls
######################################################
scan_for_ap()
{
  IFSTATE="`ifconfig | egrep \"^${INTERFACE}[\t ]\"`"
  if [ "$IFSTATE" = "" ] ; then
    ifconfig ${INTERFACE} up
    # We seem to need a sleep here to accumulate the
    # scan data - for madwifi, at least.
    sleep 5
  fi

  /sbin/iwlist ${INTERFACE} scan >${STATEFILE}

  # Be a tidy kiwi, and put the interface back how we found it...
  if [ "$IFSTATE" = "" ] ; then
    ifconfig ${INTERFACE} down
  fi
}

######################################################
# Utility to do pattern match, with awkward quoting
######################################################
ap_pattern_match()
{
  egrep "ESSID:\"$1\"" "${STATEFILE}"
}

######################################################
# Test if there is an AP in our scan that matches
######################################################
test_which_ap()
{
  ap_pattern_match "$1" >/dev/null
}

# Turn on execution tracing, for debugging...
[ -n "$DEBUGWHEREAMI" ] && set -o xtrace

PATTERN=""
ACTION="test"
INTERFACE=${INTERFACE:-eth0}
case $1 in
  *,scan)
    # First form
    INTERFACE=${1/,scan}
    ACTION="scan"
    ;;
  scan)
    # First form
    INTERFACE=${INTERFACE:-eth0}
    ACTION="scan"
    ;;
  *,*)
    # Third form
    INTERFACE=${1/,*}
    PATTERN=${1/*,}
    ;;
  *)
    # Second form
    PATTERN="$1"
    ;;
esac

STATEFILE=$STATEDIR/iwlist.$INTERFACE
mkdir -p $STATEDIR

STATUS=1
if [ "$ACTION" = "scan" ] ; then
  scan_for_ap
  STATUS=$?
fi

if [ "$PATTERN" != "" ];then
  test_which_ap "$PATTERN"
  STATUS=$?
fi

exit $STATUS