-
Notifications
You must be signed in to change notification settings - Fork 0
/
ficonfig
executable file
·182 lines (167 loc) · 5.6 KB
/
ficonfig
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env ruby
require 'pp'
# extend String
class String
def undent
gsub(/^.{#{slice(/^ +/).length}}/, '')
end
ANSI_ESC = Hash[
:reset => 0,
:bold => 1,
:dark => 2,
:italic => 3, # not widely implemented
:underline => 4,
:blink => 5,
:rapid_blink => 6, # not widely implemented
:negative => 7, # no reverse because of String#reverse
:concealed => 8,
:strikethrough => 9, # not widely implemented
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
:bg_black => 40,
:bg_red => 41,
:bg_green => 42,
:bg_yellow => 43,
:bg_blue => 44,
:bg_magenta => 45,
:bg_cyan => 46,
:bg_white => 47,
]
def ansi_escape(*keys)
str = to_s
keys.each do |key|
key = key.to_sym # ensure it is a symbol
if !ANSI_ESC.has_key?(key)
raise ArgumentError
end
str = "\e[#{ANSI_ESC[key]}m#{str}"
end
str + (0 < keys.size ? "\e[0m" : '')
end
def method_missing(meth, *args, &block)
begin
ansi_escape(meth)
rescue ArgumentError
raise NoMethodError
end
end
end
module Ficonfig
VERSION = "0.1"
INDENT = " "
REGEX_HEX = "[\\dA-Fa-f]"
REGEX_HEX_1 = "(?:#{REGEX_HEX}){2}"
REGEX_HWADDR = "(?:#{REGEX_HEX_1}\\:){5}#{REGEX_HEX_1}"
REGEX_OCTAL_1 = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"
REGEX_HEX_4 = "0x(?:#{REGEX_HEX}){8}"
REGEX_IPV4 = "(?:#{REGEX_OCTAL_1}\.){3}#{REGEX_OCTAL_1}"
REGEX_IPV6 = "(?:(?:#{REGEX_HEX}{0,4})\:){7}#{REGEX_HEX}{0,4}"
def Ficonfig.usage
puts <<-EOS.undent
ficonfig: #{VERSION}
ifconfig beautified
Usage:
ficonfig [parameters] [interface]
--no-color
Will show the output without colors.
EOS
end # def
class Ficonfig::Fi
def initialize(str)
# Name (mandatory)
@name = str[/^[^\s:]+/]
# HWaddr
@hwaddr = str[/#{REGEX_HWADDR}/]
# IP (v4) addresses
@ipv4 = str[/inet(?:\s*addr)?\:?\s*(#{REGEX_IPV4})/i, 1]
@maskv4 = str[/mask\:?\s*((?:#{REGEX_IPV4})|(?:#{REGEX_HEX_4}))/i, 1] || ''
@bcastv4 = str[/b(?:road)?cast\:?\s*(#{REGEX_IPV4})/i, 1] || ''
# IP (v6)
@ipv6 = str[/#{REGEX_IPV6}/]
end # def
def to_s
text = @name.magenta + "\n"
text += INDENT + "HWaddr: " + @hwaddr.cyan + "\n" if @hwaddr
text += INDENT + "IP: " + @ipv4.yellow + " Mask: " + @maskv4.yellow + " Bcast: " + @bcastv4.yellow + "\n" if @ipv4
text += INDENT + "IPv6: " + @ipv6.yellow + "\n" if @ipv6
text + "\n"
end # def
end # class
def Ficonfig.ficonfig
if ARGV.size > 0
puts "ficonfig does not support arguments (yet)"
# TODO fix this
system "ifconfig #{ARGV}"
return
end # if
plain = %x{"ifconfig"}
# Split the interfaces
ifs = []
plain.split(/\n\b/).each do |ln|
ifs << Fi.new(ln)
end # each
puts ifs.to_s
end # def
end # module
# Parse some special arguments
case ARGV.first
when '-h', '--help', '--usage', 'help'
Ficonfig::usage
exit 0
when '--version', '-v'
puts Ficonfig::VERSION
exit 0
end
Ficonfig::ficonfig
# Example output of ifconfig below
__END__
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 127.0.0.1 netmask 0xff000000
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:23:df:9f:b7:02
media: autoselect
status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:24:36:b0:b7:4f
inet6 fe80::224:36ff:feb0:b74f%en1 prefixlen 64 scopeid 0x5
inet 192.168.2.101 netmask 0xffffff00 broadcast 192.168.2.255
inet6 2002:d576:1d5a:1234:224:36ff:feb0:b74f prefixlen 64 autoconf
media: autoselect
status: active
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
lladdr 00:23:df:ff:fe:9f:b7:02
media: autoselect <full-duplex>
status: inactive
vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:50:56:c0:00:08
inet 192.168.207.1 netmask 0xffffff00 broadcast 192.168.207.255
vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:50:56:c0:00:01
inet 192.168.58.1 netmask 0xffffff00 broadcast 192.168.58.255
eth0 Link encap:Ethernet HWaddr 00:E0:7D:B4:57:5D
inet addr:10.0.0.150 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::2e0:7dff:feb4:575d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7942687 errors:0 dropped:0 overruns:0 frame:0
TX packets:11226689 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1546161234 (1.4 GiB) TX bytes:2389003399 (2.2 GiB)
Interrupt:16 Base address:0x1000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:22356 errors:0 dropped:0 overruns:0 frame:0
TX packets:22356 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1441595 (1.3 MiB) TX bytes:1441595 (1.3 MiB)