How to export your public IP address to a text file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fireYtail
Posts: 7
Joined: 14 Dec 2017 08:51
Location: Spain
Contact:

How to export your public IP address to a text file?

#1 Post by fireYtail » 14 Dec 2017 09:35

Hello, I need some help. As the subject says, I want to get my public IP address in batch somehow and then export it to a file. This is the only thing I've found online, but it's giving me a wrong address, so it's not working (for me??):

Code: Select all

@Echo off
for /f "tokens=2 delims=: " %%A in (
  'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A
Echo External IP is : %ExtIP%
I do know that this just shows an IP, in order to save it we'd have to change this code a little bit, like this:

Code: Select all

@Echo off
for /f "tokens=2 delims=: " %%A in (
  'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A
Echo %ExtIP% > FileName.txt
However it doesn't matter since it just doesn't work. Any suggestions, please???

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to export your public IP address to a text file?

#2 Post by Squashman » 14 Dec 2017 10:09

I normally use "internet address" for the search string.

Code: Select all

nslookup -debug myip.opendns.com. resolver1.opendns.com 2>NUL |findstr /C:"internet address"
Could we see you unfiltered nslookup output please.


fireYtail
Posts: 7
Joined: 14 Dec 2017 08:51
Location: Spain
Contact:

Re: How to export your public IP address to a text file?

#4 Post by fireYtail » 14 Dec 2017 13:51

Squashman wrote:
14 Dec 2017 10:09
I normally use "internet address" for the search string.

Code: Select all

nslookup -debug myip.opendns.com. resolver1.opendns.com 2>NUL |findstr /C:"internet address"
Could we see you unfiltered nslookup output please.
Unfortunately your code outputs absolutely nothing. If I change "internet address" to "Address:" it keeps on telling me it's 208.67.222.222, when it's not. Adding or removing the "-debug" argument won't change the result in either case.
Hackoo wrote:
14 Dec 2017 12:47
You can take à look at this https://stackoverflow.com/questions/472 ... ve#tab-top
Sorry but while that code outputs my internal LAN IP and MAC address correctly, it still lists 208.67.222.222 as my public IP address. Going to various IP-checking sites online tells me a different address which must be my true one, however.

The 222.222 IP address is from San Francisco, California, which isn't even my continent.

EDIT: 208.67.222.222 is resolver1.opendns.com's IP address, not mine.
Last edited by fireYtail on 14 Dec 2017 14:04, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to export your public IP address to a text file?

#5 Post by aGerman » 14 Dec 2017 13:58

Please run
nslookup myip.opendns.com. resolver1.opendns.com >"ip.txt" 2>&1
and copy the content of ip.txt.

Steffen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to export your public IP address to a text file?

#6 Post by Squashman » 14 Dec 2017 14:02

Squashman wrote:
14 Dec 2017 10:09
Could we see you unfiltered nslookup output please.
I really do not like asking twice.

fireYtail
Posts: 7
Joined: 14 Dec 2017 08:51
Location: Spain
Contact:

Re: How to export your public IP address to a text file?

#7 Post by fireYtail » 14 Dec 2017 14:14

aGerman wrote:
14 Dec 2017 13:58
Please run
nslookup myip.opendns.com. resolver1.opendns.com >"ip.txt" 2>&1
and copy the content of ip.txt.

Steffen

Code: Select all

*** resolver1.opendns.com no encuentra myip.opendns.com.: Non-existent domain

Servidor:  resolver1.opendns.com
Address:  208.67.222.222

Thank you very much. "no encuentra" is Spanish for "doesn't find". "Servidor" is Spanish for "Server".
Squashman wrote:
14 Dec 2017 14:02
Squashman wrote:
14 Dec 2017 10:09
Could we see you unfiltered nslookup output please.
I really do not like asking twice.
I fear I don't understand what you mean by "unfiltered" then. Maybe the whole code I posted above is what you were looking for?

P.S.: http://domaintoipconverter.com/index.php fails to find myip.opendns.com as well. Looks like a dead domain.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to export your public IP address to a text file?

#8 Post by aGerman » 14 Dec 2017 14:28

fireYtail wrote:
14 Dec 2017 14:14
I fear I don't understand what you mean by "unfiltered" then. Maybe the whole code I posted above is what you were looking for?
Basically what you did using my command line.
You seem to use some kind of proxy or your provider really assigned address 208.67.222.222 to your connection. Open this address in your browser:
http://myip.dnsomatic.com/
fireYtail wrote:
14 Dec 2017 14:14
Looks like a dead domain.
No, your code works flawlessly for me.

Steffen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to export your public IP address to a text file?

#9 Post by Squashman » 14 Dec 2017 14:37

You need to use the debug switch like I posted in my previous post to get the internet address output. All you did was change your "FILTER" with the find command, which is why you didn't get any output.

Code: Select all

nslookup -debug myip.opendns.com resolver1.opendns.com 2>nul|findstr /C:"internet address"

fireYtail
Posts: 7
Joined: 14 Dec 2017 08:51
Location: Spain
Contact:

Re: How to export your public IP address to a text file?

#10 Post by fireYtail » 14 Dec 2017 15:23

aGerman wrote:
14 Dec 2017 14:28
fireYtail wrote:
14 Dec 2017 14:14
I fear I don't understand what you mean by "unfiltered" then. Maybe the whole code I posted above is what you were looking for?
Basically what you did using my command line.
You seem to use some kind of proxy or your provider really assigned address 208.67.222.222 to your connection. Open this address in your browser:
http://myip.dnsomatic.com/
fireYtail wrote:
14 Dec 2017 14:14
Looks like a dead domain.
No, your code works flawlessly for me.

Steffen
That "website" doesn't show 208.67.222.222 as my address, but my real IP. It works fine. Is there any batch command or command line program that will download it to a file?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to export your public IP address to a text file?

#11 Post by aGerman » 14 Dec 2017 15:43

You could use a hybrid script.

*.bat

Code: Select all

@if (@X)==(@Y) @end /* valid line in Batch and JScript

:: Batch Part:
@echo off &setlocal
for /f "delims=" %%i in ('cscript //nologo //e:jscript "%~fs0"') do set "ExtIP=%%i"
echo %ExtIP%
pause


:: JScript Part:
goto :eof  */ try {
  var objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  objXMLHTTP.open("GET", "http://myip.dnsomatic.com/", false);
  objXMLHTTP.send();
  if (objXMLHTTP.readyState == 4 && objXMLHTTP.status == 200) {
    WScript.StdOut.WriteLine(objXMLHTTP.responseText);
    WScript.Quit(0);
  }
  else {
    WScript.StdErr.WriteLine("Unable to finish request or page not found.");
    WScript.Quit(1);
  }
}
catch (e) {
  WScript.StdErr.WriteLine(e.name + ": " + e.message);
  WScript.Quit(1);  
}
Steffen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to export your public IP address to a text file?

#12 Post by Squashman » 14 Dec 2017 15:52

Squashman wrote:
14 Dec 2017 14:37
You need to use the debug switch like I posted in my previous post to get the internet address output. All you did was change your "FILTER" with the find command, which is why you didn't get any output.

Code: Select all

nslookup -debug myip.opendns.com resolver1.opendns.com 2>nul|findstr /C:"internet address"
Sure would like to see the debug output from NSLOOKUP.

fireYtail
Posts: 7
Joined: 14 Dec 2017 08:51
Location: Spain
Contact:

Re: How to export your public IP address to a text file?

#13 Post by fireYtail » 15 Dec 2017 08:29

aGerman wrote:
14 Dec 2017 15:43
You could use a hybrid script.

*.bat

Code: Select all

@if (@X)==(@Y) @end /* valid line in Batch and JScript

:: Batch Part:
@echo off &setlocal
for /f "delims=" %%i in ('cscript //nologo //e:jscript "%~fs0"') do set "ExtIP=%%i"
echo %ExtIP%
pause


:: JScript Part:
goto :eof  */ try {
  var objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  objXMLHTTP.open("GET", "http://myip.dnsomatic.com/", false);
  objXMLHTTP.send();
  if (objXMLHTTP.readyState == 4 && objXMLHTTP.status == 200) {
    WScript.StdOut.WriteLine(objXMLHTTP.responseText);
    WScript.Quit(0);
  }
  else {
    WScript.StdErr.WriteLine("Unable to finish request or page not found.");
    WScript.Quit(1);
  }
}
catch (e) {
  WScript.StdErr.WriteLine(e.name + ": " + e.message);
  WScript.Quit(1);  
}
Steffen
Thank you so much, this works flawlessly and it's fast! Does it require to have Java installed though?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to export your public IP address to a text file?

#14 Post by aGerman » 27 Nov 2018 14:07

fireYtail wrote:
15 Dec 2017 08:29
Does it require to have Java installed though?
No. JScript is the Microsoft implementation of ECMAScript (commonly known as its MIME type JavaScript). On Windows it works also as stand-alone in the WSH runtime environment. Neither of them has anything to do with Java (which is a programming language developed by Sun Microsystems, now Oracle Corporation). Never confuse JavaScript with Java.

Steffen


Last bumped by fireYtail on 27 Nov 2018 14:07.

Post Reply