|
42 | 42 | my $xml; |
43 | 43 | my $cache; |
44 | 44 | my $path; |
| 45 | +#Launch IpDiscover |
| 46 | +my $isNet; |
| 47 | +my $scantype; |
| 48 | + |
| 49 | +my @networks = (); |
45 | 50 | #Default values for database connection |
46 | 51 | # |
47 | 52 | my $dbhost = 'localhost'; |
|
96 | 101 | die "Invalid address => [IP/MASK]. Abort...\n" unless $1=~/^(\d{1,3}(?:\.\d{1,3}){3})\/(.+)$/; |
97 | 102 | $iptarget = $1; |
98 | 103 | $masktarget = $2; |
| 104 | + }elsif($option=~/-network=(\S+)/){ |
| 105 | + die "Invalid address => [IP/MASK]. Abort...\n" unless $1=~/^(\d{1,3}(?:\.\d{1,3}){3})\/(.+)$/; |
| 106 | + push(@networks, "$1/$2"); |
| 107 | + $isNet = 1; |
| 108 | + }elsif($option=~/-scantype=(\S+)/){ |
| 109 | + if ($1 ne "ping" and $1 ne "nmap"){ |
| 110 | + die "Invalid address => [IP/MASK]. Abort...\n"; |
| 111 | + } |
| 112 | + $scantype = $1; |
99 | 113 | }else{ |
100 | 114 | print <<EOF; |
101 | 115 | Usage : |
|
112 | 126 | -u=xxxx user (default ocs) |
113 | 127 | -h=xxxx host (default localhost) |
114 | 128 | -s=xxxx socket (default from default mysql configuration) |
| 129 | +#SCAN OPTION |
| 130 | +-network=X.X.X.X/X (ex: 10.1.1.1/20)-> subnet to scan |
| 131 | +-scantype=xxxx (ping or nmap) tool to scan (default nmap) |
115 | 132 |
|
116 | 133 | EOF |
117 | 134 | die "Invalid options. Abort..\n"; |
|
194 | 211 | exit(0); |
195 | 212 | } |
196 | 213 |
|
| 214 | +############# |
| 215 | +# SERVER SCAN |
| 216 | +############# |
| 217 | +# |
| 218 | +if ($isNet){ |
| 219 | + print "\n########################\n"; |
| 220 | + print "Starting scan of subnets\n"; |
| 221 | + print "########################\n\n"; |
| 222 | + #Set default scan type to nmap |
| 223 | + if (!$scantype){ |
| 224 | + $scantype = "nmap"; |
| 225 | + } |
| 226 | + |
| 227 | + my $ips; |
| 228 | + my $ip; |
| 229 | + my $macAddr; |
| 230 | + my $name; |
| 231 | + my $str; |
| 232 | + |
| 233 | + for $ips (@networks){ |
| 234 | + #get subnet and mask |
| 235 | + my ($subnet, $mask) = split(/\//, $ips); |
| 236 | + $mask = _bintoascii($mask) if($mask=~/^\d\d$/); |
| 237 | + |
| 238 | + $dbh->do('DELETE FROM netmap WHERE NETID = ?', {}, $subnet); |
| 239 | + |
| 240 | + if ($scantype eq "nmap"){ #Scan with nmap |
| 241 | + $str = `nmap -sn $ips`; |
| 242 | + #get all values |
| 243 | + while ($str =~ /Nmap scan report for (\S+) ?(\((\S+)\))?(\n^.*\n^MAC Address: (\S+))?/gm) { |
| 244 | + if ($3) { |
| 245 | + $ip = $3; |
| 246 | + $name = $1; |
| 247 | + } else { |
| 248 | + $ip = $1; |
| 249 | + undef $name; |
| 250 | + } |
| 251 | + |
| 252 | + if ($5) { |
| 253 | + $macAddr = $5; |
| 254 | + } else { |
| 255 | + $macAddr = $ip; |
| 256 | + } |
| 257 | + |
| 258 | + print "Adding $ip\n"; |
| 259 | + |
| 260 | + #bdd insertion |
| 261 | + if ($name){ |
| 262 | + $dbh->do('INSERT IGNORE INTO netmap(IP,MAC,MASK,NETID,NAME) VALUES(?,?,?,?,?)', {}, $ip, $macAddr, $mask, $subnet, $name); |
| 263 | + } else { |
| 264 | + $dbh->do('INSERT IGNORE INTO netmap(IP,MAC,MASK,NETID) VALUES(?,?,?,?)', {}, $ip, $macAddr, $mask, $subnet); |
| 265 | + } |
| 266 | + } |
| 267 | + } elsif ($scantype eq "ping") { #scan with fping |
| 268 | + $_ = `fping -v`; |
| 269 | + |
| 270 | + if (/^fping:/){ |
| 271 | + foreach (`fping -g --quiet -a $ips`){ |
| 272 | + $ip = $macAddr = $_; |
| 273 | + |
| 274 | + print "Adding $ip\n"; |
| 275 | + |
| 276 | + $dbh->do('INSERT IGNORE INTO netmap(IP,MAC,MASK,NETID) VALUES(?,?,?,?) ', {}, $ip, $macAddr, $mask, $subnet); |
| 277 | + } |
| 278 | + } else { |
| 279 | + die "Please install fping to use ping for scanning or use -scantype=nmap.\n"; |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + print "\n#########################\n"; |
| 284 | + print "Finishing scan of subnets\n"; |
| 285 | + print "#########################\n\n"; |
| 286 | + exit(0); |
| 287 | +} |
| 288 | + |
197 | 289 | #Host subnet |
198 | 290 | my $network; |
199 | 291 | #Subnet mask in binary format |
|
0 commit comments