-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfty-device-scan
executable file
·96 lines (85 loc) · 2.92 KB
/
fty-device-scan
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
#!/bin/bash
# Copyright (C) 2016 - 2020 Eaton
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#! \file fty-device-scan
# \brief Helper script to scan an SNMP-capable power device and
# report the scan results via e-mail
# \author Tomas Halman <[email protected]>
# \author Arnaud Quette <[email protected]>
MAIL="[email protected]"
IP=""
CFG="/etc/default/bios.cfg"
TMPFILE="/tmp/scan-`date -u '+%s'`-$$.gz"
getCommunities() {
# This routine fetches the user-provided SNMP community strings from
# the configuration file $CFG (if any) and appends the default "public"
# to the list of strings to try against the separately scanned device.
local FILE="${CFG}"
if [[ ! -s "${FILE}" ]]; then
FILE="/dev/null"
fi
awk '
BEGIN{ section=""; subsection="" }
{
if (section == "snmp" && subsection == "community") {
i = index ($0, "\"");
if (i) {
c = substr ($0,i);
print substr (c, 2, length(c) - 2);
}
}
}
/^[a-zA-Z]/{ section = $1 }
/^ {4}[a-zA-Z]/{ subsection = $1 }
END{ print "public"; }
' < "${FILE}"
}
usage() {
echo "usage: `basename ${0}` IPADDRESS [email]"
echo " default email is ${MAIL}"
exit 1;
}
if [[ -z "${1-}" ]] ; then
usage
else
IP="${1}"
fi
if [[ -n "${2-}" ]] ; then
MAIL="${2}"
fi
echo "Scan of the device at address ${IP} will be sent to ${MAIL}."
echo "Scan can contain sensitive information such as IP address or admin contacts."
echo ""
echo "Press <Enter> to continue or <Ctrl-C> to cancel"
read
getCommunities | while read community; do
echo "Trying community \"${community}\"..."
for version in "1" "2c" ; do
if snmpgetnext -v "${version}" -c "${community}" "${IP}" .1 2> /dev/null ; then
echo "Community ${community}/v${version} works, scanning mib..."
snmpwalk -On -v "${version}" -c "${community}" "${IP}" .1 2>/dev/null | gzip > "${TMPFILE}"
break 2
fi
done
done
if [[ ! -s "${TMPFILE}" ]] ; then
echo "Failed to scan device. Is SNMP configured on ${IP}?" >&2
exit 2
fi
echo "Scanning done, posting e-mail..."
echo "" | fty-sendmail -s "SNMP scan of ${IP}" -a "${TMPFILE}" "${MAIL}"
rm -f "${TMPFILE}"