Skip to content

Commit d01382a

Browse files
authored
Merge pull request #1163 from OCSInventory-NG/ipdiscover_on_server
Add ipdiscover scan to server
2 parents 0523844 + 363b700 commit d01382a

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

ipdiscover-util.pl

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
my $xml;
4343
my $cache;
4444
my $path;
45+
#Launch IpDiscover
46+
my $isNet;
47+
my $scantype;
48+
49+
my @networks = ();
4550
#Default values for database connection
4651
#
4752
my $dbhost = 'localhost';
@@ -96,6 +101,15 @@
96101
die "Invalid address => [IP/MASK]. Abort...\n" unless $1=~/^(\d{1,3}(?:\.\d{1,3}){3})\/(.+)$/;
97102
$iptarget = $1;
98103
$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;
99113
}else{
100114
print <<EOF;
101115
Usage :
@@ -112,6 +126,9 @@
112126
-u=xxxx user (default ocs)
113127
-h=xxxx host (default localhost)
114128
-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)
115132
116133
EOF
117134
die "Invalid options. Abort..\n";
@@ -194,6 +211,81 @@
194211
exit(0);
195212
}
196213

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+
197289
#Host subnet
198290
my $network;
199291
#Subnet mask in binary format

0 commit comments

Comments
 (0)