Showing
2 changed files
with
70 additions
and
21 deletions
@@ -36,7 +36,7 @@ gather_interface_info() { | @@ -36,7 +36,7 @@ gather_interface_info() { | ||
36 | gather_if_info_ip() { | 36 | gather_if_info_ip() { |
37 | eval $(${IP} -o link | ${AWK} '{ | 37 | eval $(${IP} -o link | ${AWK} '{ |
38 | sub(/:/,"",$1); | 38 | sub(/:/,"",$1); |
39 | - no=$1; | 39 | + ifno=$1; |
40 | sub(/:/,"",$2); | 40 | sub(/:/,"",$2); |
41 | name=$2; | 41 | name=$2; |
42 | classes=classes "net_iface_" name " "; | 42 | classes=classes "net_iface_" name " "; |
@@ -50,35 +50,82 @@ gather_if_info_ip() { | @@ -50,35 +50,82 @@ gather_if_info_ip() { | ||
50 | } | 50 | } |
51 | } | 51 | } |
52 | print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";"; | 52 | print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";"; |
53 | - if ("UP" == state) classes=classes mac " " | 53 | + if ("UP" == state) classes=classes mac " "; |
54 | + if_numbers=if_numbers " " ifno; | ||
55 | + no+=1 | ||
54 | } | 56 | } |
55 | BEGIN { | 57 | BEGIN { |
56 | - classes="" | 58 | + classes=""; |
59 | + no=1; | ||
60 | + if_numbers="" | ||
57 | } | 61 | } |
58 | END { | 62 | END { |
59 | print "set_class " classes ";"; | 63 | print "set_class " classes ";"; |
60 | print "NINTERFACES=" FNR ";" | 64 | print "NINTERFACES=" FNR ";" |
65 | + sub(/^ */,"",if_numbers); | ||
66 | + sub(/ *$/,"",if_numbers); | ||
67 | + print "IF_NUMBERS=\"" if_numbers "\";"; | ||
61 | }') | 68 | }') |
62 | 69 | ||
63 | - eval $(${IP} -o addr | ${AWK} '{ | ||
64 | - sub(/:/,"",$1); | ||
65 | - no=$1; | ||
66 | - if ($3 == "inet") { | ||
67 | - sub(/[\/%].*/,"",$4); | ||
68 | - print "IF" no "_IPV4=\"${IF" no "_IPV4}" $4 " \";"; | ||
69 | - classes=classes $4 " " | ||
70 | - } | ||
71 | - if ($3 == "inet6") { | ||
72 | - sub(/[\/%].*/,"",$4); | ||
73 | - print "IF" no "_IPV6=\"${IF" no "_IPV6}" $4 " \";"; | ||
74 | - classes=classes $4 " " | 70 | + echo "DEBUG: ${IF_NO}" |
71 | + | ||
72 | + eval $(${IP} -o addr | ${AWK} -v if_numbers="${IF_NUMBERS}" '{ | ||
73 | + sub(/:/,"",$1); | ||
74 | + if_no=$1; | ||
75 | + no=r_if_numbers[if_no]; | ||
76 | + if ($3 == "inet") { | ||
77 | + split($4,addr,/[\/]/); | ||
78 | + print "IF" no "_IPV4=\"${IF" no "_IPV4}" addr[1] " \";"; | ||
79 | + network(no, addr[1], addr[2]); | ||
80 | + classes=classes $4 " " | ||
81 | + } | ||
82 | + if ($3 == "inet6") { | ||
83 | + split($4,addr,/[\/]/); | ||
84 | + print "IF" no "_IPV6=\"${IF" no "_IPV6}" addr[1] " \";"; | ||
85 | + classes=classes $4 " " | ||
86 | + } | ||
87 | + } | ||
88 | + BEGIN { | ||
89 | + classes=""; | ||
90 | + sub(/^ */,"",if_numbers); | ||
91 | + sub(/ *$/,"",if_numbers); | ||
92 | + split(if_numbers,tmp_array,/ /) | ||
93 | + for(no in tmp_array) { | ||
94 | + if_no=tmp_array[no]; | ||
95 | + r_if_numbers[if_no]=no; | ||
75 | } | 96 | } |
76 | - } | ||
77 | - BEGIN { | ||
78 | - classes="" | ||
79 | - } | ||
80 | - END { | ||
81 | - print "set_class " classes ";" | 97 | + } |
98 | + END { | ||
99 | + print "set_class " classes ";" | ||
100 | + } | ||
101 | + function network(no, addr, cidr) { | ||
102 | + mask=""; | ||
103 | + net=""; | ||
104 | + full_octets=cidr/8; | ||
105 | + part_octets=cidr%8; | ||
106 | + split(addr,parts,/\./); | ||
107 | + for(i=1; i<5; i++) { | ||
108 | + if(i-1 < full_octets) { | ||
109 | + mask=mask "255"; | ||
110 | + net=net parts[i]; | ||
111 | + } else { | ||
112 | + if (i-1 == full_octets) { | ||
113 | + mpart=256-lshift(1, 8-part_octets); | ||
114 | + mask=mask mpart; | ||
115 | + net=net and(parts[i],mpart); | ||
116 | + } else { | ||
117 | + mask=mask "0"; | ||
118 | + net=net "0"; | ||
119 | + } | ||
120 | + } | ||
121 | + if (i < 4) { | ||
122 | + mask=mask "."; | ||
123 | + net=net "."; | ||
124 | + } | ||
125 | + } | ||
126 | + classes=classes " " net; | ||
127 | + print "IF" no "_NETWORK=\"" net "\"; export " name "_NETWORK;"; | ||
128 | + print "IF" no "_MASK=\"" mask "\"; export " name "_MASK;"; | ||
82 | }') | 129 | }') |
83 | } | 130 | } |
84 | 131 |
@@ -40,6 +40,8 @@ do | @@ -40,6 +40,8 @@ do | ||
40 | eval printf \"%15s : %s\\\n\" \"IF${NO}_STATE\" \"\${IF${NO}_STATE}\" | 40 | eval printf \"%15s : %s\\\n\" \"IF${NO}_STATE\" \"\${IF${NO}_STATE}\" |
41 | eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV4\" \"\${IF${NO}_IPV4}\" | 41 | eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV4\" \"\${IF${NO}_IPV4}\" |
42 | eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV6\" \"\${IF${NO}_IPV6}\" | 42 | eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV6\" \"\${IF${NO}_IPV6}\" |
43 | + eval printf \"%15s : %s\\\n\" \"IF${NO}_MASK\" \"\${IF${NO}_MASK}\" | ||
44 | + eval printf \"%15s : %s\\\n\" \"IF${NO}_NETWORK\" \"\${IF${NO}_NETWORK}\" | ||
43 | NO=$((NO+1)) | 45 | NO=$((NO+1)) |
44 | done | 46 | done |
45 | 47 |
Please
register
or
login
to post a comment