-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnmaptonagios.sh
executable file
·198 lines (176 loc) · 4.94 KB
/
nmaptonagios.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
#
# Export Nmap result to Nagios cfg files
#
# Nicolargo - 12/2010
#
# Based on: http://blog.nicolargo.com/?p=4011
#
# Syntaxe: # ./nmaptonagios.sh <IP address range>
# Exemple: # ./nmaptonagios.sh 192.168.1.0/24
#
# ToDo List
# - Support de NMap v4.x
#
# GPL
# 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 Library 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
#
VERSION="0.1.3"
# Where can i find Nagios plugins (optionnal)
NAGIOS_PLUGINS_PATH="/usr/lib/nagios/plugins"
# Are you r00t ?
if [ $EUID -ne 0 ]; then
echo "Script should be run as root: # sudo $0" 1>&2
exit 1
fi
# Arguments
#----------
if [ $# -ne 1 ]
then
echo "Usage: `basename $0` <IP address range>"
exit 1
fi
# Fonctions
#----------
# Exec a command (quiet mode) and display text
# 1 - command
# 2 - text
function execnodisplay {
echo -n "$2: Please wait..."
$1 > /dev/null 2>&1
echo -e "\r$2: Done (return code $?)"
}
# Exec a command and display text
# 1 - command
# 2 - text
function execdisplay {
echo "$2: Please wait..."
$1
echo "$2: Done (return code $?)"
}
# Display Nmap OS
# 1 - detail
# 2 - running
# 3 - service
function displaynmapos {
if [ "$1" = "" ]
then
if [ "$2" = "" ]
then
if [ "$3" = "" ]
then
echo "$3"
else
echo "OS not detected"
fi
else
echo "$2"
fi
else
echo "$1"
fi
}
# Main program
#-------------
DATE=`date +%Y%m%d%H%M%S`
execnodisplay "nmap -sP $1 -oN /tmp/nmap-up.$DATE" "Scan IP address range $1"
cat > /tmp/nmaptonagios-$DATE.cfg << EOF
#
# Generated by nmaptonagios.sh version $VERSION
#
EOF
cat /tmp/nmap-up.$DATE | grep -i "Nmap scan report for" | awk '{ print $5 }' | while read host
do
# Get host informations
execnodisplay "nmap -sV -O $host -oN /tmp/nmap-$host.$DATE" "Scan host $host"
OS_DETAIL=`grep -i "OS details: " /tmp/nmap-$host.$DATE | cut -d\: -f2-`
OS_RUNNING=`grep -i "Running: " /tmp/nmap-$host.$DATE | cut -d\: -f2-`
OS_SERVICE=`grep -i "Service Info: " /tmp/nmap-$host.$DATE | cut -d\: -f3-`
OS_SUMMARY=`displaynmapos "$OS_DETAIL" "$OS_RUNNING" "$OS_SERVICE"`
# Check for template (can be addapted to your Nagios configuration)
if [ `expr "$OS_SUMMARY" : ".*[Ll]inux.*"` != 0 ]
then
OS_TEMPLATE="linux-server"
elif [ `expr "$OS_SUMMARY" : ".*[Bb][Ss][Dd].*"` != 0 ]
then
# No BSD template by default in Nagios, use the Linux one
OS_TEMPLATE="linux-server"
elif [ `expr "$OS_SUMMARY" : ".*Mac\ OS.*"` != 0 ]
then
# No Mac OS X template by default in Nagios, use the BSD (so Linux) one
OS_TEMPLATE="linux-server"
elif [ `expr "$OS_SUMMARY" : ".*[W]indows.*"` != 0 ]
then
OS_TEMPLATE="windows-server"
elif [ `expr "$OS_SUMMARY" : ".*[Ss]witch.*"` != 0 ]
then
OS_TEMPLATE="generic-switch"
else
OS_TEMPLATE="generic-host"
fi
# Generate host in the Nagios .cfg file
cat >> /tmp/nmaptonagios-$DATE.cfg << EOF
##############################################################################
# Host: $host
# $OS_SUMMARY
define host{
use $OS_TEMPLATE
host_name $host
alias $host
address $host
}
EOF
# Generate service in the Nagios .cfg file
# for service in `cat /tmp/nmap-$host.$DATE | egrep "^.*/[tcp|udp].*open.*"`
cat /tmp/nmap-$host.$DATE | egrep "^.*/[tcp|udp].*open.*" | while read service
do
# Get service informations
SERVICE_PORT=`echo $service | cut -d\/ -f1`
SERVICE_PROTO=`echo $service | cut -d\/ -f2 | cut -d" " -f1`
SERVICE_NAME=`echo $service | awk '{ print $3 }'`
SERVICE_DESCRIPTION="$service"
# Check if a specific Nagios plugin exist
# Otherwise check proto/port
SERVICE_CHECK="check_$SERVICE_PROTO!$SERVICE_PORT"
if [ -d "$NAGIOS_PLUGINS_PATH" ]
then
if [ -e "$NAGIOS_PLUGINS_PATH/check_$SERVICE_NAME" ]
then
SERVICE_CHECK="check_$SERVICE_NAME"
fi
fi
# Normalize Service entries
SERVICE_NAME=`echo $SERVICE_NAME | sed 's/[\?\/]/\ /g'`
cat >> /tmp/nmaptonagios-$DATE.cfg << EOF
# Service($host): $SERVICE_NAME
# $SERVICE_DESCRIPTION
define service{
use generic-service
host_name $host
service_description $SERVICE_NAME
check_command $SERVICE_CHECK
}
EOF
# Next service...
done
# Next host
cat >> /tmp/nmaptonagios-$DATE.cfg << EOF
EOF
done
echo "---------------------------------------------------------------"
echo "Nagios configuration file: /tmp/nmaptonagios-$DATE.cfg"
echo "---------------------------------------------------------------"
rm -f /tmp/nmap-*.$DATE
exit 0