Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 635f0f4

Browse files
committed
added linux support for router and DNS
1 parent e1fb17e commit 635f0f4

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

ip_utils/ip_list.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
require 'rest-client'
66
require 'socket'
7+
require 'os'
78

89
def network_info
910
# Grabs public WAN address.
@@ -23,29 +24,43 @@ def network_info
2324
exit(1)
2425
end
2526

26-
# Greps scutil for DNS server.
27+
# Greps either scutil or resolv.conf for DNS server.
2728
begin
28-
dns = %x[scutil --dns | grep nameserver | head -1 | awk '{print$3}']
29+
if OS.linux?
30+
dns = %x[cat /etc/resolv.conf | grep nameserver | head -1 | awk '{print$2}']
31+
elsif OS.mac?
32+
dns = %x[scutil --dns | grep nameserver | head -1 | awk '{print$3}']
33+
else
34+
puts "OS not supported!"
35+
exit(1)
36+
end
2937
rescue
3038
puts "Can't find DNS"
3139
exit(1)
3240
end
3341

34-
# Greps netstat for router address.
42+
# Greps netstat or ip route for router address.
3543
begin
36-
router = %x[netstat -rn | grep default | head -1 | awk '{print$2}']
44+
if OS.linux?
45+
router = %x[ip route | grep default | head -1 | awk '{print$3}']
46+
elsif OS.mac?
47+
router = %x[netstat -rn | grep default | head -1 | awk '{print$2}']
48+
else
49+
puts "OS not supported!"
50+
exit(1)
51+
end
3752
rescue
3853
puts "Can't find Router"
3954
exit(1)
4055
end
4156

4257
puts
43-
puts "======================"
58+
puts "======================="
4459
puts "| WAN: #{response}"
4560
puts "| LAN(s): #{lans.join(', ')}"
4661
puts "| ROUTER: #{router}"
4762
puts "| DNS: #{dns}"
48-
puts "======================"
63+
puts "======================="
4964
puts
5065
end
5166

0 commit comments

Comments
 (0)