netspace 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/netspace +45 -0
- data/lib/netspace.rb +48 -0
- data/lib/netspace/subnet.rb +44 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 35713b14adae18c28c9b8acaf2665d4668f130d9
|
4
|
+
data.tar.gz: 0740b921200b7893d87e3d9207ae9d5e24719cc1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1715fd297d33c2fdcdbb2d5d1a4192bf95cf50e6244ff3d882863203f15c0ca717bae68cd67e7208a3b5ff0ffb6ab0a084f5636cd3af507efec890120c579675
|
7
|
+
data.tar.gz: 23ecc4e6b1c3bd5b22f4844e4bcbe1154b5badfc08f6c02df784c54112e0e7d953141fd1041c8839a2f70bf4cf412602ba7712a45e17cec53ccb8fd2b3b07670
|
data/bin/netspace
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#-*- mode: ruby -*-
|
3
|
+
|
4
|
+
require 'netspace'
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
if ARGV.length != 1
|
8
|
+
puts
|
9
|
+
puts " usage: netspace <subnets>"
|
10
|
+
puts
|
11
|
+
puts " <subnets> File containing CIDR notated subnets"
|
12
|
+
puts
|
13
|
+
puts " Matches STDIN for IPs contained in the subnets provided in the"
|
14
|
+
puts " specified input file. I.e., acts as a CIDR-aware IP grep."
|
15
|
+
puts
|
16
|
+
exit 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_ips(line)
|
20
|
+
ips = nil
|
21
|
+
begin
|
22
|
+
ips = line.scan(/\b([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\b/)
|
23
|
+
rescue
|
24
|
+
ips = []
|
25
|
+
end
|
26
|
+
return [] unless ips.length > 0
|
27
|
+
return ips.flatten
|
28
|
+
end
|
29
|
+
|
30
|
+
nets = NetSpace::load(ARGV[0])
|
31
|
+
|
32
|
+
STDIN.each_line do |line|
|
33
|
+
out = false
|
34
|
+
hits = Set.new
|
35
|
+
get_ips(line).each do |ip|
|
36
|
+
nets.each do |net|
|
37
|
+
hits << ip if net.in? ip
|
38
|
+
end
|
39
|
+
end
|
40
|
+
hits.each do |ip|
|
41
|
+
line.gsub!(/\b#{ip}\b/, "\x1b[35;1m#{ip}\x1b[0m")
|
42
|
+
out = true
|
43
|
+
end
|
44
|
+
puts line if out
|
45
|
+
end
|
data/lib/netspace.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'netspace/subnet'
|
2
|
+
|
3
|
+
module NetSpace
|
4
|
+
|
5
|
+
#
|
6
|
+
# Load a file containing CIDR subnet definitions, one per
|
7
|
+
# line. The result will be an array of Subnet objects.
|
8
|
+
|
9
|
+
def self.load(file)
|
10
|
+
NetSpace::parse(File.read(file))
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
# Parse a string containing CIDR subnet definitions, one
|
15
|
+
# per line. The result will be an array of Subnet objects.
|
16
|
+
#
|
17
|
+
|
18
|
+
def self.parse(string)
|
19
|
+
nets = string.split(/\n/).map do |line|
|
20
|
+
begin
|
21
|
+
line.strip!
|
22
|
+
line =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+)$/
|
23
|
+
Subnet.new($1, $2.to_i)
|
24
|
+
rescue
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return nets - [nil]
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Utility for converting a string IP (e.g. "127.0.0.1") to
|
33
|
+
# it's 32-bit integer equivalent (e.g., 2130706433).
|
34
|
+
#
|
35
|
+
|
36
|
+
def self.ip2num(ip)
|
37
|
+
return ip.split(/\./).map {|i| i.to_i}.pack("C*").unpack("N")[0]
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Utility for converting a 32-bit integer IP (e.g. 2130706433)
|
42
|
+
# to it's string equivalent (e.g., "127.0.0.1").
|
43
|
+
#
|
44
|
+
|
45
|
+
def self.num2ip(num)
|
46
|
+
return [num].pack("N").unpack("C*").join(".")
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module NetSpace
|
2
|
+
|
3
|
+
#
|
4
|
+
# Class representing an IP subnet. At its core, it's a base
|
5
|
+
# IP and a netmask, both converted to 32-bit integers for fast
|
6
|
+
# comparisons.
|
7
|
+
#
|
8
|
+
|
9
|
+
class Subnet
|
10
|
+
attr_accessor :ip, :ip32, :bits, :mask, :mask32
|
11
|
+
|
12
|
+
def initialize(ip, bits)
|
13
|
+
@ip = ip
|
14
|
+
@bits = bits
|
15
|
+
@ip32 = NetSpace::ip2num(ip)
|
16
|
+
@mask32 = ~(2 ** (32 - @bits) - 1)
|
17
|
+
@mask = NetSpace::num2ip(@mask32)
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Test if indicated IP address resides within the boundaries
|
22
|
+
# of the subnet.
|
23
|
+
#
|
24
|
+
|
25
|
+
def in?(ip)
|
26
|
+
return NetSpace::ip2num(ip) & @mask32 == @ip32
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# Return a list of all IPs (as strings) that are legitimately
|
31
|
+
# members of the subnet.
|
32
|
+
#
|
33
|
+
|
34
|
+
def ips
|
35
|
+
is = []
|
36
|
+
ip = @ip32
|
37
|
+
while ip & @mask32 == @ip32
|
38
|
+
is << NetSpace::num2ip(ip)
|
39
|
+
ip += 1
|
40
|
+
end
|
41
|
+
return is
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: netspace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Stone
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Calculate with IPs and networks
|
14
|
+
email: yakovdk@gmail.com
|
15
|
+
executables:
|
16
|
+
- netspace
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/netspace.rb
|
21
|
+
- lib/netspace/subnet.rb
|
22
|
+
- bin/netspace
|
23
|
+
homepage: http://joshstone.us/netspace/
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.0.14.1
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Network Space Library
|
47
|
+
test_files: []
|