network-utility 1.1.4

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.
Files changed (179) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +2 -0
  3. data/document/acl-M6000-16E.md +27 -0
  4. data/document/acl-M6000-18S.md +27 -0
  5. data/document/acl-M6000-8.md +27 -0
  6. data/document/acl-M6000-8E.md +27 -0
  7. data/document/acl-MA5200G-8.md +204 -0
  8. data/document/acl-ME60-16.md +177 -0
  9. data/document/acl-ME60-X16.md +177 -0
  10. data/document/acl-NE40E-X16.md +204 -0
  11. data/document/acl-NE40E-X16A.md +204 -0
  12. data/document/acl-NE40E-X8.md +204 -0
  13. data/document/acl-NE40E.md +204 -0
  14. data/document/acl-NE5000E-20.md +25 -0
  15. data/document/acl-NE5000E-X16.md +25 -0
  16. data/document/acl-NE5000E-X16A.md +25 -0
  17. data/document/acl-NE80E.md +204 -0
  18. data/document/bgp-CR16010H-F.md +25 -0
  19. data/document/bgp-CR16018-F.md +25 -0
  20. data/document/bgp-CR19000-20.md +25 -0
  21. data/document/bgp-M6000-16E.md +25 -0
  22. data/document/bgp-M6000-18S.md +25 -0
  23. data/document/bgp-M6000-8.md +25 -0
  24. data/document/bgp-M6000-8E.md +25 -0
  25. data/document/bgp-MA5200G-8.md +25 -0
  26. data/document/bgp-ME60-16.md +25 -0
  27. data/document/bgp-ME60-X16.md +25 -0
  28. data/document/bgp-NE40E-X16.md +25 -0
  29. data/document/bgp-NE40E-X16A.md +25 -0
  30. data/document/bgp-NE40E-X8.md +25 -0
  31. data/document/bgp-NE40E.md +25 -0
  32. data/document/bgp-NE5000E-20.md +25 -0
  33. data/document/bgp-NE5000E-X16.md +25 -0
  34. data/document/bgp-NE5000E-X16A.md +25 -0
  35. data/document/bgp-NE80E.md +25 -0
  36. data/document/config.md +1153 -0
  37. data/document/document.rb +27 -0
  38. data/document/if-ALCATEL7750.md +48 -0
  39. data/document/if-C7609.md +47 -0
  40. data/document/if-CR16010H-F.md +300 -0
  41. data/document/if-CR16018-F.md +300 -0
  42. data/document/if-CR19000-20.md +300 -0
  43. data/document/if-CRS-16.md +47 -0
  44. data/document/if-CX600-X16A.md +349 -0
  45. data/document/if-CX600-X8A.md +349 -0
  46. data/document/if-M6000-16E.md +652 -0
  47. data/document/if-M6000-18S.md +846 -0
  48. data/document/if-M6000-8.md +653 -0
  49. data/document/if-M6000-8E.md +653 -0
  50. data/document/if-MA5200G-8.md +357 -0
  51. data/document/if-ME60-16.md +357 -0
  52. data/document/if-ME60-X16.md +357 -0
  53. data/document/if-NE40E-X16.md +357 -0
  54. data/document/if-NE40E-X16A.md +357 -0
  55. data/document/if-NE40E-X8.md +357 -0
  56. data/document/if-NE40E.md +357 -0
  57. data/document/if-NE5000E-20.md +357 -0
  58. data/document/if-NE5000E-X16.md +357 -0
  59. data/document/if-NE5000E-X16A.md +357 -0
  60. data/document/if-NE8000E-X8.md +357 -0
  61. data/document/if-NE80E.md +357 -0
  62. data/document/if-NE8100-X8.md +357 -0
  63. data/document/if-Nokia7750.md +48 -0
  64. data/document/if-T8000-18.md +653 -0
  65. data/document/if-V6000.md +663 -0
  66. data/document/if-VNE9000.md +357 -0
  67. data/document/if-ZXCTN9000-18EA.md +762 -0
  68. data/document/if-ZXCTN9000-8EA.md +762 -0
  69. data/document/lic-M6000-16E.md +28 -0
  70. data/document/lic-M6000-18S.md +28 -0
  71. data/document/lic-M6000-8.md +28 -0
  72. data/document/lic-M6000-8E.md +28 -0
  73. data/document/lic-MA5200G-8.md +27 -0
  74. data/document/lic-ME60-16.md +27 -0
  75. data/document/lic-ME60-X16.md +27 -0
  76. data/document/lic-NE40E-X16.md +27 -0
  77. data/document/lic-NE40E-X16A.md +27 -0
  78. data/document/lic-NE40E-X8.md +27 -0
  79. data/document/log-M6000-16E.md +36 -0
  80. data/document/log-M6000-18S.md +36 -0
  81. data/document/log-M6000-8.md +36 -0
  82. data/document/log-M6000-8E.md +36 -0
  83. data/document/log-MA5200G-8.md +61 -0
  84. data/document/log-ME60-16.md +61 -0
  85. data/document/log-ME60-X16.md +61 -0
  86. data/document/log-NE40E-X16.md +61 -0
  87. data/document/log-NE40E-X16A.md +61 -0
  88. data/document/log-NE40E-X8.md +61 -0
  89. data/document/log-NE40E.md +61 -0
  90. data/document/log-NE5000E-20.md +61 -0
  91. data/document/log-NE5000E-X16.md +61 -0
  92. data/document/log-NE5000E-X16A.md +61 -0
  93. data/document/log-NE80E.md +61 -0
  94. data/document/nat-CR16010H-F.md +27 -0
  95. data/document/nat-CR16018-F.md +27 -0
  96. data/document/nat-M6000-16E.md +27 -0
  97. data/document/nat-M6000-18S.md +27 -0
  98. data/document/nat-M6000-8.md +27 -0
  99. data/document/nat-M6000-8E.md +27 -0
  100. data/document/nat-ME60-16.md +44 -0
  101. data/document/nat-ME60-X16.md +44 -0
  102. data/document/nat-NE40E-X16.md +44 -0
  103. data/document/nat-NE40E-X16A.md +44 -0
  104. data/document/policy-M6000-16E.md +13 -0
  105. data/document/policy-M6000-18S.md +13 -0
  106. data/document/policy-M6000-8.md +13 -0
  107. data/document/policy-M6000-8E.md +13 -0
  108. data/document/policy-MA5200G-8.md +87 -0
  109. data/document/policy-ME60-16.md +87 -0
  110. data/document/policy-ME60-X16.md +87 -0
  111. data/document/policy-NE40E-X16.md +87 -0
  112. data/document/policy-NE40E-X16A.md +87 -0
  113. data/document/policy-NE40E-X8.md +87 -0
  114. data/document/policy-NE40E.md +87 -0
  115. data/document/policy-NE5000E-20.md +87 -0
  116. data/document/policy-NE5000E-X16.md +87 -0
  117. data/document/policy-NE5000E-X16A.md +87 -0
  118. data/document/policy-NE80E.md +87 -0
  119. data/document/pool-M6000-16E.md +126 -0
  120. data/document/pool-M6000-18S.md +126 -0
  121. data/document/pool-M6000-8.md +126 -0
  122. data/document/pool-M6000-8E.md +126 -0
  123. data/document/pool-MA5200G-8.md +138 -0
  124. data/document/pool-ME60-16.md +138 -0
  125. data/document/pool-ME60-X16.md +138 -0
  126. data/document/pool-NE40E-X16.md +138 -0
  127. data/document/pool-NE40E-X16A.md +138 -0
  128. data/document/pool-NE40E-X8.md +138 -0
  129. data/document/pool-NE40E.md +138 -0
  130. data/document/pool-NE80E.md +138 -0
  131. data/document/static-CR16010H-F.md +60 -0
  132. data/document/static-CR16018-F.md +60 -0
  133. data/document/static-CR19000-20.md +60 -0
  134. data/document/static-CX600-X16A.md +26 -0
  135. data/document/static-CX600-X8A.md +26 -0
  136. data/document/static-M6000-16E.md +104 -0
  137. data/document/static-M6000-18S.md +104 -0
  138. data/document/static-M6000-8.md +104 -0
  139. data/document/static-M6000-8E.md +104 -0
  140. data/document/static-MA5200G-8.md +82 -0
  141. data/document/static-ME60-16.md +82 -0
  142. data/document/static-ME60-X16.md +82 -0
  143. data/document/static-NE40E-X16.md +82 -0
  144. data/document/static-NE40E-X16A.md +81 -0
  145. data/document/static-NE40E-X8.md +82 -0
  146. data/document/static-NE40E.md +67 -0
  147. data/document/static-NE5000E-20.md +67 -0
  148. data/document/static-NE5000E-X16.md +67 -0
  149. data/document/static-NE5000E-X16A.md +67 -0
  150. data/document/static-NE8000E-X8.md +26 -0
  151. data/document/static-NE80E.md +67 -0
  152. data/document/static-NE8100-X8.md +26 -0
  153. data/document/static-T8000-18.md +34 -0
  154. data/document/static-ZXCTN9000-18EA.md +34 -0
  155. data/document/static-ZXCTN9000-8EA.md +34 -0
  156. data/document/system-M6000-16E.md +75 -0
  157. data/document/system-M6000-18S.md +76 -0
  158. data/document/system-M6000-8.md +75 -0
  159. data/document/system-M6000-8E.md +75 -0
  160. data/document/system-MA5200G-8.md +51 -0
  161. data/document/system-ME60-16.md +51 -0
  162. data/document/system-ME60-X16.md +51 -0
  163. data/document/system-NE40E-X16.md +51 -0
  164. data/document/system-NE40E-X16A.md +51 -0
  165. data/document/system-NE40E-X8.md +51 -0
  166. data/document/system-NE40E.md +51 -0
  167. data/document/system-NE5000E-20.md +51 -0
  168. data/document/system-NE5000E-X16.md +51 -0
  169. data/document/system-NE5000E-X16A.md +51 -0
  170. data/document/system-NE80E.md +51 -0
  171. data/document/telnet.md +332 -0
  172. data/network.rb +24 -0
  173. data/utility/ipv4_address.rb +266 -0
  174. data/utility/ipv6_address.rb +201 -0
  175. data/utility/mac_address.rb +77 -0
  176. data/utility/netmerge.rb +46 -0
  177. data/utility/route.rb +79 -0
  178. data/utility/whitelist.rb +21 -0
  179. metadata +258 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 23f18332854c170df2b04dd20be2e6f6879ef3e934a98745008c147633973d2d
