Browse Source
seperate original script in several of special purpose and implement some kind of include_once for sourcing scripts only once
master
seperate original script in several of special purpose and implement some kind of include_once for sourcing scripts only once
master
5 changed files with 352 additions and 209 deletions
-
130interface_information.sh
-
57network_tools.sh
-
46run.sh
-
231system_information.sh
-
97utils.sh
@ -0,0 +1,130 @@ |
|||||
|
#!/bin/dash |
||||
|
|
||||
|
## |
||||
|
# This creates function that will gather some system informations |
||||
|
# and propagate them as environment variables. |
||||
|
# |
||||
|
|
||||
|
. ./utils.sh |
||||
|
|
||||
|
## |
||||
|
# retrieve interfaces. I prefer using ip if it is |
||||
|
# available, else I fallback to ifconfig... don't know |
||||
|
# how to retrieve this information on other systems. |
||||
|
# This also exports the environment variables. |
||||
|
# |
||||
|
get_interface_data() { |
||||
|
local NO=1 |
||||
|
|
||||
|
if [ ${IP} ] |
||||
|
then |
||||
|
get_interface_data_ip |
||||
|
else |
||||
|
if [ ${IFCONFIG} ] |
||||
|
then |
||||
|
get_interface_data_ifconfig |
||||
|
else |
||||
|
${LOGGER} -p local0.warn 'Found no way to retrieve interface information.' |
||||
|
fi |
||||
|
fi |
||||
|
|
||||
|
export NINTERFACES |
||||
|
export CLASSES |
||||
|
|
||||
|
while [ ${NO} -le ${NINTERFACES:=0} ] |
||||
|
do |
||||
|
export IF${NO}_NAME IF${NO}_MAC IF${NO}_STATE IF${NO}_IPV4 IF${NO}_IPV6 |
||||
|
NO=$((NO+1)) |
||||
|
done |
||||
|
} |
||||
|
|
||||
|
## |
||||
|
# get the interface information from the ip tool |
||||
|
# |
||||
|
get_interface_data_ip() { |
||||
|
eval $(${IP} -o link | ${AWK} '{ |
||||
|
sub(/:/,"",$1); |
||||
|
no=$1; |
||||
|
sub(/:/,"",$2); |
||||
|
name=$2; |
||||
|
for (i=3; i<NF; i++) { |
||||
|
if ($i == "state") { |
||||
|
i++; state=$i |
||||
|
} |
||||
|
if ($i ~ /link/) { |
||||
|
i++; mac=$i; |
||||
|
classes=classes mac " " |
||||
|
} |
||||
|
} |
||||
|
print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";"; |
||||
|
if ("UP" == state) classes=classes mac " " |
||||
|
} |
||||
|
BEGIN { |
||||
|
classes="" |
||||
|
} |
||||
|
END { |
||||
|
print "CLASSES=\"${CLASSES}" classes " \";"; |
||||
|
print "NINTERFACES=" FNR ";" |
||||
|
}') |
||||
|
|
||||
|
eval $(${IP} -o addr | ${AWK} '{ |
||||
|
sub(/:/,"",$1); |
||||
|
no=$1; |
||||
|
if ($3 == "inet") { |
||||
|
sub(/[\/%].*/,"",$4); |
||||
|
print "IF" no "_IPV4=\"${IF" no "_IPV4}" $4 " \";"; |
||||
|
classes=classes $4 " " |
||||
|
} |
||||
|
if ($3 == "inet6") { |
||||
|
sub(/[\/%].*/,"",$4); |
||||
|
print "IF" no "_IPV6=\"${IF" no "_IPV6}" $4 " \";"; |
||||
|
classes=classes $4 " " |
||||
|
} |
||||
|
} |
||||
|
BEGIN { |
||||
|
classes="" |
||||
|
} |
||||
|
END { |
||||
|
print "CLASSES=\"${CLASSES}" classes " \";" |
||||
|
}') |
||||
|
} |
||||
|
|
||||
|
## |
||||
|
# get interface data via the ifconfig tool |
||||
|
# |
||||
|
get_interface_data_ifconfig() { |
||||
|
eval $(${IFCONFIG} -a | ${AWK} ' |
||||
|
/ether/ { mac=$2 } |
||||
|
/inet / { ipv4=ipv4 $2 " " } |
||||
|
/inet6/ { ipv6=ipv6 $2 " " } |
||||
|
/^[^ \t]/ { |
||||
|
if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN"; |
||||
|
if ("" != name) { |
||||
|
print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \ |
||||
|
"IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \ |
||||
|
"IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";"; |
||||
|
no++; |
||||
|
} |
||||
|
ipv4=ipv6=""; |
||||
|
sub(/:/,"",$1); |
||||
|
name=$1 |
||||
|
} |
||||
|
BEGIN { |
||||
|
no=1; |
||||
|
} |
||||
|
END { |
||||
|
if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN"; |
||||
|
print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \ |
||||
|
"IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \ |
||||
|
"IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";"; |
||||
|
print "NINTERFACES=" no |
||||
|
}') |
||||
|
} |
||||
|
|
||||
|
## |
||||
|
# autorun this if sourced. |
||||
|
# |
||||
|
|
||||
|
[ -z "${NINTERFACES}" ] && get_interface_data |
||||
|
|
||||
|
# vim: set ts=4 sw=4: |
||||
@ -0,0 +1,57 @@ |
|||||
|
#!/bin/dash |
||||
|
|
||||
|
. ./utils.sh |
||||
|
|
||||
|
## |
||||
|
# Function calculates number of bit in a netmask |
||||
|
# |
||||
|
mask2cidr() { |
||||
|
local NBITS=0 |
||||
|
local OLDIFS="${IFS}" |
||||
|
IFS=. |
||||
|
|
||||
|
for DEC in $1 |
||||
|
do |
||||
|
case ${DEC} in |
||||
|
255) NBITS=$((NBITS+8));; |
||||
|
254) NBITS=$((NBITS+7));; |
||||
|
252) NBITS=$((NBITS+6));; |
||||
|
248) NBITS=$((NBITS+5));; |
||||
|
240) NBITS=$((NBITS+4));; |
||||
|
224) NBITS=$((NBITS+3));; |
||||
|
192) NBITS=$((NBITS+2));; |
||||
|
128) NBITS=$((NBITS+1));; |
||||
|
0);; |
||||
|
*) NBITS=0; IFS="${OLDIFS}"; return 1 |
||||
|
esac |
||||
|
done |
||||
|
IFS="${OLDIFS}" |
||||
|
${ECHO} "${NBITS}" |
||||
|
} |
||||
|
|
||||
|
## |
||||
|
# Function calculates a netmask from number of bits |
||||
|
# |
||||
|
cidr2mask() { |
||||
|
local I MASK="" |
||||
|
local FULL_OCTETS=$((${1}/8)) |
||||
|
local PARTIAL_OCTET=$((${1}%8)) |
||||
|
|
||||
|
for I in 0 1 2 3 |
||||
|
do |
||||
|
if [ ${I} -lt ${FULL_OCTETS} ] |
||||
|
then |
||||
|
MASK="${MASK}255" |
||||
|
elif [ ${I} -eq ${FULL_OCTETS} ] |
||||
|
then |
||||
|
MASK="${MASK}$((256-(1<<(8-${PARTIAL_OCTET}))))" |
||||
|
else |
||||
|
MASK="${MASK}0" |
||||
|
fi |
||||
|
[ ${I} -lt 3 ] && MASK="${MASK}." |
||||
|
done |
||||
|
|
||||
|
${ECHO} ${MASK} |
||||
|
} |
||||
|
|
||||
|
# vim: set ts=4 sw=4: |
||||
@ -0,0 +1,46 @@ |
|||||
|
#!/bin/dash |
||||
|
|
||||
|
## |
||||
|
# This is a first test what static informations I can gather from a |
||||
|
# system. The minimal requirement are the IP addresses because |
||||
|
# on these I build up my classes. |
||||
|
# The system type is also important...we might need to be running |
||||
|
# on variuous UNIX flavours. (But for now we focus on various linux |
||||
|
# flavours and maybe FreeBSD. |
||||
|
# |
||||
|
|
||||
|
. ./utils.sh |
||||
|
include_once ./system_information.sh |
||||
|
include_once ../sysman/interface_information.sh |
||||
|
include_once /home/ghopp/sysman/network_tools.sh |
||||
|
|
||||
|
## |
||||
|
# report everysthing |
||||
|
# |
||||
|
|
||||
|
${PRINTF} "%15s : %s\n" "OS" "${OS}" |
||||
|
${PRINTF} "%15s : %s\n" "KERNEL" "${KERNEL}" |
||||
|
${PRINTF} "%15s : %s\n" "VERSION" "${VERSION}" |
||||
|
${PRINTF} "%15s : %s\n" "PLATFORM" "${PLATFORM}" |
||||
|
${PRINTF} "%15s : %s\n" "DIST" "${DIST}" |
||||
|
${PRINTF} "%15s : %s\n" "HOSTNAME" "${HOSTNAME}" |
||||
|
${PRINTF} "%15s : %s\n" "# INTERFACES" "${NINTERFACES}" |
||||
|
|
||||
|
NO=1 |
||||
|
while [ ${NO} -le ${NINTERFACES:=0} ] |
||||
|
do |
||||
|
eval printf \"%15s : %s\\\n\" \"IF${NO}_NAME\" \"\${IF${NO}_NAME}\" |
||||
|
eval printf \"%15s : %s\\\n\" \"IF${NO}_MAC\" \"\${IF${NO}_MAC}\" |
||||
|
eval printf \"%15s : %s\\\n\" \"IF${NO}_STATE\" \"\${IF${NO}_STATE}\" |
||||
|
eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV4\" \"\${IF${NO}_IPV4}\" |
||||
|
eval printf \"%15s : %s\\\n\" \"IF${NO}_IPV6\" \"\${IF${NO}_IPV6}\" |
||||
|
NO=$((NO+1)) |
||||
|
done |
||||
|
|
||||
|
${PRINTF} "%15s : %s\n" "CLASSES" "${CLASSES}" |
||||
|
|
||||
|
echo |
||||
|
echo $(mask2cidr 255.255.128.0) |
||||
|
echo $(cidr2mask 17) |
||||
|
eval echo \$\(cidr2mask $(mask2cidr 255.255.128.0)\) |
||||
|
|
||||
@ -0,0 +1,97 @@ |
|||||
|
#!/bin/dash |
||||
|
|
||||
|
## |
||||
|
# make definitions only when not already defined |
||||
|
# |
||||
|
|
||||
|
command -v filename2symbol >/dev/null |
||||
|
if [ $? -ne 0 ] |
||||
|
then |
||||
|
filename2symbol() { |
||||
|
local SYM="${1}" |
||||
|
|
||||
|
if [ 1 -ne $# ] |
||||
|
then |
||||
|
logger -p syslog.err 'filename2sybol: missing filename' |
||||
|
exit 1 |
||||
|
fi |
||||
|
|
||||
|
while [ "${SYM}" != "${SYM%/*}" -o "${SYM}" != "${SYM%.*}" ] |
||||
|
do |
||||
|
if [ "${SYM}" != "${SYM%.*}" ] |
||||
|
then |
||||
|
SYM="${SYM%%.*}_${SYM#*.}" |
||||
|
else |
||||
|
SYM="${SYM%%/*}_${SYM#*/}" |
||||
|
fi |
||||
|
done |
||||
|
|
||||
|
echo "${SYM}" |
||||
|
} |
||||
|
|
||||
|
canonify_name() { |
||||
|
local FILE="${1}" |
||||
|
local DIR="${PWD}" |
||||
|
|
||||
|
# if FILE starts with a / its already canonified |
||||
|
if [ "${FILE}" != ${FILE#/} ] |
||||
|
then |
||||
|
echo "${FILE}" |
||||
|
return 0 |
||||
|
fi |
||||
|
|
||||
|
# simply remove ./ and make a ../ remove a part from pwd |
||||
|
while [ "${FILE}" != ${FILE#./} -o "${FILE}" != ${FILE#../} ] |
||||
|
do |
||||
|
if [ "${FILE}" != ${FILE#./} ] |
||||
|
then |
||||
|
FILE="${FILE#./}" |
||||
|
else |
||||
|
FILE="${FILE#../}" |
||||
|
DIR="${DIR%/*}" |
||||
|
fi |
||||
|
done |
||||
|
|
||||
|
echo "${DIR}/${FILE}" |
||||
|
} |
||||
|
|
||||
|
include_once() { |
||||
|
local FILE="$(canonify_name "${1}")" |
||||
|
local SYM="$(filename2symbol "${FILE}")" |
||||
|
|
||||
|
if [ 1 -ne $# ] |
||||
|
then |
||||
|
logger -p syslog.err 'include_once: missing filename' |
||||
|
exit 1 |
||||
|
fi |
||||
|
|
||||
|
if eval [ -z \"\${SOURCED_${SYM}}\" ] |
||||
|
then |
||||
|
. ${FILE} |
||||
|
eval export SOURCED_${SYM}=\"\${FILE}\" |
||||
|
fi |
||||
|
} |
||||
|
|
||||
|
## |
||||
|
# get and export the locations of used programs and export them |
||||
|
# I assume that on every UNIX/shell variant at least [ is globally |
||||
|
# available. I will use it throughout all other scripts without |
||||
|
# checking for existence. |
||||
|
# I also assume through all scripts that eval is available. |
||||
|
# |
||||
|
[ -z "${WHICH}" ] && WHICH="/usr/bin/which" |
||||
|
[ -z "${UNAME}" ] && UNAME="$(${WHICH} uname)" |
||||
|
[ -z "${GREP}" ] && GREP="$(${WHICH} grep)" |
||||
|
[ -z "${AWK}" ] && AWK="$(${WHICH} awk)" |
||||
|
[ -z "${ECHO}" ] && ECHO="$(${WHICH} echo)" |
||||
|
[ -z "${SORT}" ] && SORT="$(${WHICH} sort)" |
||||
|
[ -z "${TR}" ] && TR="$(${WHICH} tr)" |
||||
|
[ -z "${PRINTF}" ] && PRINTF="$(${WHICH} printf)" |
||||
|
[ -z "${LOGGER}" ] && LOGGER="$(${WHICH} logger)" |
||||
|
[ -z "${IP}" ] && IP="$(${WHICH} ip)" |
||||
|
[ -z "${IFCONFIG}" ] && IFCONFIG="$(${WHICH} ifconfig)" |
||||
|
|
||||
|
export WHICH UNAME GREP AWK ECHO SORT TR PRINTF LOGGER IP IFCONFIG |
||||
|
fi |
||||
|
|
||||
|
# vim: set ts=4 sw=4: |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue