Add get_ip_public and unify all functions syntax

This commit is contained in:
Slava 2024-10-02 19:56:33 +03:00
parent 4cfbfa5ee7
commit 12e2c7846e
No known key found for this signature in database
GPG Key ID: 351E7AA9BD0DFEB8

View File

@ -1,74 +1,66 @@
#!/bin/bash #!/bin/bash
# Variables
NETWORK="${NETWORK:-testnet}"
# Function to detect OS # Function to detect OS
get_os() { get_os() {
case "$(uname -s)" in case "$(uname -s)" in
Linux*) echo "linux";; Linux*) echo "linux" ;;
Darwin*) echo "darwin";; Darwin*) echo "darwin" ;;
CYGWIN*|MINGW*|MSYS*|MINGW*) echo "windows";; CYGWIN*|MINGW*|MSYS*|MINGW*) echo "windows" ;;
*) echo "unknown";; *) echo "unknown" ;;
esac esac
} }
# Function to detect CPU architecture # Function to detect CPU architecture
get_arch() { get_arch() {
case "$(uname -m)" in case "$(uname -m)" in
x86_64|amd64) echo "amd64";; x86_64|amd64) echo "amd64" ;;
arm64|aarch64) echo "arm64";; arm64|aarch64) echo "arm64" ;;
*) echo "Unsupported architecture: $(uname -m)"; exit 1;; *) echo "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac esac
} }
# Function to change file permissions using native OS commands # Function to change file permissions using native OS commands
change_permissions() { change_permissions() {
local file_path="$1" local file_path="$1"
local permissions="$2" local permissions="$2"
case "$OS" in case "$OS" in
linux|darwin) linux|darwin) chmod $permissions "$file_path" ;;
# Use chmod for Linux and macOS windows) echo "Skipping permission change on Windows for $file_path" ;;
chmod $permissions "$file_path" *) echo "Unsupported OS for changing permissions"; return 1 ;;
;; esac
windows)
# For Windows, we'll skip changing permissions
echo "Skipping permission change on Windows for $file_path"
;;
*)
echo "Unsupported OS for changing permissions"
return 1
;;
esac
} }
# Function to get IP on Linux and macOS # Function to get IP on Linux and macOS
get_ip_unix() { get_ip_unix() {
if command -v ip >/dev/null 2>&1; then if command -v ip >/dev/null 2>&1; then
ip addr show | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | cut -d "/" -f 1 | head -n 1 ip addr show | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | cut -d "/" -f 1 | head -n 1
elif command -v ifconfig >/dev/null 2>&1; then elif command -v ifconfig >/dev/null 2>&1; then
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | head -n 1 ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | head -n 1
else else
echo "Unable to find IP address. Neither 'ip' nor 'ifconfig' command found." echo "Unable to find IP address. Neither 'ip' nor 'ifconfig' command found."
exit 1 exit 1
fi fi
} }
# Function to get IP on Windows # Function to get IP on Windows
get_ip_windows() { get_ip_windows() {
ipconfig | grep -i "IPv4 Address" | awk '{print $NF}' | head -n 1 ipconfig | grep -i "IPv4 Address" | awk '{print $NF}' | head -n 1
}
# Function to get Public IP using ip lookup service
get_ip_public() {
curl -m 5 -s https://ip.codex.storage
} }
# Detect the operating system and call the appropriate function # Detect the operating system and call the appropriate function
get_ip() { get_ip() {
case "$(uname -s)" in case "$(uname -s)" in
Linux*|Darwin*) Linux*|Darwin*) echo $(get_ip_unix) ;;
echo $(get_ip_unix) CYGWIN*|MINGW32*|MSYS*|MINGW*) echo $(get_ip_windows) ;;
;; *) echo "Unsupported operating system"; exit 1 ;;
CYGWIN*|MINGW32*|MSYS*|MINGW*) esac
echo $(get_ip_windows)
;;
*)
echo "Unsupported operating system"
exit 1
;;
esac
} }