4
+ data.tar.gz: a9ac6e6461595415f2417b337d0b7cac064bed839f0fd5100b455c65c82bbc85
5
+ SHA512:
6
+ metadata.gz: f06a78c3d35a93a3cff081c2cd472dec4c05fe2467262c3fcfb62dda916d292af34f1968b1e5115f84de4e590444069a6ec3e7f9805a17f0ed1de2001acd001e
7
+ data.tar.gz: 965189ade7f96895262c02b63b7c7b7b2cf09b23fcd4744f23c70797a2534c71714cfa1361195830e14d789842f625b483f39fd4e5121b9654e3f0c10d57c8c0
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+
2
+ # network utility
@@ -0,0 +1,27 @@
1
+
2
+ # M6000-16E 访问控制列表
3
+
4
+ ```ruby
5
+ @sign << ['M6000-16E', 'ACL']
6
+
7
+ module M6000_16E
8
+ module_function
9
+
10
+ def ACL config
11
+ table = {}
12
+ name = nil
13
+ config.split("\n").each do|line|
14
+ name = line.split('ipv4-access-list')[1].strip if line.include?('ipv4-access-list')
15
+ name = line.split('ipv6-access-list')[1].strip if line.include?('ipv6-access-list')
16
+ name = line.split('link-access-list')[1].strip if line.include?('link-access-list')
17
+ table[name] ||= {}
18
+ if line.include?('rule')
19
+ rule = line.split(' ')
20
+ table[name][rule[1].to_i] = rule[2..-1]
21
+ end
22
+ end
23
+ table.delete(nil)
24
+ return table
25
+ end
26
+ end
27
+ ```
@@ -0,0 +1,27 @@
1
+
2
+ # M6000-18S 访问控制列表
3
+
4
+ ```ruby
5
+ @sign << ['M6000-18S', 'ACL']
6
+
7
+ module M6000_18S
8
+ module_function
9
+
10
+ def ACL config
11
+ table = {}
12
+ name = nil
13
+ config.split("\n").each do|line|
14
+ name = line.split('ipv4-access-list')[1].strip if line.include?('ipv4-access-list')
15
+ name = line.split('ipv6-access-list')[1].strip if line.include?('ipv6-access-list')
16
+ name = line.split('link-access-list')[1].strip if line.include?('link-access-list')
17
+ table[name] ||= {}
18
+ if line.include?('rule')
19
+ rule = line.split(' ')
20
+ table[name][rule[1].to_i] = rule[2..-1]
21
+ end
22
+ end
23
+ table.delete(nil)
24
+ return table
25
+ end
26
+ end
27
+ ```
@@ -0,0 +1,27 @@
1
+
2
+ # M6000-8 访问控制列表
3
+
4
+ ```ruby
5
+ @sign << ['M6000-8', 'ACL']
6
+
7
+ module M6000_8
8
+ module_function
9
+
10
+ def ACL config
11
+ table = {}
12
+ name = nil
13
+ config.split("\n").each do|line|
14
+ name = line.split('ipv4-access-list')[1].strip if line.include?('ipv4-access-list')
15
+ name = line.split('ipv6-access-list')[1].strip if line.include?('ipv6-access-list')
16
+ name = line.split('link-access-list')[1].strip if line.include?('link-access-list')
17
+ table[name] ||= {}
18
+ if line.include?('rule')
19
+ rule = line.split(' ')
20
+ table[name][rule[1].to_i] = rule[2..-1]
21
+ end
22
+ end
23
+ table.delete(nil)
24
+ return table
25
+ end
26
+ end
27
+ ```
@@ -0,0 +1,27 @@
1
+
2
+ # M6000-8E 访问控制列表
3
+
4
+ ```ruby
5
+ @sign << ['M6000-8E', 'ACL']
6
+
7
+ module M6000_8E
8
+ module_function
9
+
10
+ def ACL config
11
+ table = {}
12
+ name = nil
13
+ config.split("\n").each do|line|
14
+ name = line.split('ipv4-access-list')[1].strip if line.include?('ipv4-access-list')
15
+ name = line.split('ipv6-access-list')[1].strip if line.include?('ipv6-access-list')
16
+ name = line.split('link-access-list')[1].strip if line.include?('link-access-list')
17
+ table[name] ||= {}
18
+ if line.include?('rule')
19
+ rule = line.split(' ')
20
+ table[name][rule[1].to_i] = rule[2..-1]
21
+ end
22
+ end
23
+ table.delete(nil)
24
+ return table
25
+ end
26
+ end
27
+ ```
@@ -0,0 +1,204 @@
1
+
2
+ # MA5200G-8 访问控制列表
3
+
4
+ ```ruby
5
+ @sign << ['MA5200G-8', 'ACL']
6
+ @sign << ['MA5200G-8', 'resort']
7
+ @sign << ['MA5200G-8', 'detect_adv']
8
+ @sign << ['MA5200G-8', 'gen_rule']
9
+
10
+ module MA5200G_8
11
+ module_function
12
+
13
+ def ACL config
14
+ table = {}
15
+ number = nil
16
+ config.split("\n").each do|line|
17
+ number = line.split('number')[1].strip.to_i if line.include?('acl number') or line.include?('acl ipv6 number')
18
+ table[number] ||= {}
19
+ if line.include?('rule')
20
+ rule = line.split(' ')
21
+ table[number][rule[1].to_i] = rule[2..-1]
22
+ end
23
+ end
24
+ table.delete(nil)
25
+ return table
26
+ end
27
+
28
+ # 重排ACL序号,只对纯文本编排,注意最末行
29
+ def resort text, index # {[old,old]=>new}
30
+ packs,newlist = {},[]
31
+ index.each do|is,ni| packs[ni] = [] end
32
+ text.split("\n").each do|line|
33
+ items = line.split(' ')
34
+ id = items[1].to_i
35
+ index.each do|is,ni|
36
+ packs[ni] << items[2..-1].join(' ') if (is[0]..is[1]).include?(id)
37
+ end
38
+ newlist << "undo rule #{id}"
39
+ end
40
+ packs.each do|ni,pack|
41
+ pack.sort.each_with_index do|rule, si|
42
+ newlist << "rule #{ni.to_i+si.to_i} #{rule}"
43
+ end
44
+ end
45
+ return newlist.join("\n")
46
+ end
47
+
48
+ # INTEGER<1000-1999> Interface access-list(add to current using rules)
49
+ # INTEGER<10000-10999> MPLS access list (add to current using rules)
50
+ # INTEGER<2000-2999> Basic access-list(add to current using rules)
51
+ # INTEGER<3000-3999> Advanced access-list(add to current using rules)
52
+ # INTEGER<4000-4999> Specify a L2 ACL group(add to current using rules)
53
+ # ip-pool Specify IP pool configuration
54
+ # ipv6 ACL IPv6
55
+ # name Specify a named ACL
56
+ # number Specify a numbered ACL
57
+
58
+ PORTS = {
59
+ 137 => 'netbios-ns',
60
+ 138 => 'netbios-dgm',
61
+ 139 => 'netbios-ssn',
62
+ 19 => 'CHARgen',
63
+ 179 => 'bgp',
64
+ 514 => 'cmd',
65
+ 13 => 'daytime',
66
+ 9 => 'discard',
67
+ 53 => 'domain',
68
+ 7 => 'echo',
69
+ 512 => 'exec',
70
+ 79 => 'finger',
71
+ 21 => 'ftp',
72
+ 20 => 'ftp-data',
73
+ 70 => 'gopher',
74
+ 101 => 'hostname',
75
+ 194 => 'irc',
76
+ 543 => 'klogin',
77
+ 544 => 'kshell',
78
+ 513 => 'login',
79
+ 515 => 'lpd',
80
+ 119 => 'nntp',
81
+ 109 => 'pop2',
82
+ 110 => 'pop3',
83
+ 25 => 'smtp',
84
+ 111 => 'sunrpc',
85
+ 49 => 'tacacs',
86
+ 517 => 'talk',
87
+ 23 => 'telnet',
88
+ 37 => 'time',
89
+ 540 => 'uucp',
90
+ 43 => 'whois',
91
+ 80 => 'www'
92
+ }
93
+
94
+ # acl-adv rule
95
+ def detect_adv rule,index=nil
96
+ words = rule.instance_of?(String) ? rule.split(' ') : rule # text or array
97
+ # words: <action> <protocol> [<src>] [<dst>] [<tail>]
98
+ # <src> := [ source <sip> <smk> ] [ source-port [[eq|gt|lt <spt>]|[range <spt1> <spt2>]] ]
99
+ # <dst> := [ destination <dip> <dmk>] [ destination-port [[eq|gt|lt <dpt>]|[range <dpt1> <dpt2>]] ]
100
+ # <tail> := "PENDING"
101
+ ritle = {}
102
+ ritle['index'] = index if index
103
+ action,protocol = words[0..1]
104
+ ritle['action'] = action
105
+ ritle['protocol'] = protocol
106
+ ['source','destination'].each do|edge|
107
+ if words.include?(edge)
108
+ edge_ip = words[words.index(edge)+1]
109
+ unless edge_ip=='any'
110
+ if edge_ip.include?(':')
111
+ start_ip, ei_amask = IP.v6(edge_ip)
112
+ else
113
+ ei_amask_str = words[words.index(edge)+2]
114
+ ei_amask_str = '0.0.0.0' if ei_amask_str == '0'
115
+ start_ip,ei_amask = IP.v4(edge_ip),IP.v4(ei_amask_str)
116
+ end
117
+ end_ip = start_ip.clone + ei_amask.number
118
+ ritle[edge] = [start_ip.to_s, end_ip.to_s]
119
+ end
120
+ end
121
+ end
122
+ ['source-port', 'destination-port'].each do|port|
123
+ if words.include?(port)
124
+ op = words[words.index(port)+1]
125
+ range = case op
126
+ when 'range'
127
+ a, b = words[words.index(port)+2], words[words.index(port)+3]
128
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
129
+ b = PORTS.key(b) ? PORTS.key(b) : b.to_i
130
+ [ a, b ]
131
+ when 'lt'
132
+ a = words[words.index(port)+2]
133
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
134
+ [ 0, a ]
135
+ when 'gt'
136
+ a = words[words.index(port)+2]
137
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
138
+ [ 65535, a ]
139
+ when 'eq'
140
+ a = words[words.index(port)+2]
141
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
142
+ [ a, a ]
143
+ end.sort
144
+ ritle[port] = range
145
+ end
146
+ end
147
+ # TODO: tail
148
+ return ritle
149
+ end
150
+
151
+ # 查询规则1#
152
+ def in_range? ritle, target
153
+ tip = IP.v4(target[:ip])
154
+ tnum = tip.number
155
+ if ritle["destination"] && ritle["destination-port"]
156
+ si,ei = ritle["destination"].map{|i|IP.v4(i).number}
157
+ sp,ep = ritle["destination-port"]
158
+ return ritle if (si..ei).include?(tnum) && (sp..ep).include?(target[:port]) && ritle['action']==target[:action]
159
+ end
160
+ return nil
161
+ end
162
+
163
+ # 查询规则2#
164
+ def list_in_range? list, target
165
+ tip = IP.v4(target[:ip])
166
+ tnum = tip.number
167
+ set = []
168
+ list.each do|ritle|
169
+ if ritle["destination"] && ritle["destination-port"]
170
+ si,ei = ritle["destination"].map{|i|IP.v4(i).number}
171
+ sp,ep = ritle["destination-port"]
172
+ set << ritle if (si..ei).include?(tnum) && (sp..ep).include?(target[:port]) && ritle['action']==target[:action]
173
+ end
174
+ end
175
+ return set
176
+ end
177
+
178
+ # 生成规则:
179
+ # options = {
180
+ # index: 107,
181
+ # action: 'deny',
182
+ # protocol: 'tcp',
183
+ # sip: '1.1.1.1',
184
+ # sport: 80,
185
+ # dip: '2.2.2.2',
186
+ # dport: 443
187
+ # }
188
+ def gen_rule options
189
+ rule = ['rule']
190
+ return {'conf-error'=>"缺少必要参数:规则索引"} unless options[:index]
191
+ return {'conf-error'=>"缺少必要参数:动作"} unless options[:action]
192
+ return {'conf-error'=>"缺少必要参数:协议"} unless options[:protocol]
193
+ return {'runtime-error'=>"规则空位不足"} if options[:index]=='no slot'
194
+ rule << options[:index]
195
+ rule << options[:action]
196
+ rule << options[:protocol]
197
+ options[:sip] and rule << "source #{options[:sip]}"
198
+ options[:sport] and rule << "source-port eq #{options[:sport]}"
199
+ options[:dip] and rule << "destination #{options[:dip]}"
200
+ options[:dport] and rule << "destination-port eq #{options[:dport]}"
201
+ return {"operation"=>rule.join(" ")}
202
+ end
203
+ end
204
+ ```
@@ -0,0 +1,177 @@
1
+
2
+ # ME60-16 访问控制列表
3
+
4
+ ```ruby
5
+ @sign << ['ME60-16', 'ACL']
6
+ @sign << ['ME60-16', 'resort']
7
+ @sign << ['ME60-16', 'detect_adv']
8
+ @sign << ['ME60-16', 'gen_rule']
9
+
10
+ module ME60_16
11
+ module_function
12
+
13
+ def ACL config
14
+ table = {}
15
+ number = nil
16
+ config.split("\n").each do|line|
17
+ number = line.split('number')[1].strip.to_i if line.include?('acl number') or line.include?('acl ipv6 number')
18
+ table[number] ||= {}
19
+ if line.include?('rule')
20
+ rule = line.split(' ')
21
+ table[number][rule[1].to_i] = rule[2..-1]
22
+ end
23
+ end
24
+ table.delete(nil)
25
+ return table
26
+ end
27
+
28
+ # 重排ACL序号,只对纯文本编排,注意最末行
29
+ def resort text, index # {[old,old]=>new}
30
+ packs,newlist = {},[]
31
+ index.each do|is,ni| packs[ni] = [] end
32
+ text.split("\n").each do|line|
33
+ items = line.split(' ')
34
+ id = items[1].to_i
35
+ index.each do|is,ni|
36
+ packs[ni] << items[2..-1].join(' ') if (is[0]..is[1]).include?(id)
37
+ end
38
+ newlist << "undo rule #{id}"
39
+ end
40
+ packs.each do|ni,pack|
41
+ pack.sort.each_with_index do|rule, si|
42
+ newlist << "rule #{ni.to_i+si.to_i} #{rule}"
43
+ end
44
+ end
45
+ return newlist.join("\n")
46
+ end
47
+
48
+ # INTEGER<1000-1999> Interface access-list(add to current using rules)
49
+ # INTEGER<10000-10999> MPLS access list (add to current using rules)
50
+ # INTEGER<2000-2999> Basic access-list(add to current using rules)
51
+ # INTEGER<3000-3999> Advanced access-list(add to current using rules)
52
+ # INTEGER<4000-4999> Specify a L2 ACL group(add to current using rules)
53
+ # ip-pool Specify IP pool configuration
54
+ # ipv6 ACL IPv6
55
+ # name Specify a named ACL
56
+ # number Specify a numbered ACL
57
+
58
+ PORTS = {
59
+ 137 => 'netbios-ns',
60
+ 138 => 'netbios-dgm',
61
+ 139 => 'netbios-ssn',
62
+ 19 => 'CHARgen',
63
+ 179 => 'bgp',
64
+ 514 => 'cmd',
65
+ 13 => 'daytime',
66
+ 9 => 'discard',
67
+ 53 => 'domain',
68
+ 7 => 'echo',
69
+ 512 => 'exec',
70
+ 79 => 'finger',
71
+ 21 => 'ftp',
72
+ 20 => 'ftp-data',
73
+ 70 => 'gopher',
74
+ 101 => 'hostname',
75
+ 194 => 'irc',
76
+ 543 => 'klogin',
77
+ 544 => 'kshell',
78
+ 513 => 'login',
79
+ 515 => 'lpd',
80
+ 119 => 'nntp',
81
+ 109 => 'pop2',
82
+ 110 => 'pop3',
83
+ 25 => 'smtp',
84
+ 111 => 'sunrpc',
85
+ 49 => 'tacacs',
86
+ 517 => 'talk',
87
+ 23 => 'telnet',
88
+ 37 => 'time',
89
+ 540 => 'uucp',
90
+ 43 => 'whois',
91
+ 80 => 'www'
92
+ }
93
+
94
+ # acl-adv rule
95
+ def detect_adv rule,index=nil
96
+ words = rule.split(' ')
97
+ # words: <action> <protocol> [<src>] [<dst>] [<tail>]
98
+ # <src> := [ source <sip> <smk> ] [ source-port [[eq|gt|lt <spt>]|[range <spt1> <spt2>]] ]
99
+ # <dst> := [ destination <dip> <dmk>] [ destination-port [[eq|gt|lt <dpt>]|[range <dpt1> <dpt2>]] ]
100
+ # <tail> := "PENDING"
101
+ ritle = {}
102
+ ritle['index'] = index if index
103
+ action,protocol = words[0..1]
104
+ ritle['action'] = action
105
+ ritle['protocol'] = protocol
106
+ ['source','destination'].each do|edge|
107
+ if words.include?(edge)
108
+ edge_ip = words[words.index(edge)+1]
109
+ unless edge_ip=='any'
110
+ if edge_ip.include?(':')
111
+ start_ip, ei_amask = IP.v6(edge_ip)
112
+ else
113
+ ei_amask_str = words[words.index(edge)+2]
114
+ ei_amask_str = '0.0.0.0' if ei_amask_str == '0'
115
+ start_ip,ei_amask = IP.v4(edge_ip),IP.v4(ei_amask_str)
116
+ end
117
+ end_ip = start_ip.clone + ei_amask.number
118
+ ritle[edge] = [start_ip.to_s, end_ip.to_s]
119
+ end
120
+ end
121
+ end
122
+ ['source-port', 'destination-port'].each do|port|
123
+ if words.include?(port)
124
+ op = words[words.index(port)+1]
125
+ range = case op
126
+ when 'range'
127
+ a, b = words[words.index(port)+2], words[words.index(port)+3]
128
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
129
+ b = PORTS.key(b) ? PORTS.key(b) : b.to_i
130
+ [ a, b ]
131
+ when 'lt'
132
+ a = words[words.index(port)+2]
133
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
134
+ [ 0, a ]
135
+ when 'gt'
136
+ a = words[words.index(port)+2]
137
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
138
+ [ 65535, a ]
139
+ when 'eq'
140
+ a = words[words.index(port)+2]
141
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
142
+ [ a, a ]
143
+ end.sort
144
+ ritle[port] = range
145
+ end
146
+ end
147
+ # TODO: tail
148
+ return ritle
149
+ end
150
+
151
+ # 生成规则:
152
+ # options = {
153
+ # index: 107,
154
+ # action: 'deny',
155
+ # protocol: 'tcp',
156
+ # sip: '1.1.1.1',
157
+ # sport: 80,
158
+ # dip: '2.2.2.2',
159
+ # dport: 443
160
+ # }
161
+ def gen_rule options
162
+ rule = ['rule']
163
+ return {'conf-error'=>"缺少必要参数:规则索引"} unless options[:index]
164
+ return {'conf-error'=>"缺少必要参数:动作"} unless options[:action]
165
+ return {'conf-error'=>"缺少必要参数:协议"} unless options[:protocol]
166
+ return {'runtime-error'=>"规则空位不足"} if options[:index]=='no slot'
167
+ rule << options[:index]
168
+ rule << options[:action]
169
+ rule << options[:protocol]
170
+ options[:sip] and rule << "source #{options[:sip]}"
171
+ options[:sport] and rule << "source-port eq #{options[:sport]}"
172
+ options[:dip] and rule << "destination #{options[:dip]}"
173
+ options[:dport] and rule << "destination-port eq #{options[:dport]}"
174
+ return {"operation"=>rule.join(" ")}
175
+ end
176
+ end
177
+ ```
@@ -0,0 +1,177 @@
1
+
2
+ # ME60-X16 访问控制列表
3
+
4
+ ```ruby
5
+ @sign << ['ME60-X16', 'ACL']
6
+ @sign << ['ME60-X16', 'resort']
7
+ @sign << ['ME60-X16', 'detect_adv']
8
+ @sign << ['ME60-X16', 'gen_rule']
9
+
10
+ module ME60_X16
11
+ module_function
12
+
13
+ def ACL config
14
+ table = {}
15
+ number = nil
16
+ config.split("\n").each do|line|
17
+ number = line.split('number')[1].strip.to_i if line.include?('acl number') or line.include?('acl ipv6 number')
18
+ table[number] ||= {}
19
+ if line.include?('rule')
20
+ rule = line.split(' ')
21
+ table[number][rule[1].to_i] = rule[2..-1]
22
+ end
23
+ end
24
+ table.delete(nil)
25
+ return table
26
+ end
27
+
28
+ # 重排ACL序号,只对纯文本编排,注意最末行
29
+ def resort text, index # {[old,old]=>new}
30
+ packs,newlist = {},[]
31
+ index.each do|is,ni| packs[ni] = [] end
32
+ text.split("\n").each do|line|
33
+ items = line.split(' ')
34
+ id = items[1].to_i
35
+ index.each do|is,ni|
36
+ packs[ni] << items[2..-1].join(' ') if (is[0]..is[1]).include?(id)
37
+ end
38
+ newlist << "undo rule #{id}"
39
+ end
40
+ packs.each do|ni,pack|
41
+ pack.sort.each_with_index do|rule, si|
42
+ newlist << "rule #{ni.to_i+si.to_i} #{rule}"
43
+ end
44
+ end
45
+ return newlist.join("\n")
46
+ end
47
+
48
+ # INTEGER<1000-1999> Interface access-list(add to current using rules)
49
+ # INTEGER<10000-10999> MPLS access list (add to current using rules)
50
+ # INTEGER<2000-2999> Basic access-list(add to current using rules)
51
+ # INTEGER<3000-3999> Advanced access-list(add to current using rules)
52
+ # INTEGER<4000-4999> Specify a L2 ACL group(add to current using rules)
53
+ # ip-pool Specify IP pool configuration
54
+ # ipv6 ACL IPv6
55
+ # name Specify a named ACL
56
+ # number Specify a numbered ACL
57
+
58
+ PORTS = {
59
+ 137 => 'netbios-ns',
60
+ 138 => 'netbios-dgm',
61
+ 139 => 'netbios-ssn',
62
+ 19 => 'CHARgen',
63
+ 179 => 'bgp',
64
+ 514 => 'cmd',
65
+ 13 => 'daytime',
66
+ 9 => 'discard',
67
+ 53 => 'domain',
68
+ 7 => 'echo',
69
+ 512 => 'exec',
70
+ 79 => 'finger',
71
+ 21 => 'ftp',
72
+ 20 => 'ftp-data',
73
+ 70 => 'gopher',
74
+ 101 => 'hostname',
75
+ 194 => 'irc',
76
+ 543 => 'klogin',
77
+ 544 => 'kshell',
78
+ 513 => 'login',
79
+ 515 => 'lpd',
80
+ 119 => 'nntp',
81
+ 109 => 'pop2',
82
+ 110 => 'pop3',
83
+ 25 => 'smtp',
84
+ 111 => 'sunrpc',
85
+ 49 => 'tacacs',
86
+ 517 => 'talk',
87
+ 23 => 'telnet',
88
+ 37 => 'time',
89
+ 540 => 'uucp',
90
+ 43 => 'whois',
91
+ 80 => 'www'
92
+ }
93
+
94
+ # acl-adv rule
95
+ def detect_adv rule,index=nil
96
+ words = rule.split(' ')
97
+ # words: <action> <protocol> [<src>] [<dst>] [<tail>]
98
+ # <src> := [ source <sip> <smk> ] [ source-port [[eq|gt|lt <spt>]|[range <spt1> <spt2>]] ]
99
+ # <dst> := [ destination <dip> <dmk>] [ destination-port [[eq|gt|lt <dpt>]|[range <dpt1> <dpt2>]] ]
100
+ # <tail> := "PENDING"
101
+ ritle = {}
102
+ ritle['index'] = index if index
103
+ action,protocol = words[0..1]
104
+ ritle['action'] = action
105
+ ritle['protocol'] = protocol
106
+ ['source','destination'].each do|edge|
107
+ if words.include?(edge)
108
+ edge_ip = words[words.index(edge)+1]
109
+ unless edge_ip=='any'
110
+ if edge_ip.include?(':')
111
+ start_ip, ei_amask = IP.v6(edge_ip)
112
+ else
113
+ ei_amask_str = words[words.index(edge)+2]
114
+ ei_amask_str = '0.0.0.0' if ei_amask_str == '0'
115
+ start_ip,ei_amask = IP.v4(edge_ip),IP.v4(ei_amask_str)
116
+ end
117
+ end_ip = start_ip.clone + ei_amask.number
118
+ ritle[edge] = [start_ip.to_s, end_ip.to_s]
119
+ end
120
+ end
121
+ end
122
+ ['source-port', 'destination-port'].each do|port|
123
+ if words.include?(port)
124
+ op = words[words.index(port)+1]
125
+ range = case op
126
+ when 'range'
127
+ a, b = words[words.index(port)+2], words[words.index(port)+3]
128
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
129
+ b = PORTS.key(b) ? PORTS.key(b) : b.to_i
130
+ [ a, b ]
131
+ when 'lt'
132
+ a = words[words.index(port)+2]
133
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
134
+ [ 0, a ]
135
+ when 'gt'
136
+ a = words[words.index(port)+2]
137
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
138
+ [ 65535, a ]
139
+ when 'eq'
140
+ a = words[words.index(port)+2]
141
+ a = PORTS.key(a) ? PORTS.key(a) : a.to_i
142
+ [ a, a ]
143
+ end.sort
144
+ ritle[port] = range
145
+ end
146
+ end
147
+ # TODO: tail
148
+ return ritle
149
+ end
150
+
151
+ # 生成规则:
152
+ # options = {
153
+ # index: 107,
154
+ # action: 'deny',
155
+ # protocol: 'tcp',
156
+ # sip: '1.1.1.1',
157
+ # sport: 80,
158
+ # dip: '2.2.2.2',
159
+ # dport: 443
160
+ # }
161
+ def gen_rule options
162
+ rule = ['rule']
163
+ return {'conf-error'=>"缺少必要参数:规则索引"} unless options[:index]
164
+ return {'conf-error'=>"缺少必要参数:动作"} unless options[:action]
165
+ return {'conf-error'=>"缺少必要参数:协议"} unless options[:protocol]
166
+ return {'runtime-error'=>"规则空位不足"} if options[:index]=='no slot'
167
+ rule << options[:index]
168
+ rule << options[:action]
169
+ rule << options[:protocol]
170
+ options[:sip] and rule << "source #{options[:sip]}"
171
+ options[:sport] and rule << "source-port eq #{options[:sport]}"
172
+ options[:dip] and rule << "destination #{options[:dip]}"
173
+ options[:dport] and rule << "destination-port eq #{options[:dport]}"
174
+ return {"operation"=>rule.join(" ")}
175
+ end
176
+ end
177
+ ```