5

I have a script that checks my public ip address every few minutes.
The problem is the ISP sometimes gives me cached pages (I know, I've used all the related args in wget, the isp is formed by a bunch of incompetent so-and-sos that apparently made their own super-efficient cache server) or error pages made by my own router.
And as a result wget saves the error page when it should save my ip address.

EDIT:
what I'm using to detect changes in ip address
http://paste.debian.net/292602/

womble
  • 96,255
  • 29
  • 175
  • 230
Behrooz
  • 163
  • 1
  • 1
  • 9
  • 3
    This doesn't make sense. wget _already_ doesn't save error pages by default. Also, have you tried a different service to obtain your public IP address? Coincidentally I am currently building one, which you can find at http://myip.addr.space/help . I try to set the right cache-control headers, so it ought to pass through an ISP's proxy. – Michael Hampton Aug 15 '15 at 20:13
  • @MichaelHampton I wrote my own http://78.47.35.18/ip-raw.php, it's a one liner – Behrooz Aug 15 '15 at 20:21
  • @MichaelHampton nice domain, that must have cost a lot. – Behrooz Aug 15 '15 at 20:21
  • Different responses based on user agent. Is that a good idea? And I'm using my own because I figured no one wants me fetch my ip every 60 seconds. – Behrooz Aug 15 '15 at 20:25
  • Only for the home page, if you load `/ip` you will always get raw text. – Michael Hampton Aug 15 '15 at 20:25
  • @MichaelHampton Could my problem be caused by using the -O option with wget? the manpage says it's the same as using > – Behrooz Aug 15 '15 at 20:27
  • Right, if you use `-O`, the file is always created (empty) even if there is an error downloading. – Michael Hampton Aug 15 '15 at 20:28
  • that's the problem, it's not empty. Instead it's a long error page. – Behrooz Aug 15 '15 at 20:29
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/27008/discussion-between-behrooz-and-michael-hampton). – Behrooz Aug 15 '15 at 20:34

1 Answers1

7

This code snippet should point your at right direction:

wget --server-response 78.47.35.18/ip-raw.php -O ip-current 2>&1| grep -c 'HTTP/1.1 200 OK'
1
is_200_ok=$(wget --server-response 78.47.35.18/ip-raw.php -O ip-current 2>&1| grep -c 'HTTP/1.1 200 OK')

echo $is_200_ok 
1

However i would use python or perl for this. It would be easier.

How it will look in your script:

#!/bin/bash

rm -f ip-current /tmp/ip-message-temp
touch ip-old

is_200_ok=$(wget --server-response 78.47.35.18/ip-blabl.php -O ip-tmp 2>&1| grep -c 'HTTP/1.1 200 OK')

if [ $is_200_ok == 1 ]; then
    mv ip-tmp ip-current
    echo "YES"
else
    echo "Got error instead of IP address :("
    exit 1
fi

Also avoid write dirrectly to syslog, it's much better to use logger:

NAME
 logger — a shell command interface to the syslog(3) system log module
Navern
  • 1,619
  • 1
  • 10
  • 14
  • Thank you, nice method. Michael Hampton helped me solve the problem in chat earlier. the solution was to change a flag in my router nvram and use --max-redirect 0 – Behrooz Aug 15 '15 at 21:02
  • Ok, then:) i will edit my answer. – Navern Aug 15 '15 at 21:05
  • I have slightly changed to matching part to `grep -c '200 OK')` because in case of redirection the output is slightly different like here `is_200_ok=$(wget "https://github.com/stopwords-iso/stopwords-de/blob/master/stopwords-de.txt?raw=true" -O "stopwords_de_tmp.txt" 2>&1| grep -c '200 OK')` – loretoparisi Jan 11 '19 at 11:57