DSL dynamic IP: an approach to get WAN IP via batch

N

noabody

This example uses free software and Windows XP/2003 commands to
retrieve the WAN IP from an external website. The method could easily
be adapted to a webpage contained in the router/modem.

I used a Win32 version of curl to pull the webpage at the command line.
Anyway here's the batch. www.dyndns.org is one of many websites that
will report the WAN IP. Choose whichever you like.

@echo off
for /F "usebackq tokens=6 delims= " %%A in (`curl -s
http://www.dyndns.org/cgi-bin/check_ip.cgi`) do for /F "usebackq
tokens=1 delims=<" %%B in ('%%A') do echo %%B


Here's the commands by themselves from the command line, notice how
variables have %% in the batch and only % when run from the command
line.

curl -s http://www.dyndns.org/cgi-bin/check_ip.cgi
<html><head><title>Current IP Check</title></head><body>Current IP
Address: 255.255.255.255</body></html>

Notice how the whole page outputs to the command line. Next I use the
FOR command to parse the IP from this.

for /F "usebackq tokens=6 delims= " %A in (`curl -s
http://www.dyndns.org/cgi-bin/check_ip.cgi`) do echo %A
255.255.255.255</body></html>

Typically the area surrounded by () is a text file but in this case I'm
going to pull the output of the curl command directly. FOR needs the
usebackq parameter and back quotes ('') to surround the curl command.
Delims= " and tokens=6 work together. The curl output is separated
into pieces by delims= " which is actually a blank space. Tokens=6
tells the command to use the sixth piece of the output. The pieces are
shown below:
1) <html><head><title>Current
2) IP
3) Check</title></head><body>Current
4) IP
5) Address:
6) 255.255.255.255</body></html>

The output 255.255.255.255</body></html> is currently stored in the
variable %A. I use a second FOR command to separate this output at <
and then use the first piece.
1) 255.255.255.255
2) </body>
3) </html>

for /F "usebackq tokens=6 delims= " %A in (`curl -s
http://www.dyndns.org/cgi-bin/check_ip.cgi`) do for /F "usebackq
tokens=1 delims=<" %B in ('%A') do echo %B
255.255.255.255


The FOR command does a passable job of parsing the data and I use it
because I am 1) familiar with it and 2) it's built into windows. Win32
versions of grep or sed would probably be much better. I like the fact
that I can parse the output of the curl command directly without having
a intermediate text file.

Please reply with other methods to accomplish this task.
 
R

Robert L [MS-MVP]

Thank you for sharing this with us.

Bob Lin, MS-MVP, MCSE & CNE
Networking, Internet, Routing, VPN Troubleshooting on http://www.ChicagoTech.net
How to Setup Windows, Network, VPN & Remote Access on http://www.HowToNetworking.com
This example uses free software and Windows XP/2003 commands to
retrieve the WAN IP from an external website. The method could easily
be adapted to a webpage contained in the router/modem.

I used a Win32 version of curl to pull the webpage at the command line.
Anyway here's the batch. www.dyndns.org is one of many websites that
will report the WAN IP. Choose whichever you like.

@echo off
for /F "usebackq tokens=6 delims= " %%A in (`curl -s
http://www.dyndns.org/cgi-bin/check_ip.cgi`) do for /F "usebackq
tokens=1 delims=<" %%B in ('%%A') do echo %%B


Here's the commands by themselves from the command line, notice how
variables have %% in the batch and only % when run from the command
line.

curl -s http://www.dyndns.org/cgi-bin/check_ip.cgi
<html><head><title>Current IP Check</title></head><body>Current IP
Address: 255.255.255.255</body></html>

Notice how the whole page outputs to the command line. Next I use the
FOR command to parse the IP from this.

for /F "usebackq tokens=6 delims= " %A in (`curl -s
http://www.dyndns.org/cgi-bin/check_ip.cgi`) do echo %A
255.255.255.255</body></html>

Typically the area surrounded by () is a text file but in this case I'm
going to pull the output of the curl command directly. FOR needs the
usebackq parameter and back quotes ('') to surround the curl command.
Delims= " and tokens=6 work together. The curl output is separated
into pieces by delims= " which is actually a blank space. Tokens=6
tells the command to use the sixth piece of the output. The pieces are
shown below:
1) <html><head><title>Current
2) IP
3) Check</title></head><body>Current
4) IP
5) Address:
6) 255.255.255.255</body></html>

The output 255.255.255.255</body></html> is currently stored in the
variable %A. I use a second FOR command to separate this output at <
and then use the first piece.
1) 255.255.255.255
2) </body>
3) </html>

for /F "usebackq tokens=6 delims= " %A in (`curl -s
http://www.dyndns.org/cgi-bin/check_ip.cgi`) do for /F "usebackq
tokens=1 delims=<" %B in ('%A') do echo %B
255.255.255.255


The FOR command does a passable job of parsing the data and I use it
because I am 1) familiar with it and 2) it's built into windows. Win32
versions of grep or sed would probably be much better. I like the fact
that I can parse the output of the curl command directly without having
a intermediate text file.

Please reply with other methods to accomplish this task.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top