Skip to content

Instantly share code, notes, and snippets.

@kyletimmermans
Last active February 22, 2024 13:21
Show Gist options
  • Save kyletimmermans/034afdc8bacbc80f605f6dc414470e86 to your computer and use it in GitHub Desktop.
Save kyletimmermans/034afdc8bacbc80f605f6dc414470e86 to your computer and use it in GitHub Desktop.
Network and system info for *nix OS terminal (.bashrc / .zshrc)
# List network information
netinfo() {
if [[ $(uname -s) != *"Darwin"* ]]; then
sudo echo -n ""
fi
echo -e "\n---------------- Network Information ----------------"
echo "Hostname: $(hostname)"
if [[ $(uname -s) == *"Darwin"* ]]; then
echo "Local IP: $(ipconfig getifaddr $(route -n get default | grep interface | awk '{print $2}'))"
echo "Network Interface: $(route -n get default | grep interface | awk '{print $2}')"
echo "SSID: $(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2; found=1} END {if (!found) print "N/A"}')"
echo "Firewall: $(/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate | awk '{sub(/\.$/, "", $3); print $3}')"
else
# Install 'net-tools' & 'wireless-tools'
echo "Local IP: $(hostname -I)"
echo "Network Interface: $(route | awk '/^default/{print $NF}')"
echo "SSID: $(iwgetid -r || echo "N/A")"
echo "Firewall: $(sudo ufw status | awk '{print $2}')"
fi
echo "DNS Server: $(grep "nameserver" /etc/resolv.conf | awk '{print $2}')"
echo "Public IP: $(output=$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com 2>&1); if echo "$output" | grep -q "couldn't get address"; then echo "No internet"; else echo "$output" | awk -F'"' '{ print $2 }'; fi)"
echo -e "-----------------------------------------------------\n"
}
# List system information
sysinfo() {
echo -e "\n---------------- System Information ----------------"
echo "Hostname: $(hostname)"
if [[ $(uname -s) == *"Darwin"* ]]; then
echo "OS: $(system_profiler SPSoftwareDataType | awk '/System Version:/ {gsub(/^.*System Version:[[:space:]]*/, ""); print}')"
echo "CPU: $(system_profiler SPHardwareDataType | awk '/Processor Name:/ {name=$0} /Processor Speed:/ {speed=$0} END {gsub(/^.*Processor Name: /, "", name); gsub(/^.*Processor Speed: /, "", speed); print name " @ " speed}')"
echo "Kernel: $(system_profiler SPSoftwareDataType | awk '/Kernel Version:/ {gsub(/^.*Kernel Version:[[:space:]]*/, ""); print}')"
echo "Arch: $(uname -m | awk '{ printf "%s" , $1 }')"
echo "DE: $(awk '/SOFTWARE LICENSE AGREEMENT FOR macOS/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'macOS ' '{print $NF}' | awk '{print substr($0, 0, length($0)-1)}')"
echo "Uptime: $(uptime | sed 's/.*up \([^,]*\),.*/\1/')"
else
echo "OS: $(awk '{ printf "%s %s %s %s", $1, $2, $3, $4 }' /etc/issue | sed 's/\\n//g; s/\\l//g')"
cpu_name=$(cat /proc/cpuinfo | grep "model name" | head -1 | awk '{ for (i = 4; i <= NF; i++) printf "%s ", $i }')
[[ $cpu_name == *'@'* ]] && echo "CPU: $cpu_name" || echo "CPU: $cpu_name@ $(cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq | awk '{printf "%.2f GHz\n", $1/1000000}')"
echo "Kernel: $(uname -a | awk '{ printf "%s" , $3 }')"
echo "Arch: $(uname -m | awk '{ printf "%s" , $1 }')"
echo "DE: $([ -z "${XDG_CURRENT_DESKTOP}" ] && echo "N/A" || echo $XDG_CURRENT_DESKTOP)"
echo "Uptime: $(uptime -p | sed 's/^.*up //g')"
fi
echo -e "----------------------------------------------------\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment