Browse Source

seperate original script in several of special purpose and implement some kind of include_once for sourcing scripts only once

master
Georg Hopp 12 years ago
parent
commit
86fa5b810a
  1. 130
      interface_information.sh
  2. 57
      network_tools.sh
  3. 46
      run.sh
  4. 231
      system_information.sh
  5. 97
      utils.sh

130
interface_information.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:

57
network_tools.sh

@ -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:

46
run.sh

@ -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)\)

231
system_information.sh

@ -1,155 +1,14 @@
#!/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.
# This creates function that will gather some system informations
# and propagate them as environment variables.
#
# 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}"
}
cidr2mask() {
local 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
${TEST} ${I} -lt 3 && MASK="${MASK}."
done
${ECHO} ${MASK}
}
##
# 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.
#
get_if_data() {
if [ ${IP} ]
then
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 " \";"
}')
else
if [ ${IFCONFIG} ]
then
#eval $(${IFCONFIG} -a | ${AWK} '{
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
}')
else
${LOGGER} -p local0.warn 'Found no way to retrieve interface information.'
fi
fi
}
. ./utils.sh
##
# start guessing the system type
# 1. get informations via uname
# start guessing the system type via uname and export it.
#
get_host_info() {
OS="$(${UNAME} -o)"
@ -159,6 +18,8 @@ get_host_info() {
HOSTNAME="$(${UNAME} -n)"
CLASSES="$(${ECHO} -e "${OS}\n${KERNEL}\n${VERSION}\n${PLATFORM}\n${HOSTNAME}" |\
${SORT} -u | ${TR} "\n" " ")"
export OS KERNEL VERSION PLATFORM HOSTNAME CLASSES
}
##
@ -186,10 +47,7 @@ get_host_info() {
# the file.
#
get_dist_info() {
if [ -z "${KERNEL}" ]
then
get_host_info
fi
[ -z "${KERNEL}" ] && get_host_info
if [ "Linux" = "${KERNEL}" ]
then
@ -229,7 +87,7 @@ get_dist_info() {
for DIR in ${FILES}
do
${TEST} -f ${DIR} || CHK=$((CHK&~CUR))
[ -f ${DIR} ] || CHK=$((CHK&~CUR))
done
CUR=$((CUR*2))
@ -237,16 +95,16 @@ get_dist_info() {
DIST="Unknown"
${TEST} ${CHK} -eq ${SUSE} && DIST="Suse"
${TEST} ${CHK} -eq ${REDHAT} && DIST="Redhat"
${TEST} ${CHK} -eq ${FEDORA} && DIST="Fedora"
${TEST} ${CHK} -eq ${SLACKWARE} && DIST="Slakware"
${TEST} ${CHK} -eq ${DEBIAN} && DIST="Debian"
${TEST} ${CHK} -eq ${MANDRAKE} && DIST="Mandrake"
${TEST} ${CHK} -eq ${YELLOWDOG} && DIST="Yellowdog"
${TEST} ${CHK} -eq ${SUNJDS} && DIST="Sun"
${TEST} ${CHK} -eq ${GENTOO} && DIST="Gentoo"
${TEST} ${CHK} -eq ${SOLARIS} && DIST="Solaris"
[ ${CHK} -eq ${SUSE} ] && DIST="Suse"
[ ${CHK} -eq ${REDHAT} ] && DIST="Redhat"
[ ${CHK} -eq ${FEDORA} ] && DIST="Fedora"
[ ${CHK} -eq ${SLACKWARE} ] && DIST="Slakware"
[ ${CHK} -eq ${DEBIAN} ] && DIST="Debian"
[ ${CHK} -eq ${MANDRAKE} ] && DIST="Mandrake"
[ ${CHK} -eq ${YELLOWDOG} ] && DIST="Yellowdog"
[ ${CHK} -eq ${SUNJDS} ] && DIST="Sun"
[ ${CHK} -eq ${GENTOO} ] && DIST="Gentoo"
[ ${CHK} -eq ${SOLARIS} ] && DIST="Solaris"
if [ 'Unknown' != "${DIST}" ]
then
@ -257,59 +115,14 @@ get_dist_info() {
else
DIST="${OS}"
fi
}
##
# Here now starts the usage of the functions defined above.
#
##
# first specify some programs
#
WHICH="/usr/bin/which"
UNAME="$(${WHICH} uname)"
TEST="$(${WHICH} test)"
GREP="$(${WHICH} grep)"
AWK="$(${WHICH} awk)"
ECHO="$(${WHICH} echo)"
SORT="$(${WHICH} sort)"
TR="$(${WHICH} tr)"
PRINTF="$(${WHICH} printf)"
LOGGER="$(${WHICH} logger)"
IP="$(${WHICH} ip)"
IFCONFIG="$(${WHICH} ifconfig)"
get_dist_info
get_if_data
export DIST CLASSES
}
##
# report everysthing
# autorun this if sourced.
#
${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)\)
[ -z "${DIST}" ] && get_dist_info
# vim: set ts=4 sw=4:

97
utils.sh

@ -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:
Loading…
Cancel
Save