Skip to content

Instantly share code, notes, and snippets.

@mattfoster
Last active September 24, 2019 08:34
Show Gist options
  • Save mattfoster/2ee71e3ab3ead61bf6b108fff1449231 to your computer and use it in GitHub Desktop.
Save mattfoster/2ee71e3ab3ead61bf6b108fff1449231 to your computer and use it in GitHub Desktop.
Some functions for network fun
# sort -V can also work
function ipsort {
sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n
}
function grep4 {
grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
}
function grep6 {
grep -oE "\b((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*\b"
}
function strip_proto {
echo $1 | sed -E -e "s~(https?|ftp|ssh|rsync){0,1}://~~" -e "s~/.*~~"
}
function host {
HOST=$(strip_proto $1)
shift
command host "${HOST}" "$@"
}
function dig {
host=$(strip_proto $1)
shift
command dig "${host}" "$@"
}
function nslookup {
host=$(strip_proto $1)
shift
command nslookup "${host}" "$@"
}
function ipwhois {
host=$(strip_proto $1)
if [[ $host =~ [0-9]{1,3}\.[0-9]{1,3}\. ]]; then
local ip=$host
else
# Assuming this is a dns name
local ip=$(dig +short $host)
fi
if [[ -z "${ip}" ]]; then
return 1;
fi
echo "IP whois for ${ip}"
command whois "${ip}" "$@"
}
# Taken from: https://github.com/twe4ked/dotfiles/blob/master/shell/functions/ping.sh
# Modified to pull out strip_proto func and improve proto list
function ping {
HOST=$(strip_proto $1)
shift
command ping "$HOST" "$@"
}
# Pipe curl through pager
function cl {
curl -ik "$@" | less
}
function whois {
HOST=$(strip_proto $1)
shift
command whois "$HOST" "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment