dynmodt.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/bin/bash
  2. # Dynamic Motd
  3. # Robert Tulke, rt@debian.sh
  4. ## only root can start
  5. if [ $(whoami) != root ]; then
  6. #cat /etc/motd
  7. exit 0
  8. fi
  9. ## version
  10. version="dynmotd v0.8"
  11. fqdn=$(hostname --fqdn)
  12. ## some colors for a scheme
  13. C_RED="\033[0;31m"
  14. C_BLUE="\033[0;34m"
  15. C_BLACK="\033[0;30m"
  16. C_CYAN="\033[0;36m"
  17. C_PINK="\033[0;35m"
  18. C_GREY="\033[0;37m"
  19. C_LGREEN="\033[1;32m"
  20. ## color schemes
  21. # DOT, day of the tentacle scheme
  22. F1=${C_GREY}
  23. F2=${C_PINK}
  24. F3=${C_LGREEN}
  25. F4=${C_RED}
  26. ## create .maintenance file if not exist
  27. if [ ! -f /root/.maintenance ]; then
  28. touch /root/.maintenance
  29. fi
  30. ## investigate linux distribution
  31. ## create .environment file if not exist
  32. function createenv {
  33. if [ ! -f /root/.environment ]; then
  34. echo "First login... We want to assign a function name for $fqdn,"
  35. echo "like: Backup Server|File Server|Gateway|Proxy|..."
  36. echo
  37. echo -n "System Function: "
  38. read SYSFUNCTION
  39. echo -n "System Environment, like PRD|TST|ITG: "
  40. read SYSENV
  41. echo -n "Service Level Agreement, like SLA1|SLA2|SLA3: "
  42. read SYSSLA
  43. touch /root/.environment
  44. echo "SYSENV=\"$SYSENV\"" >> /root/.environment
  45. echo "SYSFUNCTION=\"$SYSFUNCTION\"" >> /root/.environment
  46. echo "SYSSLA=\"$SYSSLA\"" >> /root/.environment
  47. fi
  48. }
  49. ## environment check
  50. if [ ! -f /root/.environment ]; then
  51. createenv ; # if not exist then create
  52. fi
  53. ## include sys environment variables
  54. source /root/.environment
  55. ## test sys .environment variables, if any of them are empty or currupt
  56. if [ -z "${SYSFUNCTION}" ] || [ -z "${SYSENV}" ] || [ -z "${SYSSLA}" ]; then
  57. rm /root/.environment
  58. createenv ; # variables are exist but empty, create new
  59. fi
  60. ## get a list of all logged in users
  61. LOGGEDIN=$( echo $( for i in $( who |awk -F '[()]' '{ print $2 '} |sort -n ) ; do echo $i; done |uniq -c |awk {'print "(" $1 ") "$2","'} ) |sed 's/,$//' |sed '1,$s/\([^,]*,[^,]*,[^,]*,\)/\1\n\\033[1;32m\t /g' )
  62. ## get my terminal
  63. MYTTY=$(tty |sed 's/\/dev\///')
  64. ## get my hostname
  65. MYHOST=$(who |egrep $MYTTY |awk -F '[()]' {'print $2'})
  66. ## extract my apn from fqdn
  67. APN=$(echo $MYHOST |awk -F '()' '{print $1}')
  68. ## get current procs
  69. PROCCOUNT=$(ps -Afl |egrep -v 'ps|wc' |wc -l)
  70. ## get maxium usable procs
  71. PROCMAX=$(ulimit -u)
  72. ## get my own user groups
  73. GROUPZ=$(groups)
  74. ## how many ssh super user (root) are there
  75. SUPERUSERCOUNT=$(cat /root/.ssh/authorized_keys |egrep '^ssh-' |wc -l)
  76. ## who is super user (ignore root@)
  77. SUPERUSER=$(cat /root/.ssh/authorized_keys |egrep '^ssh-' |egrep -v 'root\@|^$|^.$' |awk '{if ($0) print}' |awk {'print $3" "$4'} |sed 's/@.*$//g'| 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')
  78. #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' |sed 's/\b\(.\)/\u\1/g')
  79. ## how many system users are there, only check uid <1000 and has a login shell
  80. SYSTEMUSERCOUNT=$(cat /etc/passwd |egrep '\:x\:10[0-9][0-9]' |grep '\:\/bin\/bash' |wc -l)
  81. ## who is a system user, only check uid <1000 and has a login shell
  82. 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')
  83. ## print any authorized ssh-key-user of a existing system user
  84. 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 |sed 's/@.*$//g'| awk -F [.] {'print $1 $2'} |sed 's/\b\(.\)/\u\1/g' |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' )
  85. ## not working
  86. 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)
  87. ## get system uptime
  88. UPTIME=$(uptime |cut -c2- |cut -d, -f1)
  89. ## get maximum usable memory
  90. MEMMAX=$(echo $(cat /proc/meminfo |egrep MemTotal |awk {'print $2'})/1024 |bc)
  91. ## get current free memory
  92. MEMFREE=$(echo $(cat /proc/meminfo |egrep MemFree |awk {'print $2'})/1024 |bc)
  93. ## get maximum usable swap space
  94. SWAPMAX=$(echo $(cat /proc/meminfo |egrep SwapTotal |awk {'print $2'})/1024 |bc)
  95. ## get current free swap space
  96. SWAPFREE=$(echo $(cat /proc/meminfo |egrep SwapFree |awk {'print $2'})/1024 |bc)
  97. ## get current kernel version
  98. UNAME=$(uname -r)
  99. ## get my fqdn hostname.domain.name.tld
  100. HOSTNAME=$fqdn
  101. ## get my main ip
  102. IP=$(host $HOSTNAME |awk {'print $4'})
  103. ## get system cpu model
  104. CPUMODEL=$(cat /proc/cpuinfo |egrep 'model name' |uniq |awk -F ': ' {'print $2'})
  105. ## how many cpu i have
  106. CPUS=$(cat /proc/cpuinfo|grep processor|wc -l)
  107. ## how many user logged in at the moment
  108. SESSIONS=$(who |wc -l)
  109. ## get my username
  110. WHOIAM=$(whoami)
  111. ## get my user id
  112. ID=$(id)
  113. ## get runnig distribution name
  114. if [ -f /etc/SuSE-release ]; then
  115. VERSION=$(cat /etc/SuSE-release |egrep SUSE -m 1)
  116. ## get the curernt installed patch level
  117. PATCHLEVEL=$(cat /etc/SuSE-release |egrep PATCHLEVEL |awk -F '= ' {'print $2'})
  118. DISTRIBUTION="$VERSION SP$PATCHLEVEL"
  119. fi
  120. ## get runnig distribution name
  121. if [ -f /etc/debian_version ]; then
  122. PATCHLEVEL=$(cat /etc/debian_version)
  123. DISTRIBUTION="Debian GNU/Linux $PATCHLEVEL"
  124. fi
  125. ## get latest maintenance information
  126. #MAINTENANCE1=$(cat /root/.maintenance)
  127. function getmaintenance {
  128. COUNT=1
  129. while read line; do
  130. NAME=$line;
  131. echo "$COUNT $NAME"
  132. COUNT=$((COUNT+1))
  133. done < /root/.maintenance
  134. }
  135. MAINTENANCE=$(getmaintenance)
  136. ## get current storage information, how many space are left :)
  137. STORAGE=$(df -h |sed -e 's/^File.*$/\x1b[0;37m&\x1b[1;32m/' | sed -e 's/^Datei.*$/\x1b[0;37m&\x1b[1;32m/' |egrep -v docker )
  138. ## Main Menu
  139. echo -e "
  140. ${F2}============[ ${F1}System Data${F2} ]====================================================
  141. ${F1} Hostname ${F2}= ${F3}$HOSTNAME
  142. ${F1} Address ${F2}= ${F3}$IP
  143. ${F1} Kernel ${F2}= ${F3}$UNAME
  144. ${F1} Distribution ${F2}= ${F3}$DISTRIBUTION
  145. ${F1} Uptime ${F2}= ${F3}$UPTIME
  146. ${F1} CPU ${F2}= ${F3}$CPUS x $CPUMODEL
  147. ${F1} Memory ${F2}= ${F3}$MEMFREE MB Free of $MEMMAX MB Total
  148. ${F1} Swap Memory ${F2}= ${F3}$SWAPFREE MB Free of $SWAPMAX MB Total
  149. ${F1} Processes ${F2}= ${F3}$PROCCOUNT of $PROCMAX MAX
  150. ${F2}============[ ${F1}Storage Data${F2} ]===================================================
  151. ${F3}${STORAGE}
  152. ${F2}============[ ${F1}User Data${F2} ]======================================================
  153. ${F1} Username ${F2}= ${F3}$WHOIAM, ($APN)
  154. ${F1} Privileges ${F2}= ${F3}$ID
  155. ${F1} Sessions ${F2}= ${F3}[$SESSIONS] $LOGGEDIN
  156. ${F1} SystemUsers ${F2}= ${F3}[$SYSTEMUSERCOUNT] $SYSTEMUSER
  157. ${F1} SuperUsers ${F2}= ${F3}[$SUPERUSERCOUNT] $SUPERUSER
  158. ${F1} SshKeyUsers ${F2}= ${F3}[$KEYUSERCOUNT] $KEYUSER
  159. ${F2}============[ ${F1}Environment Data${F2} ]===============================================
  160. ${F1} Function ${F2}= ${F3}$SYSFUNCTION
  161. ${F1} Environment ${F2}= ${F3}$SYSENV
  162. ${F1}Service Level ${F2}= ${F3}$SYSSLA
  163. ${F2}============[ ${F1}Maintenance Information${F2} ]========================================
  164. ${F4}$(getmaintenance)
  165. ${F2}=============================================================[ ${F1}$version${F2} ]==
  166. ${F1}
  167. "