-
Notifications
You must be signed in to change notification settings - Fork 1
/
inventory-ap.sh
executable file
·158 lines (140 loc) · 3.71 KB
/
inventory-ap.sh
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#/!bin/bash
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
echo "Script exited by user. Cleaning up temp files."
rm -f *.temp
exit 2
}
# check if snmpwalk is installed
type snmpwalk >/dev/null 2>&1 || { echo >&2 "Error: This script requires snmpwalk but it's not installed. Please install the snmp package using for example:
sudo apt-get install snmp."; exit 1; }
# some time variables
THEDATE=`date +%Y-%m-%d`
THETIME=`date +%Y-%m-%d_%H:%M:%S`
# variable for background looping
PID_ARRAY=()
# help message function
usage()
{
cat << EOF
usage: $0 [OPTION] DATA [OPTION] DATA
This script will scan Cisco WLC(s) for AP inventory, using snmp v2.
Email: [email protected]
OPTIONS:
-h, --help display this help message
-c COMMUNITY set the community string. sets community "public" if unset
-f FILE file containing wlc ip(s)
-H IP scan just 1 host. may not be used with -f
-o FILENAME output file
EOF
}
# send help message for --help
if [ "$1" == "--help" ] ; then
usage
exit 1
fi
# help message if no flags were given
if [ $# == 0 ] ; then
usage
exit 2
fi
# variables used
#WLCFILE=
#WLCIP=
#COMMUNITY=
#RESULTFILE=
# parse script input using getopts
while getopts "hH:f:c:o:" OPTION; do
case $OPTION in
h)
usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG"
usage
exit 1
;;
c)
COMMUNITY=$OPTARG
;;
H)
WLCIP=$OPTARG
;;
f)
WLCFILE=$OPTARG
;;
o)
RESULTFILE=$OPTARG
;;
esac
done
# check if resultfile already exists
if test -f "$RESULTFILE"; then
echo "ERROR: Output file already exists. Exiting."
exit 2
fi
# check if -H and -f was used together
if [[ -v WLCFILE ]] && [[ -v WLCIP ]]; then
echo "ERROR: Cannot use -H and -f together. Exiting."
exit 2
fi
# default community public if unset
if [[ -z $COMMUNITY ]]; then
COMMUNITY=public
fi
# inform that script is running
echo "Running script... this can take a moment."
# initial line to resultfile if needed
if ! [[ -z $RESULTFILE ]]; then
echo "AP IP;HOSTNAME;MODEL;S/N;AP Software Version;WLC IP" >> $RESULTFILE
fi
apscanner() {
# gather data from wlc
WLC=$1
NAME=$(snmpwalk -v 2c -c $COMMUNITY $WLC 1.3.6.1.4.1.14179.2.2.1.1.3)
DEVICE=$(snmpwalk -v 2c -c $COMMUNITY $WLC 1.3.6.1.4.1.14179.2.2.1.1.16)
SERIAL=$(snmpwalk -v 2c -c $COMMUNITY $WLC 1.3.6.1.4.1.14179.2.2.1.1.17)
IP=$(snmpwalk -v 2c -c $COMMUNITY $WLC 1.3.6.1.4.1.14179.2.2.1.1.19)
VER=$(snmpwalk -v 2c -c $COMMUNITY $WLC 1.3.6.1.4.1.14179.2.2.1.1.8)
PROCESS_ARRAY=()
# process data
while read -r APNAME; do
ID=$(echo "$APNAME" | sed -n 's/.*14179\.2\.2\.1\.1\.3\.\([0-9.]\+\).*/\1/p')
HN=$(echo "$APNAME" | grep "$ID " | awk '/STRING: / {print substr($4,1)}' | sed 's/"//g')
DEV=$(echo "$DEVICE" | grep "$ID " | awk '/STRING: / {print substr($4,1)}' | sed 's/"//g')
SN=$(echo "$SERIAL" | grep "$ID " | awk '/STRING: / {print substr($4,1)}' | sed 's/"//g')
APIP=$(echo "$IP" | grep "$ID " | awk '/IpAddress: / {print substr($4,1)}' | sed 's/"//g')
APVER=$(echo "$VER" | grep "$ID " | awk '/STRING: / {print substr($4,1)}' | sed 's/"//g')
# print result
echo "$APIP;$HN;$DEV;$SN;$APVER;$WLC"
done <<< "$NAME"
}
# do different actions if -H or -f was used, and if output file is chosen
if ! [[ -z $WLCIP ]]; then
if ! [[ -z $RESULTFILE ]]; then
apscanner $WLCIP >> $RESULTFILE
elif [[ -z $RESULTFILE ]] ; then
apscanner $WLCIP
fi
echo "Done!"
exit 0
elif ! [[ -z $WLCFILE ]]; then
for i in `cat $WLCFILE`; do
# add each background PID to an array
if ! [[ -z $RESULTFILE ]]; then
apscanner $i >> $RESULTFILE &
PID_ARRAY+=($!)
elif [[ -z $RESULTFILE ]] ; then
apscanner $i &
PID_ARRAY+=($!)
fi
done
# wait for each background PID to finish
for i in ${PID_ARRAY[*]} ; do
wait $i
done
echo "Done!"
exit 0
fi