Commit 86fa5b810af641a5e53edbd26571ec84889fe065
1 parent
b382bfa3
seperate original script in several of special purpose and implement some kind o…
…f include_once for sourcing scripts only once
Showing
5 changed files
with
352 additions
and
209 deletions
interface_information.sh
0 → 100755
1 | +#!/bin/dash | |
2 | + | |
3 | +## | |
4 | +# This creates function that will gather some system informations | |
5 | +# and propagate them as environment variables. | |
6 | +# | |
7 | + | |
8 | +. ./utils.sh | |
9 | + | |
10 | +## | |
11 | +# retrieve interfaces. I prefer using ip if it is | |
12 | +# available, else I fallback to ifconfig... don't know | |
13 | +# how to retrieve this information on other systems. | |
14 | +# This also exports the environment variables. | |
15 | +# | |
16 | +get_interface_data() { | |
17 | + local NO=1 | |
18 | + | |
19 | + if [ ${IP} ] | |
20 | + then | |
21 | + get_interface_data_ip | |
22 | + else | |
23 | + if [ ${IFCONFIG} ] | |
24 | + then | |
25 | + get_interface_data_ifconfig | |
26 | + else | |
27 | + ${LOGGER} -p local0.warn 'Found no way to retrieve interface information.' | |
28 | + fi | |
29 | + fi | |
30 | + | |
31 | + export NINTERFACES | |
32 | + export CLASSES | |
33 | + | |
34 | + while [ ${NO} -le ${NINTERFACES:=0} ] | |
35 | + do | |
36 | + export IF${NO}_NAME IF${NO}_MAC IF${NO}_STATE IF${NO}_IPV4 IF${NO}_IPV6 | |
37 | + NO=$((NO+1)) | |
38 | + done | |
39 | +} | |
40 | + | |
41 | +## | |
42 | +# get the interface information from the ip tool | |
43 | +# | |
44 | +get_interface_data_ip() { | |
45 | + eval $(${IP} -o link | ${AWK} '{ | |
46 | + sub(/:/,"",$1); | |
47 | + no=$1; | |
48 | + sub(/:/,"",$2); | |
49 | + name=$2; | |
50 | + for (i=3; i<NF; i++) { | |
51 | + if ($i == "state") { | |
52 | + i++; state=$i | |
53 | + } | |
54 | + if ($i ~ /link/) { | |
55 | + i++; mac=$i; | |
56 | + classes=classes mac " " | |
57 | + } | |
58 | + } | |
59 | + print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";"; | |
60 | + if ("UP" == state) classes=classes mac " " | |
61 | + } | |
62 | + BEGIN { | |
63 | + classes="" | |
64 | + } | |
65 | + END { | |
66 | + print "CLASSES=\"${CLASSES}" classes " \";"; | |
67 | + print "NINTERFACES=" FNR ";" | |
68 | + }') | |
69 | + | |
70 | + eval $(${IP} -o addr | ${AWK} '{ | |
71 | + sub(/:/,"",$1); | |
72 | + no=$1; | |
73 | + if ($3 == "inet") { | |
74 | + sub(/[\/%].*/,"",$4); | |
75 | + print "IF" no "_IPV4=\"${IF" no "_IPV4}" $4 " \";"; | |
76 | + classes=classes $4 " " | |
77 | + } | |
78 | + if ($3 == "inet6") { | |
79 | + sub(/[\/%].*/,"",$4); | |
80 | + print "IF" no "_IPV6=\"${IF" no "_IPV6}" $4 " \";"; | |
81 | + classes=classes $4 " " | |
82 | + } | |
83 | + } | |
84 | + BEGIN { | |
85 | + classes="" | |
86 | + } | |
87 | + END { | |
88 | + print "CLASSES=\"${CLASSES}" classes " \";" | |
89 | + }') | |
90 | +} | |
91 | + | |
92 | +## | |
93 | +# get interface data via the ifconfig tool | |
94 | +# | |
95 | +get_interface_data_ifconfig() { | |
96 | + eval $(${IFCONFIG} -a | ${AWK} ' | |
97 | + /ether/ { mac=$2 } | |
98 | + /inet / { ipv4=ipv4 $2 " " } | |
99 | + /inet6/ { ipv6=ipv6 $2 " " } | |
100 | + /^[^ \t]/ { | |
101 | + if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN"; | |
102 | + if ("" != name) { | |
103 | + print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \ | |
104 | + "IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \ | |
105 | + "IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";"; | |
106 | + no++; | |
107 | + } | |
108 | + ipv4=ipv6=""; | |
109 | + sub(/:/,"",$1); | |
110 | + name=$1 | |
111 | + } | |
112 | + BEGIN { | |
113 | + no=1; | |
114 | + } | |
115 | + END { | |
116 | + if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN"; | |
117 | + print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \ | |
118 | + "IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \ | |
119 | + "IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";"; | |
120 | + print "NINTERFACES=" no | |
121 | + }') | |
122 | +} | |
123 | + | |
124 | +## | |
125 | +# autorun this if sourced. | |
126 | +# | |
127 | + | |
128 | +[ -z "${NINTERFACES}" ] && get_interface_data | |
129 | + | |
130 | +# vim: set ts=4 sw=4: | ... | ... |
network_tools.sh
0 → 100755
1 | +#!/bin/dash | |
2 | + | |
3 | +. ./utils.sh | |
4 | + | |
5 | +## | |
6 | +# Function calculates number of bit in a netmask | |
7 | +# | |
8 | +mask2cidr() { | |
9 | + local NBITS=0 | |
10 | + local OLDIFS="${IFS}" | |
11 | + IFS=. | |
12 | + | |
13 | + for DEC in $1 | |
14 | + do | |
15 | + case ${DEC} in | |
16 | + 255) NBITS=$((NBITS+8));; | |
17 | + 254) NBITS=$((NBITS+7));; | |
18 | + 252) NBITS=$((NBITS+6));; | |
19 | + 248) NBITS=$((NBITS+5));; | |
20 | + 240) NBITS=$((NBITS+4));; | |
21 | + 224) NBITS=$((NBITS+3));; | |
22 | + 192) NBITS=$((NBITS+2));; | |
23 | + 128) NBITS=$((NBITS+1));; | |
24 | + 0);; | |
25 | + *) NBITS=0; IFS="${OLDIFS}"; return 1 | |
26 | + esac | |
27 | + done | |
28 | + IFS="${OLDIFS}" | |
29 | + ${ECHO} "${NBITS}" | |
30 | +} | |
31 | + | |
32 | +## | |
33 | +# Function calculates a netmask from number of bits | |
34 | +# | |
35 | +cidr2mask() { | |
36 | + local I MASK="" | |
37 | + local FULL_OCTETS=$((${1}/8)) | |
38 | + local PARTIAL_OCTET=$((${1}%8)) | |
39 | + | |
40 | + for I in 0 1 2 3 | |
41 | + do | |
42 | + if [ ${I} -lt ${FULL_OCTETS} ] | |
43 | + then | |
44 | + MASK="${MASK}255" | |
45 | + elif [ ${I} -eq ${FULL_OCTETS} ] | |
46 | + then | |
47 | + MASK="${MASK}$((256-(1<<(8-${PARTIAL_OCTET}))))" | |
48 | + else | |
49 | + MASK="${MASK}0" | |
50 | + fi | |
51 | + [ ${I} -lt 3 ] && MASK="${MASK}." | |
52 | + done | |
53 | + | |
54 | + ${ECHO} ${MASK} | |
55 | +} | |
56 | + | |
57 | +# vim: set ts=4 sw=4: | ... | ... |
run.sh
0 → 100755
1 | +#!/bin/dash | |
2 | + | |
3 | +## | |
4 | +# This is a first test what static informations I can gather from a | |
5 | +# system. The minimal requirement are the IP addresses because | |
6 | +# on these I build up my classes. | |
7 | +# The system type is also important...we might need to be running | |
8 | +# on variuous UNIX flavours. (But for now we focus on various linux | |
9 | +# flavours and maybe FreeBSD. | |
10 | +# | |
11 | + | |
12 | +. ./utils.sh | |
13 | +include_once ./system_information.sh | |
14 | +include_once ../sysman/interface_information.sh | |
15 | +include_once /home/ghopp/sysman/network_tools.sh | |
16 | + | |
17 | +## | |
18 | +# report everysthing | |
19 | +# | |
20 | + | |
21 | +${PRINTF} "%15s : %s\n" "OS" "${OS}" | |
22 | +${PRINTF} "%15s : %s\n" "KERNEL" "${KERNEL}" | |
23 | +${PRINTF} "%15s : %s\n" "VERSION" "${VERSION}" | |
24 | +${PRINTF} "%15s : %s\n" "PLATFORM" "${PLATFORM}" | |
25 | +${PRINTF} "%15s : %s\n" "DIST" "${DIST}" | |
26 | +${PRINTF} "%15s : %s\n" "HOSTNAME" "${HOSTNAME}" | |
27 | +${PRINTF} "%15s : %s\n" "# INTERFACES" "${NINTERFACES}" | |
28 | + | |
29 | +NO=1 | |
30 | +while [ ${NO} -le ${NINTERFACES:=0} ] | |
31 | +do | |
32 | + eval printf \"%15s : %s\\\n\" \"IF${NO}_NAME\" \"\${IF${NO}_NAME}\" | |
33 | + eval printf \"%15s : %s\\\n\" \"IF${NO}_MAC\" \"\${IF${NO}_MAC}\" | |
34 | + eval printf \"%15s : %s\\\n\" \"IF${NO}_STATE\" \"\${IF${NO}_STATE}\" | |
35 | + eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV4\" \"\${IF${NO}_IPV4}\" | |
36 | + eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV6\" \"\${IF${NO}_IPV6}\" | |
37 | + NO=$((NO+1)) | |
38 | +done | |
39 | + | |
40 | +${PRINTF} "%15s : %s\n" "CLASSES" "${CLASSES}" | |
41 | + | |
42 | +echo | |
43 | +echo $(mask2cidr 255.255.128.0) | |
44 | +echo $(cidr2mask 17) | |
45 | +eval echo \$\(cidr2mask $(mask2cidr 255.255.128.0)\) | |
46 | + | ... | ... |
1 | 1 | #!/bin/dash |
2 | 2 | |
3 | 3 | ## |
4 | -# This is a first test what static informations I can gather from a | |
5 | -# system. The minimal requirement are the IP addresses because | |
6 | -# on these I build up my classes. | |
7 | -# The system type is also important...we might need to be running | |
8 | -# on variuous UNIX flavours. (But for now we focus on various linux | |
9 | -# flavours and maybe FreeBSD. | |
4 | +# This creates function that will gather some system informations | |
5 | +# and propagate them as environment variables. | |
10 | 6 | # |
11 | 7 | |
12 | - | |
13 | -# Function calculates number of bit in a netmask | |
14 | -# | |
15 | -mask2cidr() { | |
16 | - local NBITS=0 | |
17 | - local OLDIFS="${IFS}" | |
18 | - IFS=. | |
19 | - | |
20 | - for DEC in $1 | |
21 | - do | |
22 | - case ${DEC} in | |
23 | - 255) NBITS=$((NBITS+8));; | |
24 | - 254) NBITS=$((NBITS+7));; | |
25 | - 252) NBITS=$((NBITS+6));; | |
26 | - 248) NBITS=$((NBITS+5));; | |
27 | - 240) NBITS=$((NBITS+4));; | |
28 | - 224) NBITS=$((NBITS+3));; | |
29 | - 192) NBITS=$((NBITS+2));; | |
30 | - 128) NBITS=$((NBITS+1));; | |
31 | - 0);; | |
32 | - *) NBITS=0; IFS="${OLDIFS}"; return 1 | |
33 | - esac | |
34 | - done | |
35 | - IFS="${OLDIFS}" | |
36 | - ${ECHO} "${NBITS}" | |
37 | -} | |
38 | - | |
39 | -cidr2mask() { | |
40 | - local MASK="" | |
41 | - local FULL_OCTETS=$((${1}/8)) | |
42 | - local PARTIAL_OCTET=$((${1}%8)) | |
43 | - | |
44 | - for I in 0 1 2 3 | |
45 | - do | |
46 | - if [ ${I} -lt ${FULL_OCTETS} ] | |
47 | - then | |
48 | - MASK="${MASK}255" | |
49 | - elif [ ${I} -eq ${FULL_OCTETS} ] | |
50 | - then | |
51 | - MASK="${MASK}$((256-(1<<(8-${PARTIAL_OCTET}))))" | |
52 | - else | |
53 | - MASK="${MASK}0" | |
54 | - fi | |
55 | - ${TEST} ${I} -lt 3 && MASK="${MASK}." | |
56 | - done | |
57 | - | |
58 | - ${ECHO} ${MASK} | |
59 | -} | |
60 | - | |
61 | -## | |
62 | -# retrieve interfaces. I prefer using ip if it is | |
63 | -# available, else I fallback to ifconfig... don't know | |
64 | -# how to retrieve this information on other systems. | |
65 | -# | |
66 | -get_if_data() { | |
67 | - if [ ${IP} ] | |
68 | - then | |
69 | - eval $(${IP} -o link | ${AWK} '{ | |
70 | - sub(/:/,"",$1); | |
71 | - no=$1; | |
72 | - sub(/:/,"",$2); | |
73 | - name=$2; | |
74 | - for (i=3; i<NF; i++) { | |
75 | - if ($i == "state") { | |
76 | - i++; state=$i | |
77 | - } | |
78 | - if ($i ~ /link/) { | |
79 | - i++; mac=$i; | |
80 | - classes=classes mac " " | |
81 | - } | |
82 | - } | |
83 | - print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";"; | |
84 | - if ("UP" == state) classes=classes mac " " | |
85 | - } | |
86 | - BEGIN { | |
87 | - classes="" | |
88 | - } | |
89 | - END { | |
90 | - print "CLASSES=\"${CLASSES}" classes " \";"; | |
91 | - print "NINTERFACES=" FNR ";" | |
92 | - }') | |
93 | - | |
94 | - eval $(${IP} -o addr | ${AWK} '{ | |
95 | - sub(/:/,"",$1); | |
96 | - no=$1; | |
97 | - if ($3 == "inet") { | |
98 | - sub(/[\/%].*/,"",$4); | |
99 | - print "IF" no "_IPV4=\"${IF" no "_IPV4}" $4 " \";"; | |
100 | - classes=classes $4 " " | |
101 | - } | |
102 | - if ($3 == "inet6") { | |
103 | - sub(/[\/%].*/,"",$4); | |
104 | - print "IF" no "_IPV6=\"${IF" no "_IPV6}" $4 " \";"; | |
105 | - classes=classes $4 " " | |
106 | - } | |
107 | - } | |
108 | - BEGIN { | |
109 | - classes="" | |
110 | - } | |
111 | - END { | |
112 | - print "CLASSES=\"${CLASSES}" classes " \";" | |
113 | - }') | |
114 | - else | |
115 | - if [ ${IFCONFIG} ] | |
116 | - then | |
117 | - #eval $(${IFCONFIG} -a | ${AWK} '{ | |
118 | - eval $(ifconfig -a | awk ' | |
119 | - /ether/ { mac=$2 } | |
120 | - /inet / { ipv4=ipv4 $2 " " } | |
121 | - /inet6/ { ipv6=ipv6 $2 " " } | |
122 | - /^[^ \t]/ { | |
123 | - if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN"; | |
124 | - if ("" != name) { | |
125 | - print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \ | |
126 | - "IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \ | |
127 | - "IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";"; | |
128 | - no++; | |
129 | - } | |
130 | - ipv4=ipv6=""; | |
131 | - sub(/:/,"",$1); | |
132 | - name=$1 | |
133 | - } | |
134 | - BEGIN { | |
135 | - no=1; | |
136 | - } | |
137 | - END { | |
138 | - if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN"; | |
139 | - print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \ | |
140 | - "IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \ | |
141 | - "IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";"; | |
142 | - print "NINTERFACES=" no | |
143 | - }') | |
144 | - else | |
145 | - ${LOGGER} -p local0.warn 'Found no way to retrieve interface information.' | |
146 | - fi | |
147 | - fi | |
148 | -} | |
8 | +. ./utils.sh | |
149 | 9 | |
150 | 10 | ## |
151 | -# start guessing the system type | |
152 | -# 1. get informations via uname | |
11 | +# start guessing the system type via uname and export it. | |
153 | 12 | # |
154 | 13 | get_host_info() { |
155 | 14 | OS="$(${UNAME} -o)" |
... | ... | @@ -159,6 +18,8 @@ get_host_info() { |
159 | 18 | HOSTNAME="$(${UNAME} -n)" |
160 | 19 | CLASSES="$(${ECHO} -e "${OS}\n${KERNEL}\n${VERSION}\n${PLATFORM}\n${HOSTNAME}" |\ |
161 | 20 | ${SORT} -u | ${TR} "\n" " ")" |
21 | + | |
22 | + export OS KERNEL VERSION PLATFORM HOSTNAME CLASSES | |
162 | 23 | } |
163 | 24 | |
164 | 25 | ## |
... | ... | @@ -186,10 +47,7 @@ get_host_info() { |
186 | 47 | # the file. |
187 | 48 | # |
188 | 49 | get_dist_info() { |
189 | - if [ -z "${KERNEL}" ] | |
190 | - then | |
191 | - get_host_info | |
192 | - fi | |
50 | + [ -z "${KERNEL}" ] && get_host_info | |
193 | 51 | |
194 | 52 | if [ "Linux" = "${KERNEL}" ] |
195 | 53 | then |
... | ... | @@ -229,7 +87,7 @@ get_dist_info() { |
229 | 87 | |
230 | 88 | for DIR in ${FILES} |
231 | 89 | do |
232 | - ${TEST} -f ${DIR} || CHK=$((CHK&~CUR)) | |
90 | + [ -f ${DIR} ] || CHK=$((CHK&~CUR)) | |
233 | 91 | done |
234 | 92 | |
235 | 93 | CUR=$((CUR*2)) |
... | ... | @@ -237,16 +95,16 @@ get_dist_info() { |
237 | 95 | |
238 | 96 | DIST="Unknown" |
239 | 97 | |
240 | - ${TEST} ${CHK} -eq ${SUSE} && DIST="Suse" | |
241 | - ${TEST} ${CHK} -eq ${REDHAT} && DIST="Redhat" | |
242 | - ${TEST} ${CHK} -eq ${FEDORA} && DIST="Fedora" | |
243 | - ${TEST} ${CHK} -eq ${SLACKWARE} && DIST="Slakware" | |
244 | - ${TEST} ${CHK} -eq ${DEBIAN} && DIST="Debian" | |
245 | - ${TEST} ${CHK} -eq ${MANDRAKE} && DIST="Mandrake" | |
246 | - ${TEST} ${CHK} -eq ${YELLOWDOG} && DIST="Yellowdog" | |
247 | - ${TEST} ${CHK} -eq ${SUNJDS} && DIST="Sun" | |
248 | - ${TEST} ${CHK} -eq ${GENTOO} && DIST="Gentoo" | |
249 | - ${TEST} ${CHK} -eq ${SOLARIS} && DIST="Solaris" | |
98 | + [ ${CHK} -eq ${SUSE} ] && DIST="Suse" | |
99 | + [ ${CHK} -eq ${REDHAT} ] && DIST="Redhat" | |
100 | + [ ${CHK} -eq ${FEDORA} ] && DIST="Fedora" | |
101 | + [ ${CHK} -eq ${SLACKWARE} ] && DIST="Slakware" | |
102 | + [ ${CHK} -eq ${DEBIAN} ] && DIST="Debian" | |
103 | + [ ${CHK} -eq ${MANDRAKE} ] && DIST="Mandrake" | |
104 | + [ ${CHK} -eq ${YELLOWDOG} ] && DIST="Yellowdog" | |
105 | + [ ${CHK} -eq ${SUNJDS} ] && DIST="Sun" | |
106 | + [ ${CHK} -eq ${GENTOO} ] && DIST="Gentoo" | |
107 | + [ ${CHK} -eq ${SOLARIS} ] && DIST="Solaris" | |
250 | 108 | |
251 | 109 | if [ 'Unknown' != "${DIST}" ] |
252 | 110 | then |
... | ... | @@ -257,59 +115,14 @@ get_dist_info() { |
257 | 115 | else |
258 | 116 | DIST="${OS}" |
259 | 117 | fi |
260 | -} | |
261 | 118 | |
262 | -## | |
263 | -# Here now starts the usage of the functions defined above. | |
264 | -# | |
265 | - | |
266 | -## | |
267 | -# first specify some programs | |
268 | -# | |
269 | -WHICH="/usr/bin/which" | |
270 | -UNAME="$(${WHICH} uname)" | |
271 | -TEST="$(${WHICH} test)" | |
272 | -GREP="$(${WHICH} grep)" | |
273 | -AWK="$(${WHICH} awk)" | |
274 | -ECHO="$(${WHICH} echo)" | |
275 | -SORT="$(${WHICH} sort)" | |
276 | -TR="$(${WHICH} tr)" | |
277 | -PRINTF="$(${WHICH} printf)" | |
278 | -LOGGER="$(${WHICH} logger)" | |
279 | -IP="$(${WHICH} ip)" | |
280 | -IFCONFIG="$(${WHICH} ifconfig)" | |
281 | - | |
282 | -get_dist_info | |
283 | -get_if_data | |
119 | + export DIST CLASSES | |
120 | +} | |
284 | 121 | |
285 | 122 | ## |
286 | -# report everysthing | |
123 | +# autorun this if sourced. | |
287 | 124 | # |
288 | 125 | |
289 | -${PRINTF} "%15s : %s\n" "OS" "${OS}" | |
290 | -${PRINTF} "%15s : %s\n" "KERNEL" "${KERNEL}" | |
291 | -${PRINTF} "%15s : %s\n" "VERSION" "${VERSION}" | |
292 | -${PRINTF} "%15s : %s\n" "PLATFORM" "${PLATFORM}" | |
293 | -${PRINTF} "%15s : %s\n" "DIST" "${DIST}" | |
294 | -${PRINTF} "%15s : %s\n" "HOSTNAME" "${HOSTNAME}" | |
295 | -${PRINTF} "%15s : %s\n" "# INTERFACES" "${NINTERFACES}" | |
296 | - | |
297 | -NO=1 | |
298 | -while [ ${NO} -le ${NINTERFACES:=0} ] | |
299 | -do | |
300 | - eval printf \"%15s : %s\\\n\" \"IF${NO}_NAME\" \"\${IF${NO}_NAME}\" | |
301 | - eval printf \"%15s : %s\\\n\" \"IF${NO}_MAC\" \"\${IF${NO}_MAC}\" | |
302 | - eval printf \"%15s : %s\\\n\" \"IF${NO}_STATE\" \"\${IF${NO}_STATE}\" | |
303 | - eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV4\" \"\${IF${NO}_IPV4}\" | |
304 | - eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV6\" \"\${IF${NO}_IPV6}\" | |
305 | - NO=$((NO+1)) | |
306 | -done | |
307 | - | |
308 | -${PRINTF} "%15s : %s\n" "CLASSES" "${CLASSES}" | |
309 | - | |
310 | -echo | |
311 | -echo $(mask2cidr 255.255.128.0) | |
312 | -echo $(cidr2mask 17) | |
313 | -eval echo \$\(cidr2mask $(mask2cidr 255.255.128.0)\) | |
126 | +[ -z "${DIST}" ] && get_dist_info | |
314 | 127 | |
315 | 128 | # vim: set ts=4 sw=4: | ... | ... |
utils.sh
0 → 100755
1 | +#!/bin/dash | |
2 | + | |
3 | +## | |
4 | +# make definitions only when not already defined | |
5 | +# | |
6 | + | |
7 | +command -v filename2symbol >/dev/null | |
8 | +if [ $? -ne 0 ] | |
9 | +then | |
10 | + filename2symbol() { | |
11 | + local SYM="${1}" | |
12 | + | |
13 | + if [ 1 -ne $# ] | |
14 | + then | |
15 | + logger -p syslog.err 'filename2sybol: missing filename' | |
16 | + exit 1 | |
17 | + fi | |
18 | + | |
19 | + while [ "${SYM}" != "${SYM%/*}" -o "${SYM}" != "${SYM%.*}" ] | |
20 | + do | |
21 | + if [ "${SYM}" != "${SYM%.*}" ] | |
22 | + then | |
23 | + SYM="${SYM%%.*}_${SYM#*.}" | |
24 | + else | |
25 | + SYM="${SYM%%/*}_${SYM#*/}" | |
26 | + fi | |
27 | + done | |
28 | + | |
29 | + echo "${SYM}" | |
30 | + } | |
31 | + | |
32 | + canonify_name() { | |
33 | + local FILE="${1}" | |
34 | + local DIR="${PWD}" | |
35 | + | |
36 | + # if FILE starts with a / its already canonified | |
37 | + if [ "${FILE}" != ${FILE#/} ] | |
38 | + then | |
39 | + echo "${FILE}" | |
40 | + return 0 | |
41 | + fi | |
42 | + | |
43 | + # simply remove ./ and make a ../ remove a part from pwd | |
44 | + while [ "${FILE}" != ${FILE#./} -o "${FILE}" != ${FILE#../} ] | |
45 | + do | |
46 | + if [ "${FILE}" != ${FILE#./} ] | |
47 | + then | |
48 | + FILE="${FILE#./}" | |
49 | + else | |
50 | + FILE="${FILE#../}" | |
51 | + DIR="${DIR%/*}" | |
52 | + fi | |
53 | + done | |
54 | + | |
55 | + echo "${DIR}/${FILE}" | |
56 | + } | |
57 | + | |
58 | + include_once() { | |
59 | + local FILE="$(canonify_name "${1}")" | |
60 | + local SYM="$(filename2symbol "${FILE}")" | |
61 | + | |
62 | + if [ 1 -ne $# ] | |
63 | + then | |
64 | + logger -p syslog.err 'include_once: missing filename' | |
65 | + exit 1 | |
66 | + fi | |
67 | + | |
68 | + if eval [ -z \"\${SOURCED_${SYM}}\" ] | |
69 | + then | |
70 | + . ${FILE} | |
71 | + eval export SOURCED_${SYM}=\"\${FILE}\" | |
72 | + fi | |
73 | + } | |
74 | + | |
75 | + ## | |
76 | + # get and export the locations of used programs and export them | |
77 | + # I assume that on every UNIX/shell variant at least [ is globally | |
78 | + # available. I will use it throughout all other scripts without | |
79 | + # checking for existence. | |
80 | + # I also assume through all scripts that eval is available. | |
81 | + # | |
82 | + [ -z "${WHICH}" ] && WHICH="/usr/bin/which" | |
83 | + [ -z "${UNAME}" ] && UNAME="$(${WHICH} uname)" | |
84 | + [ -z "${GREP}" ] && GREP="$(${WHICH} grep)" | |
85 | + [ -z "${AWK}" ] && AWK="$(${WHICH} awk)" | |
86 | + [ -z "${ECHO}" ] && ECHO="$(${WHICH} echo)" | |
87 | + [ -z "${SORT}" ] && SORT="$(${WHICH} sort)" | |
88 | + [ -z "${TR}" ] && TR="$(${WHICH} tr)" | |
89 | + [ -z "${PRINTF}" ] && PRINTF="$(${WHICH} printf)" | |
90 | + [ -z "${LOGGER}" ] && LOGGER="$(${WHICH} logger)" | |
91 | + [ -z "${IP}" ] && IP="$(${WHICH} ip)" | |
92 | + [ -z "${IFCONFIG}" ] && IFCONFIG="$(${WHICH} ifconfig)" | |
93 | + | |
94 | + export WHICH UNAME GREP AWK ECHO SORT TR PRINTF LOGGER IP IFCONFIG | |
95 | +fi | |
96 | + | |
97 | +# vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment