Skip to content

Instantly share code, notes, and snippets.

@markuskreitzer
Last active December 5, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markuskreitzer/551468dbd47c9e45ff1d94f84c180060 to your computer and use it in GitHub Desktop.
Save markuskreitzer/551468dbd47c9e45ff1d94f84c180060 to your computer and use it in GitHub Desktop.
IP Info Function for BashRC

IP Info Function for bash shell loading.

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 "=========================================="

}
from ipwhois import IPWhois
import sys
print(IPWhois(sys.argv[1]).lookup_whois()['asn_description'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment