Find the answer to your Linux question:
Results 1 to 9 of 9
Hello all, I have a webserver running on my home network, and I am trying to find the best way to have my dynamic IP address constantly updated to afraid.org. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    34

    Using a bash script to read my external ip address


    Hello all,

    I have a webserver running on my home network, and I am trying to find the best way to have my dynamic IP address constantly updated to afraid.org. I am currently trying to use the fdclient php script in order to update my ip address. however, it wants to read the external ip address from the output of another script.

    It suggests using a script called getmyip.php, but this only outputs my local ip address.

    I know that dyndns.org has "checkip.dyndns.com", but this doesn't output solely the ip address, which fdupdate requires. I am new to bash scripting, so does anyone know how i could take the page at checkip.dyndns.com and print only the ip address to a file that can be read by the fdupdate script?

    Sorry if this is a very obvious thing to do. If someone could point me to a good tutorial or would care to explain it, i would be very thankful.

    Thanks!

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Ugly, but it works

    Code:
    #!/bin/bash
    wget http://checkip.dyndns.com/ 2>1 /dev/null
    cat index.html | awk '{print $6}'|cut -d"<" -f1
    #rm index.html
    I commented out the `rm` because it removes things. The `wget` command creates a file called 'index.html' is the current directory, and appends a number if another index.html already is in the current directory. `rm`, if uncommented, would remove that and unless that is what you wanted, it would not be very nice. And you can direct `wget` to use another file as output. But it's not '-o outputfile' and I'm to lazy to look further
    Can't tell an OS by it's GUI

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Location
    Virginia, USA
    Posts
    18
    Another good method is to use the curl command. It basically dumps the HTML of the web page. Then you can use a regex to find the IP address within the code. Eg:

    Code:
    curl -s URLHere | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"

  4. #4
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    ooo i like that....imma use curl more often...

  5. #5
    Just Joined!
    Join Date
    Feb 2008
    Posts
    34
    Quote Originally Posted by flakblas View Post
    Another good method is to use the curl command. It basically dumps the HTML of the web page. Then you can use a regex to find the IP address within the code. Eg:

    Code:
    curl -s URLHere | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
    What do i insert in the [0-9] spaces?

  6. #6
    Just Joined!
    Join Date
    Jun 2009
    Posts
    1
    Quote Originally Posted by plr4ever View Post
    What do i insert in the [0-9] spaces?
    You don't change the 0-9 section, that is just a regular expression to search for the IP address in the text. Just need to change the URLHere bit to something.
    ie
    Code:
    curl -s checkip.dyndns.com | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
    So if you paste that into command line it should just give you your IP address.

    Just make sure you don't schedule it more often than once every 10 minutes, otherwise you might end up being blocked.

    PS I like the curl version thanks flakblas!

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Freston View Post
    Ugly, but it works

    Code:
    #!/bin/bash
    wget http://checkip.dyndns.com/ 2>1 /dev/null
    cat index.html | awk '{print $6}'|cut -d"<" -f1
    #rm index.html
    I commented out the `rm` because it removes things. The `wget` command creates a file called 'index.html' is the current directory, and appends a number if another index.html already is in the current directory. `rm`, if uncommented, would remove that and unless that is what you wanted, it would not be very nice. And you can direct `wget` to use another file as output. But it's not '-o outputfile' and I'm to lazy to look further
    use -O- -q of wget to not create a temp file and then pipe to awk, without cat or cut
    Code:
    # wget -q -O-  http://checkip.dyndns.com/| awk '{gsub(/.*Current IP Address:|<\/body>.*/,"")}1'

  8. #8
    Just Joined!
    Join Date
    Feb 2008
    Posts
    34
    @FlakBlas

    Quote Originally Posted by flakblas View Post
    Another good method is to use the curl command. It basically dumps the HTML of the web page. Then you can use a regex to find the IP address within the code. Eg:

    Code:
    curl -s URLHere | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
    Thank you so much! this works perfectly. Now i will just pipe the output into a text file that the php script can read.

  9. #9
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    Just FYI, there is a service that does not requires grep:

    curl http://www.whatismyip.com/automation/n09230945.asp

    Technically, it is safer than try to detect the IP string, because if the dyndns page includes some strange HTML code to render the IP string (e.g. some span tag on each digit), the grep solution will fail.
    EOF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •