Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aix-pm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gerhard Gonter
aix-pm
Commits
3f474e0b
Commit
3f474e0b
authored
1 month ago
by
Gerhard Gonter
Browse files
Options
Downloads
Patches
Plain Diff
utility module to perform repeated CIDR lookups
parent
686cb3fd
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/util/Util/CIDR.pm
+127
-0
127 additions, 0 deletions
modules/util/Util/CIDR.pm
modules/util/t_util_cidr.pl
+25
-0
25 additions, 0 deletions
modules/util/t_util_cidr.pl
with
152 additions
and
0 deletions
modules/util/Util/CIDR.pm
0 → 100644
+
127
−
0
View file @
3f474e0b
#!/usr/bin/perl
use
strict
;
package
Util::
CIDR
;
use
Net::
CIDR
;
__PACKAGE__
->
main
()
unless
caller
();
sub
new
{
my
$class
=
shift
;
my
$self
=
{
_cidr
=>
{},
_ips
=>
{}
};
bless
(
$self
,
$class
);
$self
->
set
(
@
_
);
$self
;
}
sub
set
{
my
$self
=
shift
;
my
%pars
=
@_
;
foreach
my
$par
(
keys
%pars
)
{
$self
->
{
$par
}
=
$pars
{
$par
};
}
$self
;
}
sub
add_cidr
{
my
$self
=
shift
;
my
$ip_block
=
shift
;
my
$comment
=
shift
;
my
$v
=
Net::CIDR::
cidrvalidate
(
$ip_block
);
return
undef
unless
(
defined
(
$v
)
&&
$ip_block
=~
m#/#
);
my
$cidr
=
$self
->
{
_cidr
};
my
$b
;
unless
(
defined
(
$b
=
$cidr
->
{
$ip_block
}))
{
$b
=
$cidr
->
{
$ip_block
}
=
{
ip_block
=>
$ip_block
};
}
if
(
$comment
)
{
# print __LINE__, " comment=[$comment] b: ", main::Dumper($b);
my
$c
;
unless
(
defined
(
$c
=
$b
->
{
comments
}))
{
$c
=
$b
->
{
comments
}
=
[]
}
push
(
@$c
=>
$comment
);
}
$b
;
}
sub
read_cidr_list
{
my
$self
=
shift
;
my
$fnm
=
shift
;
unless
(
open
(
FI
,
'
<:utf8
',
$fnm
))
{
print
__LINE__
,
"
can't read CIDR list from fnm=[
$fnm
]
\n
";
return
0
;
}
my
$cnt
=
0
;
L:
while
(
defined
(
my
$l
=
<
FI
>
))
{
chop
(
$l
);
next
L
if
(
$l
eq
''
||
$l
=~
m/^#/
);
my
(
$ip_block
,
$comment
)
=
split
('
',
$l
,
2
);
$self
->
add_cidr
(
$ip_block
,
$comment
);
$cnt
++
;
}
close
(
FI
);
$cnt
;
}
=head2 lookup
lookup IP address in cidr list and return structure pointing to the IP block and counter
=cut
sub
lookup
{
my
$self
=
shift
;
my
$ip
=
shift
;
if
(
exists
(
$self
->
{
_ips
}
->
{
$ip
}))
{
my
$x
=
$self
->
{
_ips
}
->
{
$ip
};
$x
->
{
count
}
++
;
return
$x
;
}
# not yet looked up...
my
$x
=
$self
->
{
_ips
}
->
{
$ip
}
=
{
count
=>
1
};
my
$c
=
$self
->
{
_cidr
};
L:
foreach
my
$cidr
(
keys
%$c
)
{
if
(
Net::CIDR::
cidrlookup
(
$ip
,
$cidr
))
{
my
$b
=
$c
->
{
$cidr
};
$x
->
{
ip_block
}
=
$b
;
last
;
}
}
$x
;
}
sub
main
{
print
join
('
',
__FILE__
,
__LINE__
,
'
main: caller=[
'
.
caller
()
.
'
]
'),
"
\n
";
}
This diff is collapsed.
Click to expand it.
modules/util/t_util_cidr.pl
0 → 100755
+
25
−
0
View file @
3f474e0b
#!/usr/bin/perl
use
lib
'
.
';
use
Util::
CIDR
;
use
Data::
Dumper
;
$
Data::Dumper::
Indent
=
1
;
$
Data::Dumper::
Sortkeys
=
1
;
my
$uc
=
Util::
CIDR
->
new
();
$uc
->
add_cidr
('
111.121.0.0/16
',
'
blocked dv05
');
$uc
->
add_cidr
('
111.122.0.0/16
',
'
just looking
');
$uc
->
add_cidr
('
131.130.0.0/16
',
'
UNIVIE
');
$uc
->
add_cidr
('
85.208.96.0/24
',
'
US crawlers
');
foreach
my
$ip
(
qw(111.121.2.3 131.130.2.194 84.83.32.81 85.208.96.212 1.2.3.4)
)
{
my
$res
=
$uc
->
lookup
(
$ip
);
print
__LINE__
,
"
ip=[
$ip
] res:
",
Dumper
(
$res
);
}
print
__LINE__
,
"
uc:
",
Dumper
(
$uc
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment