编程知识 cdmana.com

Did you make a fuss today? Customize your own Linux SSH welcome information

Please start your performance

linux-oz6w:~ # cat << 'eof' > /etc/profile.d/ssh-login-info.sh
#!/bin/sh
#  Output an image 
echo -e "\033[1;35m
██████╗   ██████╗
██╔══██╗  ██╔══██╗
██████╔╝  ██║  ██║
██╔══██╗  ██║  ██║
██████╔╝  ██████╔╝
╚═════╝   ╚═════╝\033[0m"

#  Get the running time of the system (uptime The unit of time the command sees is minutes ,/proc/uptime The unit of time you see in it is seconds , Conversion is needed )
upSeconds="$(cut -d. -f1 /proc/uptime)"
secs=$((${upSeconds}%60))
mins=$((${upSeconds}/60%60))
hours=$((${upSeconds}/3600%24))
days=$((${upSeconds}/86400))
UPTIME_INFO=$(printf "%d days, %02dh %02dm %02ds" "$days" "$hours" "$mins" "$secs")
#  Judge linux Distribution version 
if [ -f /etc/redhat-release ] ; then
    PRETTY_NAME=$(< /etc/redhat-release)

elif [ -f /etc/debian_version ]; then
   DIST_VER=$(</etc/debian_version)
   PRETTY_NAME="$(grep PRETTY_NAME /etc/os-release | sed -e 's/PRETTY_NAME=//g' -e 's/"//g') ($DIST_VER)"

else
    PRETTY_NAME=$(cat /etc/*-release | grep "PRETTY_NAME" | sed -e 's/PRETTY_NAME=//g' -e 's/"//g')
fi
#  Determine whether the current operating system is a virtual machine or container 
if [[ -d "/system/app/" && -d "/system/priv-app" ]]; then
    model="$(getprop ro.product.brand) $(getprop ro.product.model)"

elif [[ -f /sys/devices/virtual/dmi/id/product_name ||
        -f /sys/devices/virtual/dmi/id/product_version ]]; then
    model="$(< /sys/devices/virtual/dmi/id/product_name)"
    model+=" $(< /sys/devices/virtual/dmi/id/product_version)"

elif [[ -f /sys/firmware/devicetree/base/model ]]; then
    model="$(< /sys/firmware/devicetree/base/model)"

elif [[ -f /tmp/sysinfo/model ]]; then
    model="$(< /tmp/sysinfo/model)"
fi

MODEL_INFO=${model}                   #  Get the hardware environment of the operating system 
KERNEL=$(uname -srmo)                 #  Get the kernel version 
USER_NUM=$(who -u | wc -l)            #  Get the number of users connected to the terminal 
RUNNING=$(ps ax | wc -l | tr -d " ")  #  Get the number of running processes 

#  Get disk information : The total number of disks and utilization rate 
totaldisk=$(df -h -x devtmpfs -x tmpfs -x debugfs -x aufs -x overlay --total 2>/dev/null | tail -1)
disktotal=$(awk '{print $2}' <<< "${totaldisk}")
diskused=$(awk '{print $3}' <<< "${totaldisk}")
diskusedper=$(awk '{print $5}' <<< "${totaldisk}")
DISK_INFO="\033[0;33m${diskused}\033[0m of \033[1;34m${disktotal}\033[0m disk space used (\033[0;33m${diskusedper}\033[0m)"

#  obtain CPU Information :cpu model 、 The core number 、 Logical number 、
cpu=$(awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//')
cpun=$(grep -c '^processor' /proc/cpuinfo)
cpuc=$(grep '^cpu cores' /proc/cpuinfo | tail -1 | awk '{print $4}')
cpup=$(grep '^physical id' /proc/cpuinfo | wc -l)
CPU_INFO="${cpu} ${cpup}P ${cpuc}C ${cpun}L"

#  Get system load :1 minute 、5 minute 、15 minute 
read one five fifteen rest < /proc/loadavg
LOADAVG_INFO="\033[0;33m${one}\033[0m / ${five} / ${fifteen} with \033[1;34m$(( cpun*cpuc ))\033[0m core(s) at \033[1;34m$(grep '^cpu MHz' /proc/cpuinfo | tail -1 | awk '{print $4}')\033 MHz"

#  Get memory information : The total amount of memory and usage rate 
MEM_INFO="$(cat /proc/meminfo | awk '/MemTotal:/{
     
     total=$2/1024/1024;next} /MemAvailable:/{
     
     use=total-$2/1024/1024; printf("\033[0;33m%.2fGiB\033[0m of \033[1;34m%.2fGiB\033[0m RAM used (\033[0;33m%.2f%\033[0m)",use,total,(use/total)*100);}')"

#  Get server IP Address 
# extranet_ip=" and $(curl -s ip.cip.cc)"
IP_INFO="$(ip a | grep glo | awk '{print $2}' | head -1 | cut -f1 -d/)${extranet_ip:-}"

#  After remote connection , Output the information to the terminal 
echo -e "
 \033[0;1;31mInformation as of\033[0m..: \033[1;34m$(date +"%Y-%m-%d %T")\033[0m

 \033[0;1;31mProduct\033[0m............: ${MODEL_INFO}
 \033[0;1;31mOS\033[0m.................: ${PRETTY_NAME}
 \033[0;1;31mKernel\033[0m.............: ${KERNEL}
 \033[0;1;31mCPU\033[0m................: ${CPU_INFO}

 \033[0;1;31mHostname\033[0m...........: \033[1;34m$(hostnamectl | grep 'Static hostname' | awk -F ': ' '{
   
   print $2}')\033[0m
 \033[0;1;31mIP Addresses\033[0m.......: \033[1;34m${IP_INFO}\033[0m

 \033[0;1;31mUptime\033[0m.............: \033[0;33m${UPTIME_INFO}\033[0m
 \033[0;1;31mMemory\033[0m.............: ${MEM_INFO}
 \033[0;1;31mLoad Averages\033[0m......: ${LOADAVG_INFO}
 \033[0;1;31mDisk Usage\033[0m.........: ${DISK_INFO}

 \033[0;1;31mUsers online\033[0m.......: \033[1;34m${USER_NUM}\033[0m
 \033[0;1;31mRunning Processes\033[0m..: \033[1;34m${RUNNING}\033[0m
"
eof
linux-oz6w:~ # chmod +x /etc/profile.d/ssh-login-info.sh  #  Give an executive authority 

The renderings are as follows :
 Insert picture description here

About echo -e The font of

# \e and \033 It's the same , No difference 
echo -e "\e[30m test content black  \e[0m"
echo -e "\e[31m test content red  \e[0m"
echo -e "\e[32m test content green  \e[0m"
echo -e "\e[33m test content yellow  \e[0m"
echo -e "\e[34m test content blue  \e[0m"
echo -e "\e[35m test content purple  \e[0m"
echo -e "\e[36m test content Sky blue  \e[0m"
echo -e "\e[37m test content white  \e[0m"
echo -e "\033[1;31m Red \033[0m"
echo -e "\033[1;33m Yellow \033[0m"
echo -e "\033[1;34m Blue \033[0m"
echo -e "\033[1;32m Green \033[0m"
echo -e "\033[1;35m Pink \033[0m"

The effect is as follows :
 Insert picture description here

 Font control options :
\033[0m      #  Turn off all properties 
\033[1m      #  Set high brightness 
\033[4m      #  Underline 
\033[5m      #  flashing 
\033[7m      #  Reflexion , Color contrast shows , It's white and black , Or white text on a black background 
\033[8m      #  Shadow elimination , The character color will be the same as the background color 
\033[nA      #  The cursor moves up n That's ok 
\033[nB      #  The cursor moves down n That's ok 
\033[nC      #  The cursor moves right n That's ok 
\033[nD      #  The cursor moves left n That's ok 
\033[y;xH    #  Set cursor position 
\033[2J      #  Clear the screen 
\033[K       #  Clear the contents from the cursor to the end of the line 
\033[s       #  Save cursor position 
\033[u       #  Restore cursor position 
\033[?25l    #  hide cursor 
\033[?25h    #  Show cursor 

版权声明
本文为[osc_ uxjivoss]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224104932372F.html

Scroll to Top