Add projects/curl.md

This commit is contained in:
2024-04-19 01:08:11 +00:00
parent f9f70ad14c
commit 6458cc2c60

84
projects/curl.md Normal file
View File

@@ -0,0 +1,84 @@
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.
- **Detailed IP and Connection Info**:
- Command: `curl ipinfo.io/json`
- Output: JSON-formatted details including IP, location, ISP, and more.
#### **Alternative Services**
- **Icanhazip**:
- Command: `curl icanhazip.com`
- Description: Returns the IP address in plain text.
- **Ifconfig.co**:
- Command: `curl ifconfig.co/json`
- Description: Provides detailed JSON output similar to ipinfo.io.
### Advanced Use Cases
#### **1. Logging IP Changes**
Track changes in your public IP address over time, useful for dynamic IP users:
```bash
#!/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:
```bash
#!/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:
```bash
#!/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):
```bash
# 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.