The following function is used to determine a few basics about the network of the user, such as, external and internal IP addresses, subnet, the default gateway, and who's logged in. Note that the 3-liner, ext_ip_info.py
is also attached to this gist.
function host_information(){
export GATEWAY=$(netstat -nr | perl -lne 'print $1 if m/default\s+(.+?)\s+?.+?en0/g')
export MY_EXT_IP=$(curl -s https://icanhazip.com)
export EXT_IP_OWNER=$(python3 bin/ext_ip_info.py $MY_EXT_IP)
export MY_INT_IP=$(nmap --iflist |perl -lne 'print "$1" if m/en0\s+\S+\s+(\S+)\/(\S+)\s+ethernet/g'|head -n1)
export SUBNET=$(nmap --iflist |perl -lne 'print "$2" if m/en0\s+\S+\s+(\S+)\/(\S+)\s+ethernet/g'|head -n1)
echo "=========================================="
echo "External IP:" $MY_EXT_IP "("$EXT_IP_OWNER")"
echo "Internal IP:" $MY_INT_IP
echo " Subnet:" $SUBNET
echo " Gateway:" $GATEWAY
echo "=========================================="
echo "Users Currently Logged In"
#echo "USERNAME TTY START/END DURATION"
last |perl -lne 'print "$1,$2,$3,$4" if m/(.+?)\s+(.+?)\s+(.+?)\s+(still logged in)/g'
echo "=========================================="
echo "=========================================="
}