From d4c6cd5eb87c4c0623dbebea898ccbb0ae41cfa4 Mon Sep 17 00:00:00 2001 From: ThomasC Date: Wed, 13 Apr 2022 08:23:33 +0200 Subject: [PATCH] added some more functions - user and group check, permissions check, countfiles, readfiles, count line in files, md5 file ... --- functions.sh | 78 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/functions.sh b/functions.sh index 4d4a9ea..c64c299 100755 --- a/functions.sh +++ b/functions.sh @@ -40,7 +40,7 @@ function fileMTime { function fileNumLines { if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi n=$(wc -l $1 | awk '{print $1}') - echo -ne $n; + echo -ne "$n;" } @@ -193,10 +193,17 @@ function addHeader { echo -ne "${user}$(hostname -s);" } +# get file content +# arg1: file to read +function getFileContent { + test -f $1 && content="$(cat $1 | sed 's/\n//');" + echo -ne "${content}" +} + # get file md5 # arg1: file to checksum function md5 { - test -f "$1" && local am=$(md5sum "$1" | awk '{print $1}') + test -f "$2" && local am=$(md5sum "$1" | awk '{print $1}') echo -ne "${am};" } @@ -230,10 +237,30 @@ function pkgNotInstalled { echo -ne "${?};" } +# WIP: dont work + +# ok if given process is listening on given port +# arg1: chroot +# arg2: process to check +# arg3: port +function processIsListening { + chroot=$1 + process=$2 + port=$3 + chroot $chroot bash -c "ss -taupen|grep -q \"LISTEN.*:${port}.*${process}\"" + test $? -eq 0 && e 1 || e 0 +} + +# WIP: dont work + # ok if given process is running -# arg1: process to check +# arg1: chroot +# arg2: process to check function processIsRunning { - if ( pgrep -f $1 &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi + chroot=$1 + process=$2 + chroot $chroot bash -c "ps -ef | grep -q $process" + test $? -ne 0 && e 1 || e 0 } #Ok if given command returns 0 @@ -249,6 +276,49 @@ function commandIsWorking { } +# get num of files in given dir +# arg1: dir +function numFilesInDir { + local dir=$1 + test -d $dir || { e 0 ; return 0 ; } + local n=$(ls -l $dir | wc -l) + e $n +} + +# check wether file mode is correct +# arg1: file +# arg2: mode +function modeIsCorrect { + file=$1 + mode=$2 + if [ ! -e $1 ] ; then e 0 ; return 0 ; fi + current_mode=$(stat --format "%a" $1) + if [ "$mode" = "$current_mode" ] ; then e 1 ; else e 0 ; fi +} + + +# check wether file owner is correct +# arg1: file +# arg2: owner +function ownerIsCorrect { + file=$1 + owner=$2 + if [ ! -e $1 ] ; then e 0 ; return 0 ; fi + current_owner=$(stat --format "%u" $1) + if [ "$owner" = "$current_owner" ] ; then e 1 ; else e 0 ; fi +} + +# check wether file group is correct +# arg1: file +# arg2: group +function groupIsCorrect { + file=$1 + group=$2 + if [ ! -e $1 ] ; then e 0 ; return 0 ; fi + current_group=$(stat --format "%g" $1) + if [ "$group" = "$current_group" ] ; then e 1 ; else e 0 ; fi +} + # ok if given pattern is detected on command's stdout or stderr # arg1: chroot # arg2: command