Skip to content

Commit 2d3cc52

Browse files
Update IP_Geolocation_Info.sh
added status code checked
1 parent 6a28773 commit 2d3cc52

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Network/IP_Geolocation_Info.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
# Send a curl request to ipinfo.io and capture the response
44
response=$(curl -s ipinfo.io)
55

6+
# Check the HTTP status code
7+
status_code=$(curl -s -w "%{http_code}" ipinfo.io -o /dev/null)
8+
9+
if [[ "$status_code" != "200" ]]; then
10+
echo "Error: HTTP request failed with status code $status_code"
11+
exit 1 # Exit with an error code
12+
fi
13+
614
# Extract the desired fields using jq
715
ip=$(echo "$response" | jq -r '.ip')
816
city=$(echo "$response" | jq -r '.city')
@@ -20,4 +28,6 @@ fi
2028
# Print the data row in CSV format, appending to the file
2129
echo "$ip,$city,$region,$country,$loc,$org,$postal" >> ipinfo.csv
2230

23-
echo "Data saved to ipinfo.csv"
31+
echo "Data saved to ipinfo.csv"
32+
33+
cat ipinfo.csv

0 commit comments

Comments
 (0)