Skip to content

Instantly share code, notes, and snippets.

@electblake
Last active May 16, 2024 04:14
Show Gist options
  • Save electblake/c173af23b05bf0b029600836224f4006 to your computer and use it in GitHub Desktop.
Save electblake/c173af23b05bf0b029600836224f4006 to your computer and use it in GitHub Desktop.
list and search socks servers using nordvpn api
#!/bin/bash
# Fetch data from API and store it in a variable
data=$(curl --silent --globoff "https://api.nordvpn.com/v1/servers?filters[servers_technologies][identifier]=socks&limit=0" | \
jq '[.[] | {
name: .name,
created_at: .created_at,
hostname: .hostname,
ip: .station,
load: .load,
status: .status,
location: (.locations[0].country.city.name + ", " + .locations[0].country.name),
country: .locations[0].country.name
}]')
# Check if data is not empty
if [ -z "$data" ]; then
echo "Failed to fetch data or data is empty."
exit 1
fi
# Ask for sorting preference
echo "How would you like to sort the data? Options: load, name, status"
read sort_option
# Ensure valid sort option is chosen
if [[ "$sort_option" != "load" && "$sort_option" != "name" && "$sort_option" != "status" ]]; then
echo "Invalid sort option selected."
exit 1
fi
# Ask for country filter preference
echo "Do you want to filter by country? Enter country name or 'no' to skip:"
read country_filter
# Apply sorting and optional filtering
if [ "$country_filter" = "no" ]; then
jq_command="sort_by(.$sort_option)"
else
jq_command="map(select(.country == \"$country_filter\")) | sort_by(.$sort_option)"
fi
# Execute jq command on the variable and display the result
echo "$data" | jq "$jq_command"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment