请开始你的表演
linux-oz6w:~ # cat << 'eof' > /etc/profile.d/ssh-login-info.sh #!/bin/sh # 输出一个图像 echo -e "\033[1;35m ██████╗ ██████╗ ██╔══██╗ ██╔══██╗ ██████╔╝ ██║ ██║ ██╔══██╗ ██║ ██║ ██████╔╝ ██████╔╝ ╚═════╝ ╚═════╝\033[0m" # 获取系统运行的时间(uptime命令看到的时间单位是分钟,/proc/uptime里面看到的时间单位是秒,需要做换算) 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") # 判断linux发行版 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 # 判断当前操作系统是否是虚拟机或容器 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} # 获取操作系统所在的硬件环境 KERNEL=$(uname -srmo) # 获取内核版本 USER_NUM=$(who -u | wc -l) # 获取连接终端的用户数量 RUNNING=$(ps ax | wc -l | tr -d " ") # 获取运行的进程数量 # 获取磁盘信息:磁盘总量以及使用率 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)" # 获取CPU信息:cpu型号、核心数、逻辑数、 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 | sort | uniq | wc -l) CPU_INFO="${cpu} ${cpup}P ${cpuc}C ${cpun}L" # 获取系统负载情况:1分钟、5分钟、15分钟 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" # 获取内存信息:内存总量以及使用率 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);}')" # 获取服务器IP地址 # extranet_ip=" and $(curl -s ip.cip.cc)" IP_INFO="$(ip a | grep glo | awk '{print $2}' | head -1 | cut -f1 -d/)${extranet_ip:-}" # 远程连接后,将一下信息输出到终端 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 # 给一个执行权限
效果图如下:
关于echo -e的字体
# \e和\033是一样的,没区别 echo -e "\e[30m test content黑 \e[0m" echo -e "\e[31m test content红 \e[0m" echo -e "\e[32m test content绿 \e[0m" echo -e "\e[33m test content黄 \e[0m" echo -e "\e[34m test content蓝 \e[0m" echo -e "\e[35m test content紫 \e[0m" echo -e "\e[36m test content天蓝 \e[0m" echo -e "\e[37m test content白 \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"
效果如下:
字体控制选项: \033[0m # 关闭所有属性 \033[1m # 设置高亮度 \033[4m # 下划线 \033[5m # 闪烁 \033[7m # 反显,撞色显示,显示为白色黑底,或者显示为黑底白字 \033[8m # 消影,字符颜色将会与背景颜色相同 \033[nA # 光标上移n行 \033[nB # 光标下移n行 \033[nC # 光标右移n行 \033[nD # 光标左移n行 \033[y;xH # 设置光标位置 \033[2J # 清屏 \033[K # 清除从光标到行尾的内容 \033[s # 保存光标位置 \033[u # 恢复光标位置 \033[?25l # 隐藏光标 \033[?25h # 显示光标