Files
shell/helpers/net.sh

52 lines
1.4 KiB
Bash

#!/usr/bin/env bash
########################################################
# Networking functions
########################################################
get_current_ip() {
local CURRENT_IP
[ -f /etc/os-release ] && source /etc/os-release
case "$ID" in
debian|ubuntu) CURRENT_IP=$(hostname -I | awk '{print $1}') ;;
alpine) CURRENT_IP=$(ip -4 addr show eth0 | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1) ;;
*) CURRENT_IP="Unknown" ;;
esac
echo "$CURRENT_IP"
}
get_external_ip() {
local ip="$(curl -s https://api.myip.com | jq .ip)"
echo "$ip" | tr -d '"'
}
is_valid_ipv4() {
local ip="$1"
local regex="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
if [[ $ip =~ $regex ]]; then
IFS='.' read -r -a parts <<< "$ip"
for part in "${parts[@]}"; do
if ! [[ $part =~ ^[0-9]+$ ]] || ((part < 0 || part > 255)); then
return 1
fi
done
return 0
fi
return 1
}
curltime() {
curl -w @- -o /dev/null -s "$@" <<'EOF'
time_namelookup: %{time_namelookup} sec\n
time_connect: %{time_connect} sec\n
time_appconnect: %{time_appconnect} sec\n
time_pretransfer: %{time_pretransfer} sec\n
time_redirect: %{time_redirect} sec\n
time_starttransfer: %{time_starttransfer} sec\n
---------------\n
time_total: %{time_total} sec\n
EOF
}