Last active
June 17, 2025 20:45
-
-
Save DiracSpace/64a3f1440cd0069c7c4cea6a4ac6238b to your computer and use it in GitHub Desktop.
Check IP address with loading in PowerShell and Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Load { | |
| param( | |
| [scriptblock]$function, | |
| [string]$Label | |
| ) | |
| $job = Start-Job -ScriptBlock $function | |
| $symbols = @("⣾⣿", "⣽⣿", "⣻⣿", "⢿⣿", "⡿⣿", "⣟⣿", "⣯⣿", "⣷⣿", | |
| "⣿⣾", "⣿⣽", "⣿⣻", "⣿⢿", "⣿⡿", "⣿⣟", "⣿⣯", "⣿⣷") | |
| $i = 0 | |
| while ($job.State -eq "Running") { | |
| $symbol = $symbols[$i] | |
| Write-Host -NoNewLine "`r$symbol $Label" -ForegroundColor Green | |
| Start-Sleep -Milliseconds 100 | |
| $i++ | |
| if ($i -eq $symbols.Count) { | |
| $i = 0 | |
| } | |
| } | |
| Write-Host -NoNewLine "`r" # Clear spinner when done | |
| Receive-Job -Job $job | |
| Remove-Job -Job $job | |
| } | |
| function Get-PublicIP { | |
| Load -function { | |
| try { | |
| $response = Invoke-RestMethod -Uri "https://api.ipify.org?format=json" | |
| if ($response -and $response.ip) { | |
| Write-Output "`rYour public IP address is: $($response.ip)" | |
| } | |
| else { | |
| Write-Output "`rUnable to determine public IP address." | |
| } | |
| } | |
| catch { | |
| Write-Output "`rAn error occurred: $_" | |
| } | |
| } -Label "Fetching public IP..." | |
| } | |
| Set-Alias -Name myip -Value Get-PublicIP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| load_with_spinner() { | |
| local func="$1" | |
| local label="$2" | |
| local symbols=("⣾⣿" "⣽⣿" "⣻⣿" "⢿⣿" "⡿⣿" "⣟⣿" "⣯⣿" "⣷⣿" "⣿⣾" "⣿⣽" "⣿⣻" "⣿⢿" "⣿⡿" "⣿⣟" "⣿⣯" "⣿⣷") | |
| local i=0 | |
| local temp_file=$(mktemp) | |
| ($func > "$temp_file" 2>&1) & | |
| local job_pid=$! | |
| while kill -0 "$job_pid" 2>/dev/null; do | |
| printf "\r\033[32m%s %s\033[0m" "${symbols[$i]}" "$label" | |
| sleep 0.1 | |
| ((i++)) | |
| if [ $i -eq ${#symbols[@]} ]; then | |
| i=0 | |
| fi | |
| done | |
| wait "$job_pid" | |
| local exit_code=$? | |
| printf "\r\033[K" | |
| cat "$temp_file" | |
| rm -f "$temp_file" | |
| return $exit_code | |
| } | |
| fetch_public_ip() { | |
| if command -v curl >/dev/null 2>&1; then | |
| local response=$(curl -s "https://api.ipify.org?format=json" 2>/dev/null) | |
| if [ $? -eq 0 ] && [ -n "$response" ]; then | |
| local ip=$(echo "$response" | grep -o '"ip":"[^"]*"' | cut -d'"' -f4) | |
| if [ -n "$ip" ]; then | |
| echo "Your public IP address is: $ip" | |
| return 0 | |
| fi | |
| fi | |
| elif command -v wget >/dev/null 2>&1; then | |
| local response=$(wget -qO- "https://api.ipify.org?format=json" 2>/dev/null) | |
| if [ $? -eq 0 ] && [ -n "$response" ]; then | |
| local ip=$(echo "$response" | grep -o '"ip":"[^"]*"' | cut -d'"' -f4) | |
| if [ -n "$ip" ]; then | |
| echo "Your public IP address is: $ip" | |
| return 0 | |
| fi | |
| fi | |
| else | |
| echo "Error: Neither curl nor wget is available" | |
| return 1 | |
| fi | |
| echo "Unable to determine public IP address" | |
| return 1 | |
| } | |
| get_public_ip() { | |
| load_with_spinner fetch_public_ip "Fetching public IP..." | |
| } | |
| alias myip='get_public_ip' | |
| if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
| get_public_ip | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment