Page 1 of 1

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

Posted: 14 Dec 2017 09:35
by fireYtail
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???

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

Posted: 14 Dec 2017 10:09
by Squashman
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.

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

Posted: 14 Dec 2017 12:47
by Hackoo

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

Posted: 14 Dec 2017 13:51
by fireYtail
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.

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

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

Steffen

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

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

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

Posted: 14 Dec 2017 14:14
by fireYtail
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.

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

Posted: 14 Dec 2017 14:28
by aGerman
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

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

Posted: 14 Dec 2017 14:37
by Squashman
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"

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

Posted: 14 Dec 2017 15:23
by fireYtail
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?

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

Posted: 14 Dec 2017 15:43
by aGerman
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

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

Posted: 14 Dec 2017 15:52
by Squashman
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.

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

Posted: 15 Dec 2017 08:29
by fireYtail
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?

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

Posted: 27 Nov 2018 14:07
by aGerman
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