-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist-subnet
executable file
·124 lines (98 loc) · 2.66 KB
/
list-subnet
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
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to search for host entrys
#
use strict;
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d $opt_r);
getopts ('hdr');
my $debug = 0;
my $raw = 0;
$debug = 1 if (defined ($opt_d));
$raw = 1 if (defined ($opt_r));
if (defined ($ARGV[0]) and $ARGV[0] eq "-d") {
shift (@ARGV);
$debug = 1;
}
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my @searchfor = @ARGV;
if ($#searchfor == -1 or $opt_h) {
die ("Syntax: $0 [-dr] subnet/slash ...\n" .
" subnet is the subnet in slash notation (eg. 192.168.1.0/24)\n" .
" options :\n" .
" -d debug output\n" .
" -r raw mode, just IP address and hostnames, separated by semi-colon\n" .
"\n");
}
while ($searchfor[0]) {
my $subnet = shift (@searchfor);
if ($hostdb->is_valid_subnet ($subnet)) {
my @hosts = $hostdb->findhostbyiprange ($hostdb->get_netaddr ($subnet),
$hostdb->get_broadcast ($subnet));
my @subnets;
@subnets = $hostdb->findsubnetlongerprefix ($subnet);
if ($#subnets != -1) {
my $subnet;
foreach $subnet (@subnets) {
# interpolation
my $color = $subnet->htmlcolor () || 'NULL';
my $subnet_name = $subnet->subnet ();
my $short_desc = $subnet->short_description ();
my $desc = $subnet->description ();
my $id = $subnet->id ();
my $owner = $subnet->owner ();
my $profilelist = $subnet->profilelist ();
my $addresses = $subnet->addresses ();
my $netmask = $subnet->netmask ();
my $broadcast = $subnet->broadcast ();
if (! $raw) {
print (<<EOT);
$subnet_name :
Netmask $netmask
Broadcast $broadcast
Short description '$short_desc'
Description '$desc'
Id $id
Owner $owner
Profiles $profilelist
EOT
}
my @subnet_hosts = get_hosts_in_subnet ($subnet->subnet(), @hosts);
if (@subnet_hosts) {
my $num_hosts = scalar @subnet_hosts;
print (" $num_hosts hosts in subnet :\n\n") unless ($raw);
my $host;
my $fmt = " %-20s %s\n";
$fmt = "%s;%s\n" if ($raw);
foreach $host (@subnet_hosts) {
printf $fmt,
$host->ip (),
defined ($host->hostname ())?$host->hostname ():"NULL";
}
}
}
print ("\n") unless ($raw);
}
} else {
warn ("Ignoring invalid subnet '$subnet'\n");
}
}
sub get_hosts_in_subnet
{
my $subnet = shift;
my @hosts = @_;
my @result;
my $low = $hostdb->aton ($hostdb->get_netaddr ($subnet));
my $high = $hostdb->aton ($hostdb->get_broadcast ($subnet));
my $host;
foreach $host (@hosts) {
my $ip = $hostdb->aton ($host->ip ());
push (@result, $host) if ($ip >= $low and $ip <= $high);
}
return @result;
}