I need to update a file which contain my external ip address on a server via scp. Before updating the file I want to ensure that it contain an actual IP address else it must not be updated on the server.

My cron script that check for my IP often result in garbage error result.

How can I achieve my goal?

  • Can you show rhe script you run to check if the file contains an IP, and the results you get so far? – Vincent Achard Oct 18 '19 at 8:23
  • curl icanhazip.com > ip-location1.txt – grimdex Oct 18 '19 at 8:36
  • result is an ip address in a file name ip-location1.txt – grimdex Oct 18 '19 at 8:37
  • when there is an error i get this instead <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="utf-8"> <title>Application Error</title> <style media="screen"> html,body,iframe { margin: 0; padding: 0; } html,body { height: 100%; overflow: hidden; } iframe { width: 100%; height: 100%; border: 0; } </style> </head> <body> – grimdex Oct 18 '19 at 8:38
  • I do not have any script to check the file for ip, I am looking for one – grimdex Oct 18 '19 at 8:39

If you're currently using

curl icanhazip.com > ip-location1.txt

then by adding the -f option you can make curl return an error (and no output) if the webserver does, and so there hopefully won't be a need to figure out whether the output is an IP address or a HTML error message.

curl -f icanhazip.com > ip-location1.txt

With a bit more complexity, you can add a retry feature:

for i in 1 2 3   # if you want more retries, add more numbers here
    curl -f icanhazip.com > ip-location1.txt
    if [ $? -eq 0 ] && [ -s ip-location1.txt ]
    # if we get here, the current attempt failed
    sleep 5  # be nice and wait a bit before retrying instead of spamming the service
if [ ! -s ip-location1.txt ]
    echo "i cannot haz ip."
    # do whatever you want to do in case of all the retries fail
|improve this answer|||||
  • thanks, this solution help. file still result blank if no ip at moment of check. Im looking for a way to check this file for a new IP changed from previous known IP before uploading via scp so that it check and confirm that the file possess an IP address – grimdex Oct 18 '19 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.