#!/bin/bash # # 10-sysinfo - generate the system information # Copyright (c) 2013 Nick Charlton # # Authors: Nick Charlton &amp;amp;lt;hello@nickcharlton.net&amp;amp;gt; # Modified by Oitibs: https://oitibs.com/?s=Debian+Dynamic+MOTD # Modified by Leitbogioro: https://github.com/leitbogioro/ # https://www.zhihu.com/column/originaltechnic # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. date=`date` load=`cat /proc/loadavg | awk '{print $1}'` root_usage=`df -h / | awk '/\// {print $(NF-1)}'` memory_usage=`free -m | awk '/Mem:/ { total=$2; used=$3 } END { printf("%3.1f%%", used/total*100)}'` swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", $3/$2*100) }'` users=`users | wc -w` time=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }'` processes=`ps aux | wc -l` localip=`hostname -I | awk '{print $1}'` IPv4=`timeout 1 dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's/\"//g'` IPv6=`timeout 1 dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's/\"//g'` # IP_Check=$(echo $IPv4 | awk -F. '$1<255&&$2<255&&$3<255&&$4<255{print "isIPv4"}') IP_Check="$IPv4" if expr "$IP_Check" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then for i in 1 2 3 4; do if [ $(echo "$IP_Check" | cut -d. -f$i) -gt 255 ]; then echo "fail ($IP_Check)" exit 1 fi done IP_Check="isIPv4" fi # If the last two strings of IPv6 is "::", we should replace ":" to "0" in the last to make sure it's a valid IPv6(can't end with ":") [[ ${IPv6: -1} == ":" ]] && IPv6=$(echo "$IPv6" | sed 's/.$/0/') # Add ":" in the last of the IPv6 address for cut all items infront of the ":" by split by symbol ":". IP6_Check="$IPv6"":" # Total numbers of the hex blocks in IPv6 address includes with empty value(abbreviation of "0000") IP6_Hex_Num=`echo "$IP6_Check" | tr -cd ":" | wc -c` # Default number of empty values of the hex block is "0" IP6_Hex_Abbr="0" # The first filter plays a role of: # 1. check if 0-9 or a-z and ":" in original IPv6; # 2. the longest number of ":" in IPv6 is "7", because of variable "IP6_Check" one more ":" has been add, # so the total number of ":" in variable "IP6_Check" should be less or equal "8" if [[ `echo "$IPv6" | grep -i '[[:xdigit:]]' | grep ':'` ]] && [[ "$IP6_Hex_Num" -le "8" ]]; then # Total cycles of the check(sequence of the current hex block) for ((i=1; i<="$IP6_Hex_Num"; i++)){ # Every IPv6 hex block of current cycle IP6_Hex=$(echo "$IP6_Check" | cut -d: -f$i) # Abbreviation of hex block should be appeared only one time in principle, # but if the first of hex block is empty, it shouldn't be as an abbreviation. [[ "$IP6_Hex" == "" ]] && { [[ "$i" -gt "1" ]] && IP6_Hex_Abbr=`expr $IP6_Hex_Abbr + 1` } # String number of letters or numbers in one block should less or equal "4" [[ `echo "$IP6_Hex" | wc -c` -le "4" ]] && { # The second filter plays a reversion role of the following to exclude an effective IPv6 hex block: # Except 0-9 and a-f; # Abbreviation of hex block should be appeared less than or equal one time. if [[ `echo "$IP6_Hex" | grep -iE '[^0-9a-f]'` ]] || [[ "$IP6_Hex_Abbr" -gt "2" ]]; then echo "fail ($IP6_Check)" exit 1 fi } } IP6_Check="isIPv6" fi if [[ "${IP6_Check}" != "isIPv6" ]] && [[ -z ${IPv6} ]]; then IPv6="N/A" fi if [[ "${IP_Check}" != "isIPv4" ]] && [[ -z ${IPv4} ]]; then IPv4="N/A" fi if [[ "${localip}" == "${IPv4}" ]] || [[ "${localip}" == "${IPv6}" ]]; then # localip=`ip -o a show | grep -w "lo" | grep -w "inet" | cut -d ' ' -f7 | awk '{split($1, a, "/"); print $2 "" a[1]}'` localip=`cat /etc/hosts | grep "localhost" | sed -n 1p | awk '{print $1}'` fi echo " System information as of $date" echo printf "%-30s%-15s\n" " System Load:" "$load" printf "%-30s%-15s\n" " Private IP address:" "$localip" printf "%-30s%-15s\n" " Public IPv4 address:" "$IPv4" printf "%-30s%-15s\n" " Public IPv6 address:" "$IPv6" printf "%-30s%-15s\n" " Memory Usage:" "$memory_usage" printf "%-30s%-15s\n" " Usage On /:" "$root_usage" printf "%-30s%-15s\n" " Swap Usage:" "$swap_usage" printf "%-30s%-15s\n" " Local Users:" "$users" printf "%-30s%-15s\n" " Processes:" "$processes" printf "%-30s%-15s\n" " System Uptime:" "$time" echo