3.0 KiB
Here's a concise reference guide for using curl to retrieve public IP and device information through various online services. This guide also includes some advanced use cases for integrating these commands into broader workflows.
Basic Commands
Get Public IP Address
- Plain IP Address:
- Command:
curl ifconfig.me - Output: Just your public IP address in plain text.
- Command:
- Detailed IP and Connection Info:
- Command:
curl ipinfo.io/json - Output: JSON-formatted details including IP, location, ISP, and more.
- Command:
Alternative Services
- Icanhazip:
- Command:
curl icanhazip.com - Description: Returns the IP address in plain text.
- Command:
- Ifconfig.co:
- Command:
curl ifconfig.co/json - Description: Provides detailed JSON output similar to ipinfo.io.
- Command:
Advanced Use Cases
1. Logging IP Changes
Track changes in your public IP address over time, useful for dynamic IP users:
#!/bin/bash
CURRENT_IP=$(curl -s ifconfig.me)
LOG_FILE="ip_change_log.txt"
if [ ! -f $LOG_FILE ]; then
echo "Starting IP: $CURRENT_IP" > $LOG_FILE
fi
LAST_IP=$(tail -n 1 $LOG_FILE | cut -d ' ' -f 3)
if [ "$CURRENT_IP" != "$LAST_IP" ]; then
echo "$(date): IP changed to $CURRENT_IP" >> $LOG_FILE
fi
2. Conditional Actions Based on IP Location
Perform actions based on the geographical location of your IP:
#!/bin/bash
IP_INFO=$(curl -s ipinfo.io/json)
COUNTRY=$(echo $IP_INFO | jq -r '.country')
if [ "$COUNTRY" == "US" ]; then
echo "Execute tasks specific to US"
# Add commands here
else
echo "Outside US: actions taken will differ"
# Different commands here
fi
3. Combining IP Data with Network Diagnostics
Combine IP information retrieval with local network diagnostics for comprehensive reports:
#!/bin/bash
echo "Gathering Network Info..."
PUBLIC_IP=$(curl -s ifconfig.me)
PING_RESULT=$(ping -c 4 example.com)
echo "Public IP: $PUBLIC_IP"
echo "Ping Test Results:"
echo "$PING_RESULT"
4. Scheduled IP Check with Notifications
Use cron to schedule regular IP checks and send notifications if the IP changes (Linux):
# Add this script to cron jobs (crontab -e)
* */6 * * * /path/to/ip_check_script.sh
The script could use system notifications or email to alert you to changes.
Considerations
- Privacy: Be cautious with how and where you use these services, as you are revealing your IP to them.
- Rate Limiting: Frequent requests to these services may be rate-limited. Use responsibly.
- Dependence on External Services: These commands depend on external websites, which might change their output format or availability.
This guide should serve as a comprehensive starting point for both basic and advanced usage of curl for IP and network-related tasks. Adjust and expand based on your specific requirements and environment.
curl -I "https://prod-grants-gov-chatbot.s3.amazonaws.com/extracts/GrantsDBExtract$(date +\%Y\%m\%d)v2.zip"
watch -n 1 'ls -l /home/medusa/grants_extracts/; tail -n 5 /home/medusa/cron.log'