ipscan 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61800893649005026086244f7edc773894947869
4
- data.tar.gz: fdf8e90e53edfe30ad48198a79fbf76afb843e93
3
+ metadata.gz: 28f0852ab8dc39016f37d4e781441b5c74e27d98
4
+ data.tar.gz: 25e3f199daaa1504e888e49ba78c5479c02c2382
5
5
  SHA512:
6
- metadata.gz: d18d2d37e7ff94760c4c98d3422c2459201176aea8044f2cc542a4c7bff1259038f5f29a84ad2e8b35bea4ade2b744a5e3b7d634d60d30268eb101a4f0fb650f
7
- data.tar.gz: 14acc5b6cc5ac6df30015384ae0dfd23c1d0ef00c912c3224889dd3440fcc1a4fb6f363aa94e278e7af6a2cffa6b9b80015d532e9cf666f7455b290ba701d847
6
+ metadata.gz: 985db2876f5d07380465e5b2403e0d8dcc6a9613dbe54507748b9e3bbada9ddf0e878fe69b6927436e07bb101165b0271f27a061e0df2dd1c9cc3549c6af2ca1
7
+ data.tar.gz: b8bdef4f08be552a0a601da6867a1eafb8a9152243448c6c8ca3cf2d75cc1a2dde6bbf3ef4df9fb042f5d1d2d7aec6eb29ca3fdd21ed565eae139a5b73255625
data/lib/ipscan.rb CHANGED
@@ -10,40 +10,97 @@ require 'ruby-progressbar'
10
10
 
11
11
  module IPScan
12
12
 
13
- def self.scan(network_addr = nil)
14
- selfip = IPSocket::getaddress(Socket::gethostname)
15
- unless network_addr
16
- network = selfip.split(".")[0..2].join(".")
13
+ def self.arp(ip)
14
+ case RbConfig::CONFIG['host_os']
15
+ when /linux/i
16
+ line = `arp #{ip}`.split("\n").last.split(" ")
17
+ return {name: line[0], mac: line[2]}
18
+ when /bsd|osx|mach|darwin/i
19
+ line = `arp #{ip}`.split("\n").last.split(" ")
20
+ return {name: line[0], mac: line[3]}
21
+ else
22
+ raise "host_os=#{RbConfig::CONFIG['host_os']} are not supported"
17
23
  end
18
- puts "check #{network}.0 ~ #{network}.254"
24
+ end
19
25
 
26
+ def self.get_range_addresses(network_addr, start_host, last_host)
20
27
  addrs = []
21
- 0.upto(254) do |i|
22
- addrs << "#{network}.#{i}"
28
+ start_host.upto(last_host) do |i|
29
+ addrs << "#{network_addr}.#{i}"
23
30
  end
31
+ addrs
32
+ end
33
+
34
+ def self.get_netbios_info(range)
35
+ nbtinfo = {}
36
+ body_started = false
37
+ result = `nbtscan #{range}`
38
+ result.each_line do |line|
39
+ if line.index("-----------")
40
+ body_started = true
41
+ next
42
+ end
43
+ next unless body_started
44
+ h = line.split(" ")
45
+ ip = h[0]
46
+ netbios_name = h[1]
47
+ nbtinfo[ip] = {netbios_name: netbios_name}
48
+ end
49
+ nbtinfo
50
+ end
51
+
52
+ def self.scan(renge)
53
+ unless renge.index("-")
54
+ renge = renge + "-254"
55
+ end
56
+ scan_result = renge.scan(/(.*)-(.*)/)[0]
57
+ network_addr = scan_result[0].split(".")[0..2].join(".")
58
+ start_host = scan_result[0].split(".")[3].to_i
59
+ last_host = scan_result[1].to_i
60
+ host_total = (start_host..last_host).size
61
+ puts "check #{network_addr}.#{start_host} ~ #{network_addr}.#{last_host}"
62
+
63
+ netbios_info = {}
64
+ if `which nbtscan` != ""
65
+ netbios_info = get_netbios_info(range)
66
+ end
67
+
68
+ addrs = get_range_addresses(network_addr, start_host, last_host)
24
69
 
25
70
  results = []
26
- progressbar = ProgressBar.create(:total => 255)
71
+ progressbar = ProgressBar.create(:total => host_total)
27
72
  Parallel.each(addrs, in_threads: 100) do |addr|
28
73
  data = {:ip => addr}
29
- if addr == selfip
30
- data[:name] = "(localhost)"
31
- data[:mac] = "(localhost)"
32
- results << data
33
- else
34
- pinger = Net::Ping::External.new(addr)
35
- if pinger.ping?
36
- arp_resp = `arp #{addr}`.split(" ")
37
- data[:name] = arp_resp[0]
38
- data[:mac] = arp_resp[3]
39
- results << data
74
+ data.merge!(arp(addr))
75
+ pinger = Net::Ping::External.new(addr)
76
+ if pinger.ping?
77
+ arp_line = `arp #{addr}`
78
+ arp_hash = arp_line.split(" ")
79
+ if arp_hash[0] == "?"
80
+ data[:name] = "<unknown>"
81
+ else
82
+ data[:name] = arp_hash[0]
83
+ end
84
+
85
+ if arp_line.index("no entry")
86
+ data[:mac] = "<no entry> "
87
+ else
88
+ data[:mac] = arp_hash[3]
89
+ end
90
+
91
+ if netbios_info[addr]
92
+ data[:netbios_name] = netbios_info[addr][:netbios_name]
93
+ else
94
+ data[:netbios_name] = "<unknown>"
40
95
  end
96
+ results << data
41
97
  end
42
98
  progressbar.increment
43
99
  end
44
100
 
101
+ puts "IP Address \tMac Address \tName \tNetBIOS Name"
45
102
  results.each do |res|
46
- puts "#{res[:ip]}\t#{res[:mac]}\t#{res[:name]}"
103
+ puts "#{res[:ip]} \t#{res[:mac]} \t#{res[:name]} \t#{res[:netbios_name]}"
47
104
  end
48
105
  end
49
106
  end
data/lib/ipscan/cli.rb CHANGED
@@ -4,9 +4,9 @@ require 'thor'
4
4
 
5
5
  module IPScan
6
6
  class CLI < Thor
7
- desc "scan [NETWORK_ADDR]", "scan LAN devices. ex: scan 192.168.1"
8
- def scan(network_addr=nil)
9
- IPScan.scan(network_addr)
7
+ desc "range [SCAN_RANGE]", "scan 192.168.1.0-254"
8
+ def range(range)
9
+ IPScan.scan(range)
10
10
  end
11
11
 
12
12
  default_task :scan
@@ -1,3 +1,3 @@
1
1
  module IPScan
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipscan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - k-yamada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-18 00:00:00.000000000 Z
11
+ date: 2014-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler