User Tools

Site Tools


fbsd-banner

FreeBSD - Login Banner With System Information

This is the result of the nice login banner we will build here

Host: host.example.org (96.7.128.192)
    CPU: 0.2% user, 0.0% nice, 0.2% system, 0.1% interrupt, 99.4% idle
    Mem: 10M Active, 550M Inact, 225M Wired, 103M Buf, 184M Free
   Swap: 0B used, 512M available, 0% capacity
 
   Load: 0.33, 0.32, 0.30
 Uptime: 1 hour, 13 minutes
 We are: 18 Jan, 03:20:54

Create the file as hidden file

cat > $HOME/.banner

. .banner.subr

# Stylize site name if FIGlet is installed:
if command -v figlet >/dev/null; then
	figlet "${motd_sitename}"
fi

# Display system information:
cat <<EOF
   Host: ${motd_host}
    CPU: ${motd_stats_cpu}
    Mem: ${motd_stats_mem}
   Swap: ${motd_swap}
 
   Load: ${motd_load}
 Uptime: ${motd_uptime}
 We are: ${motd_date}

EOF

Make it executable

chmox +x $HOME/.banner

Create a second file which contains the subroutine used by the banner script

cat > $HOME/.banner.subr
if [ -r /etc/defaults/rc.conf ]; then
	. /etc/defaults/rc.conf
	source_rc_confs
elif [ -r /etc/rc.conf ]; then
	. /etc/rc.conf
fi

: ${motd_sitename:=$(hostname -d)}

# Displays OS version (copied from /etc/rc.d/motd).
get_version()
{
	uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,'
}

# Displays baseboard information.
get_baseboard()
{
	printf '%s %s %s %s' \
		"$(kenv -q smbios.planar.maker)" \
		"$(kenv -q smbios.planar.product)" \
		"$(kenv -q smbios.planar.version)" \
		"$(kenv -q smbios.planar.serial)"
}

# Displays processor information.
get_processor()
{
	sysctl -n hw.model
}

# Displays IPv4/IPv6 addresses assigned to all interfaces, filtering
# localhost and link-local addresses.
get_ipaddrs()
{
	ifconfig -a | awk '
	/inet6?/ && $2 != "127.0.0.1" && $2 != "::1" &&
		    $2 !~ "^169\.254\." && $2 !~ "^fe80::" {
		print $2
	}' | xargs
}

# Displays host information.
get_host()
{
	printf '%s (%s)' "$(hostname)" "$(get_ipaddrs)"
}

# Displays system load.
get_load()
{
	sysctl -n vm.loadavg | awk '{ printf "%s, %s, %s\n", $2, $3, $4 }'
}

# Displays uptime in a friendlier format than w(1).
get_uptime()
{
	local boottime uptime day hour minute second first name
	boottime=$(sysctl -n kern.boottime | sed -e 's/.*sec = \([0-9]*\),.*$/\1/')
	uptime=$(( $(date +%s) - boottime ))
	day=$(( uptime / 86400 ))
	hour=$(( uptime / 3600 % 24 ))
	minute=$(( uptime / 60 % 60 ))
	second=$(( uptime % 60 ))

	# Reporting seconds is less interesting after the first hour has
	# passed. Once elapsed, we round up to the nearest minute:
	if [ $day -gt 0 -o $hour -gt 0 ]; then
		if [ $second -ge 30 ]; then
			if [ $minute -eq 59 ]; then
				if [ $hour -eq 23 ]; then
					day=$(( day + 1 ))
					hour=0
				else
					hour=$(( hour + 1 ))
				fi
				minute=0
			else
				minute=$(( minute + 1 ))
			fi
		fi
		second=0
	fi

	first=1
	for name in day hour minute second; do
		eval val=\$$name
		if [ $val -gt 0 ]; then
			[ $first -ne 0 ] && first=0 || printf ', '
			printf '%d %s' "${val}" "${name}"
			[ $val -gt 1 ] && printf 's'
		fi
	done
	printf '\n'
}

get_swap()
{
  swapinfo -h | awk 'NR==2 {print $3 " used, " $4 " available, " $5 " capacity"}'
}

get_date()
{
  date +'%d %b, %H:%M:%S'
}

# Reads statistics from top(1), removing extra whitespace.
read_stats()
{
	{
		read -r motd_stats_cpu
		read -r motd_stats_mem
	} <<-EOF
	$(top -b | awk '/^CPU:/, /^Swap:/ {
		sub(/^([[:alpha:]]+:)?[[:blank:]]+/, "")
		gsub(/[[:blank:]]{2,}/, " ")
		print
	}')
	EOF
}

motd_version=$(get_version)
motd_baseboard=$(get_baseboard)
motd_processor=$(get_processor)
motd_host=$(get_host)
motd_load=$(get_load)
motd_uptime=$(get_uptime)
motd_swap=$(get_swap)
motd_date=$(get_date)
read_stats

Now you should have 2 file in your home directory

ls ~/.banner*
-rwxr-xr-x  /home/me/.banner
-rw-r--r--  /home/user/.banner.subr

Start the banner at login, append the following line to ~/.profile

# banner (should be executable)
if [ -x $HOME/.banner ] ; then  $HOME/.banner ; fi

This is the whole login using Customize Message of the day unauthorized banner and this script with system information

Last login: Sat Jan 18 04:11:34 2025 from xxx.xxx.xxx.xxx
FreeBSD 14.2-RELEASE (GENERIC) releng/14.2-n269506-c8918d6c7412

    ---------------------------------------------
   | All connections are monitored and recorded. |
   | Disconnect IMMEDIATELY if not authorized!!! |
    ---------------------------------------------
                \   ^__^
                 \  (oo)\_______
                    (__)\       )\/\
                     §§ ||----w |
                        ||     ||

   Host: host.example.org (96.7.128.192)
    CPU: 0.1% user, 0.0% nice, 0.2% system, 0.1% interrupt, 99.5% idle
    Mem: 5584K Active, 651M Inact, 164M Wired, 40M Buf, 154M Free
   Swap: 0B used, 512M available, 0% capacity
 
   Load: 0.26, 0.30, 0.31
 Uptime: 2 hours, 10 minutes
 We are: 18 Jan, 04:18:01

fbsd-banner.txt · Last modified: January 2025 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki