Created
March 14, 2023 03:44
-
-
Save ricardojlrufino/975e97d120333d94ae1008e1c2b263d3 to your computer and use it in GitHub Desktop.
Alternativa para https://github.com/rtulke/dynmotd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# dynamic message of the day | |
# Robert Tulke, [email protected] | |
## version | |
VERSION="dynmotd v1.6" | |
## configuration and logfile | |
MAINLOG="/root/.dynmotd/maintenance.log" | |
ENVFILE="/root/.dynmotd/environment.cfg" | |
## enable system related information about your system | |
SYSTEM_INFO="1" # show system information | |
STORAGE_INFO="1" # show storage information | |
USER_INFO="0" # show some user infomration | |
ENVIRONMENT_INFO="0" # show environement information | |
MAINTENANCE_INFO="0" # show maintenance infomration | |
UPDATE_INFO="0" # show update information, deactivate when using redhat | |
VERSION_INFO="0" # show the version banner | |
LIST_LOG_ENTRY="2" # how many log line will be display in MAINTENANCE_INFO | |
## some colors | |
C_BLACK="\033[0;30m" # Black | |
C_DGRAY="\033[1;30m" # Dark Grey | |
C_GREY="\033[0;37m" # Grey | |
C_WHITE="\033[1;37m" # White | |
C_RED="\033[0;31m" # Red | |
C_LRED="\033[1;31m" # Light Red | |
C_BLUE="\033[0;34m" # Blue | |
C_LBLUE="\033[1;34m" # Blue | |
C_CYAN="\033[0;36m" # Cyan | |
C_LCYAN="\033[1;36m" # Light Cyan | |
C_PINK="\033[0;35m" # Purple | |
C_PINK="\033[1;35m" # Light Purple | |
C_GREEN="\033[0;32m" # Green | |
C_LGREEN="\033[1;32m" # Light Green | |
C_BROWN="\033[0;33m" # Brown/Orangen | |
C_YELLOW="\033[1;33m" # Yellow | |
#### color schemes | |
## DOT, day of the tentacle scheme | |
F1=${C_GREY} | |
F2=${C_PINK} | |
F3=${C_LGREEN} | |
F4=${C_RED} | |
## retro hacker | |
#F1=${C_GREEN} | |
#F2=${C_GREEN} | |
#F3=${C_GREEN} | |
#F4=${C_RED} | |
## don't start as root | |
#if [ $(whoami) != root ]; then | |
# cat /etc/motd | |
# exit 0 | |
#fi | |
#### Configuration Part | |
## create .maintenance file if not exist | |
function createmaintenance { | |
if [ ! -f $MAINLOG ]; then | |
DYNMOTDDIR=$(dirname $MAINLOG) | |
mkdir -p $DYNMOTDDIR | |
touch $MAINLOG | |
chmod 600 $MAINLOG | |
echo "new log file created $MAINLOG" | |
echo | |
fi | |
} | |
## create .environment file if not exist | |
function createenv { | |
echo "We want to assign a function name for $(hostname --fqdn)" | |
echo | |
echo -n "System Function, like Webserver, Mailserver [${1}]: " | |
read SYSFUNCTION | |
echo -n "System Environment, like PRD|TST|ITG [${2}]: " | |
read SYSENV | |
echo -n "Service Level Agreement, like SLA1|SLA2|SLA3: [${3}] " | |
read SYSSLA | |
rm -rf $ENVFILE | |
mkdir -p $(dirname $ENVFILE) | |
touch $ENVFILE | |
chmod 600 $ENVFILE | |
echo "SYSENV=\"$SYSENV\"" >> $ENVFILE | |
echo "SYSFUNCTION=\"$SYSFUNCTION\"" >> $ENVFILE | |
echo "SYSSLA=\"$SYSSLA\"" >> $ENVFILE | |
} | |
#### Parameter Part | |
## addlog | |
function addlog () { | |
if [ ! -f "$MAINLOG" ]; then | |
echo "maintenance logfile not found: $MAINLOG try to create a new one..." | |
createmaintenance | |
fi | |
if [ -z "$1" ]; then | |
echo "Usage:" | |
echo | |
echo " ./$(basename $0) -a \"new guest account added\" " | |
echo | |
exit 1 | |
fi | |
mydate=$(date +"%b %d %H:%M:%S") | |
echo $mydate $1 >> $MAINLOG | |
echo "log entry added..." | |
} | |
## rmlog | |
function rmlog () { | |
if [ -z "$1" ]; then | |
echo "Usage: " | |
echo | |
echo " ./$(basename $0) -r [line-number] " | |
echo | |
exit 1 | |
fi | |
re='^[0-9]+$' | |
if ! [[ $1 =~ $re ]] ; then | |
echo "$1 : not a number" | |
exit 1 | |
fi | |
## remove specific line | |
sed -i "$1"'d' $MAINLOG | |
RC=$? | |
if [ $RC = "0" ]; then | |
echo "line $1 successfully deleted..." | |
else | |
echo "something went wrong" | |
exit 1 | |
fi | |
} | |
## listlog | |
function listlog () { | |
if [ ! -f "$MAINLOG" ]; then | |
echo "Maintenance Logfile not found: $MAINLOG" | |
createmaintenance | |
fi | |
COUNT=1 | |
while read line; do | |
NAME=$line; | |
echo -e "${F2}$COUNT ${F1}$NAME${F2}" | |
COUNT=$((COUNT+1)) | |
done < $MAINLOG | |
} | |
#### Output Part | |
## System Info | |
function show_system_info () { | |
if [ "$SYSTEM_INFO" = "1" ]; then | |
## get my fqdn hostname.domain.name.tld | |
HOSTNAME=$(hostname --fqdn) | |
## get my main ip | |
IP=$(host $HOSTNAME |grep "has address" |head -n1 |awk {'print $4'}) | |
## get current kernel version | |
UNAME=$(uname -r) | |
## get runnig sles distribution name | |
DISTRIBUTION=$(lsb_release -s -d) | |
## get hardware platform | |
PLATFORM=$(uname -m) | |
## get system uptime | |
UPTIME=$(uptime |cut -c2- |cut -d, -f1) | |
## get amount of cpu processors | |
CPUS=$(cat /proc/cpuinfo|grep processor|wc -l) | |
## get system cpu model | |
CPUMODEL=$(cat /proc/cpuinfo |egrep 'model name' |uniq |awk -F ': ' {'print $2'}) | |
## get current free memory | |
MEMFREE=$(echo $(cat /proc/meminfo |egrep MemFree |awk {'print $2'})/1024 |bc) | |
## get maxium usable memory | |
MEMMAX=$(echo $(cat /proc/meminfo |egrep MemTotal |awk {'print $2'})/1024 |bc) | |
## get current free swap space | |
SWAPFREE=$(echo $(cat /proc/meminfo |egrep SwapFree |awk {'print $2'})/1024 |bc) | |
## get maxium usable swap space | |
SWAPMAX=$(echo $(cat /proc/meminfo |egrep SwapTotal |awk {'print $2'})/1024 |bc) | |
## get current procs | |
PROCCOUNT=$(ps -Afl |egrep -v 'ps|wc' |wc -l) | |
## get maxium usable procs | |
PROCMAX=$(ulimit -u) | |
## display system information | |
echo -e " | |
${F2}============[ ${F1}System Info${F2} ]==================================================== | |
${F1} Address ${F2}= ${F3}$IP | |
${F1} Uptime ${F2}= ${F3}$UPTIME | |
${F1} Memory ${F2}= ${F3}$MEMFREE MB Free of $MEMMAX MB Total | |
${F1} Swap Memory ${F2}= ${F3}$SWAPFREE MB Free of $SWAPMAX MB Total | |
${F1} Processes ${F2}= ${F3}$PROCCOUNT ${F1} | |
" | |
fi | |
} | |
## Update Information only for APT based distributions | |
function show_update_info () { | |
if [ -f /usr/bin/apt-get ]; then | |
if [ "$UPDATE_INFO" = "1" ]; then | |
## get outdated updates | |
UPDATES=$(/usr/bin/apt-get -s dist-upgrade |egrep "upgraded" |egrep "newly installed" |awk {'print $1'}) | |
## display update information | |
echo -e " | |
${F2}============[ ${F1}Update Info${F2} ]==================================================== | |
${F1}Available Updates ${F2}= ${F3}${UPDATES}${F1}" | |
fi | |
fi | |
} | |
## Storage Informations | |
function show_storage_info () { | |
if [ "$STORAGE_INFO" = "1" ]; then | |
## get current storage information, how many space a left :) | |
STORAGE=$(df -hT |sort -r -k 6 -i |sed -e 's/^File.*$/\x1b[0;37m&\x1b[1;32m/' |sed -e 's/^Datei.*$/\x1b[0;37m&\x1b[1;32m/' |egrep -v docker |egrep -v tmpfs) | |
## display storage information | |
echo -e " | |
${F2}============[ ${F1}Storage Info${F2} ]=================================================== | |
${F3}${STORAGE}${F1}" | |
fi | |
} | |
## User Informations | |
function show_user_info () { | |
if [ "$USER_INFO" = "1" ]; then | |
## get my username | |
WHOIAM=$(whoami) | |
## get my own user groups | |
GROUPZ=$(groups) | |
## get my user id | |
ID=$(id) | |
## how many users are logged in | |
SESSIONS=$(who |wc -l) | |
## get a list of all logged in users | |
LOGGEDIN=$(echo $(who |awk {'print $1" " $5'} |awk -F '[()]' '{ print $1 $2 '} |uniq -c |awk {'print "(" $1 ") "$2" " $3","'} ) |sed 's/,$//' |sed '1,$s/\([^,]*,[^,]*,[^,]*,\)/\1\n\\033[1;32m\t /g') | |
## how many system users are there, only check uid <1000 and has a login shell | |
SYSTEMUSERCOUNT=$(cat /etc/passwd |egrep '\:x\:10[0-9][0-9]' |grep '\:\/bin\/bash' |wc -l) | |
## who is a system user, only check uid <1000 and has a login shell | |
SYSTEMUSER=$(cat /etc/passwd |egrep '\:x\:10[0-9][0-9]' |egrep '\:\/bin\/bash|\:\/bin/sh' |awk '{if ($0) print}' |awk -F ':' {'print $1'} |awk -vq=" " 'BEGIN{printf""}{printf(NR>1?",":"")q$0q}END{print""}' |cut -c2- |sed 's/ ,/,/g' |sed '1,$s/\([^,]*,[^,]*,[^,]*,[^,]*,[^,]*,\)/\1\n\\033[1;32m\t /g') | |
## how many ssh super user (root) are there | |
SUPERUSERCOUNT=$(cat /root/.ssh/authorized_keys |egrep '^ssh-' |wc -l) | |
## who is super user (ignore root@) | |
SUPERUSER=$(cat /root/.ssh/authorized_keys |egrep '^ssh-' |awk '{print $NF}' |awk -vq=" " 'BEGIN{printf""}{printf(NR>1?",":"")q$0q}END{print""}' |cut -c2- |sed 's/ ,/,/g' |sed '1,$s/\([^,]*,[^,]*,[^,]*,\)/\1\n\\033[1;32m\t /g' ) | |
## count sshkeys | |
KEYUSERCOUNT=$(for i in $(cat /etc/passwd |egrep '\:x\:10[0-9][0-9]' |awk -F ':' {'print $6'}) ; do cat $i/.ssh/authorized_keys 2> /dev/null |grep ^ssh- |awk '{print substr($0, index($0,$3)) }'; done |wc -l) | |
## print any authorized ssh-key-user of a existing system user | |
KEYUSER=$(for i in $(cat /etc/passwd |egrep '\:x\:10[0-9][0-9]' |awk -F ':' {'print $6'}) ; do cat $i/.ssh/authorized_keys 2> /dev/null |grep ^ssh- |awk '{print substr($0, index($0,$3)) }'; done |awk -vq=" " 'BEGIN {printf ""}{printf(NR>1?",":"")q$0q}END{print""}' |cut -c2- |sed 's/ , /, /g' |sed '1,$s/\([^,]*,[^,]*,[^,]*,\)/\1\n\\033[1;32m\t /g' ) | |
## show user information | |
echo -e " | |
${F2}============[ ${F1}User Data${F2} ]====================================================== | |
${F1} Your Username ${F2}= ${F3}$WHOIAM | |
${F1} Your Privileges ${F2}= ${F3}$ID | |
${F1} Current Sessions ${F2}= ${F3}[$SESSIONS] $LOGGEDIN | |
${F1} SystemUsers ${F2}= ${F3}[$SYSTEMUSERCOUNT] $SYSTEMUSER | |
${F1} SshKeyRootUsers ${F2}= ${F3}[$SUPERUSERCOUNT] $SUPERUSER | |
${F1} SshKeyUsers ${F2}= ${F3}[$KEYUSERCOUNT] $KEYUSER${F1}" | |
fi | |
} | |
## Environment Informations | |
function show_environment_info () { | |
if [ "$ENVIRONMENT_INFO" = "1" ]; then | |
## environment file check | |
if [ ! -f $ENVFILE ]; then | |
createenv ; | |
fi | |
## include sys environment variables | |
source $ENVFILE | |
## test environment.cfg variables, if any of them are empty or damaged | |
if [ -z "${SYSFUNCTION}" ] || [ -z "${SYSENV}" ] || [ -z "${SYSSLA}" ]; then | |
rm $ENVFILE | |
createenv ; # variables are exist but empty, create new | |
fi | |
## display environment information | |
echo -e " | |
${F2}============[ ${F1}Environment Data${F2} ]=============================================== | |
${F1} Function ${F2}= ${F3}$SYSFUNCTION | |
${F1} Environment ${F2}= ${F3}$SYSENV | |
${F1} Service Level ${F2}= ${F3}$SYSSLA${F1}" | |
fi | |
} | |
## Maintenance Information | |
function show_maintenance_info () { | |
if [ "$MAINTENANCE_INFO" = "1" ]; then | |
## get latest maintenance information | |
MAINTENANCE=$(listlog |head -n${LIST_LOG_ENTRY}) | |
## display maintenance information | |
echo -e " | |
${F2}============[ ${F1}Maintenance Information${F2} ]======================================== | |
${F4}$MAINTENANCE${F1}" | |
fi | |
} | |
## Version Information | |
function show_version_info () { | |
if [ "$VERSION_INFO" = "1" ]; then | |
## display version information | |
echo -e " | |
${F2}=============================================================[ ${F1}$VERSION${F2} ]== | |
${F1}" | |
fi | |
} | |
## Display Output | |
function show_info () { | |
show_system_info | |
show_storage_info | |
show_user_info | |
show_update_info | |
show_environment_info | |
show_maintenance_info | |
show_version_info | |
} | |
#### Main Part | |
## if no parameter is passed then start show_info | |
if [ -z "$1" ]; then | |
show_info | |
fi | |
## paremeter | |
param="$2 $3" | |
case "$1" in | |
addlog|-a|--addlog) | |
addlog "$2" | |
;; | |
rmlog|-d|--rmlog) | |
rmlog "$2" | |
;; | |
log|--log|-l|--listlog|listlog) | |
listlog | |
;; | |
config|-c|--config|setup|-s|--setup) | |
createenv | |
;; | |
help|-h|--help|?) | |
echo -e " | |
Usage: $0 [-c|-a|-d|--help] <params> | |
e.g. $0 -a \"start web migration\" | |
Parameter: | |
-a | addlog | --addlog \"...\" add new log entry | |
-d | rmlog | --rmlog [loglinenumber] delete specific log entry | |
-l | log | --log list complete log | |
-c | config | --config configuration setup | |
" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment