Skip to content

Instantly share code, notes, and snippets.

@jtyr
Last active January 28, 2020 23:38
Show Gist options
  • Save jtyr/a7e455da5c6aa0ea2f6d50a9a483d9d4 to your computer and use it in GitHub Desktop.
Save jtyr/a7e455da5c6aa0ea2f6d50a9a483d9d4 to your computer and use it in GitHub Desktop.
Shell script for gathering facts from Unix systems.
#!/bin/sh
# Get the total memory in kB
if [ -f /bin/esxcli ]; then
MEM=$(esxcli hardware memory get | grep Physical | awk '{printf("%d", $3/1000)}')
elif [ -f /proc/meminfo ]; then
MEM=$( (grep MemTotal /proc/meminfo 2>/dev/null || echo 'x unknown') | awk '{print $2}')
else
MEM='unknown'
fi
# Get the number of CPUs
if [ -f /proc/cpuinfo ]; then
CPU=$(cat /proc/cpuinfo | egrep '^processor' | wc -l)
else
CPU='unknown'
fi
# Determine distro and version
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO=$ID
VERSION=$VERSION_ID
elif [ -f /etc/redhat-release ]; then
TMP_DIST=$(cat /etc/redhat-release | sed -r 's/^(.{6}).*/\1/' | tr 'A-Z' 'a-z')
VERSION=$(cat /etc/redhat-release | sed -r 's/.*\ ([0-9]).*\ \(.+\)\s*$/\1/')
if [ "$TMP_DIST" == 'centos' ]; then
DISTRO=$TMP_DIST
elif [ "$TMP_DIST" == 'red ha' ]; then
DISTRO='redhat'
else
DISTRO='unknown'
fi
elif [ -f /etc/debian_version ]; then
DISTRO='debian'
VERSION='unknown'
else
DISTRO=$(uname -s)
VERSION=$(uname -r)
fi
# Get the default IP
if [ -f /sbin/ip ]; then
DEFAULT_IFACE=$(/sbin/ip route | grep '^default' | sed -r 's/.*\ dev\ ([a-z0-9]+)\ .*/\1/' | head -n1)
IP=$(/sbin/ip addr show dev "$DEFAULT_IFACE" | grep 'inet ' | head -n1 | awk '{print $2}' | sed 's,/.*,,')
fi
# Get the hostname
HOST=$(hostname -f | tr 'A-Z' 'a-z' 2>/dev/null || hostname -f)
# Print the results
echo "$HOST,$IP,$DISTRO,$VERSION,$CPU,$MEM"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment