batch find external ipaddress

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Billd59
Posts: 5
Joined: 02 Jul 2012 17:43

batch find external ipaddress

#1 Post by Billd59 » 20 Jul 2012 12:33

I know If you go to a website like Ipchicken you can get your external IPaddress. can you send the contents of a website like IPchicken to a file with bach? For example: If I open explorer with start In batch and have It goto IPChicken I just want to copy my External IP to a Txt or bat file. All I want It to have a batch file tell me external Ipaddress.

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

Re: batch find external ipaddress

#2 Post by Squashman » 20 Jul 2012 13:57

You can use a 3rd party utility like WGET and point it at this website: http://automation.whatismyip.com/n09230945.asp

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: batch find external ipaddress

#3 Post by abc0502 » 20 Jul 2012 14:27

I think there was a topic like that here i don't remember exactly, but you will need to use a vbscript, batch won't help:
It get the IP from http://checkip.dyndns.org

This is a modified code from the original it will give you a txt file called IP.txt Contain your External IP "Remember it's a VBscript"

Code: Select all

Const ForReading = 1
Const ForAppending = 8
Dim ipLog, objHTTP, strHTML, varStart
Dim varStop, strIP, strCurrIP, objFSO
Dim txtFile, strLine, objShell

' Log for tracking external IP addresses
ipLog = "IP.txt"

' Get current external IP address from web
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
objHTTP.Send()
strHTML = objHTTP.ResponseText

' Extarct IP from HTML if HTML was recieved
If strHTML <> "" Then
   varStart = InStr(1, strHTML, "Current IP Address:", vbTextCompare) + 19
   If varStart Then varStop = InStr(varStart, strHTML, "</body>", vbTextCompare)
   If varStart And varStop Then strIP = Mid(strHTML, varStart, varStop - varStart)
Else
   strIP = "Unavailable"
End If
' Remove preceeding or trailing spaces
strCurrIP = Trim(strIP)

' Check for log file and last log entry
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not (objFSO.FileExists(ipLog)) Then
   ' If log file doesn't exist create it
   Set txtFile = objFSO.CreateTextFile(ipLog, True)
   strIP = ""
Else
   ' Get last external IP address entry from log file
   Set txtFile = objFSO.OpenTextFile(ipLog, ForReading)
   Do Until txtFile.AtEndOfStream
      strLine = txtFile.ReadLine
      If Len(strLine) > 0 Then
        strIP = strLine
      End If
   Loop
   
End If
txtFile.Close

' Extarct last external IP from log file entry
If strIP <> "" Then
   varStart = 1
   varStop = InStr(varStart, strIP, ",", vbTextCompare) - 1
   If varStop Then strIP = Mid(strIP, varStart, varStop - varStart)
   ' Remove preceeding or trailing spaces
   Trim(strIP)
Else
   strIP = "Unavailable"
End If

' Check if external IP has changed
If strCurrIP = strIP Then
Else
   ' If changed log to file and display IP
   Set txtFile = objFSO.OpenTextFile(ipLog, ForAppending)
   txtFile.Write(strCurrIP)
   txtFile.Close
End If

' Clear variables
Set ipLog = Nothing
Set objHTTP = Nothing
Set strHTML = Nothing
Set varStart = Nothing
Set varStop = Nothing
Set strIP = Nothing
Set strCurrIP = Nothing
Set objFSO = Nothing
Set txtFile = Nothing
Set strLine = Nothing
Set objShell = Nothing

You can compine this code in abatch file that make the vbscript code then run it and extract the ip and pop it up in a box with OK botten using

Code: Select all

MSG * %ip%

I use it like that some times

Billd59
Posts: 5
Joined: 02 Jul 2012 17:43

Re: batch find external ipaddress

#4 Post by Billd59 » 20 Jul 2012 14:39

abc0502 wrote:I think there was a topic like that here i don't remember exactly, but you will need to use a vbscript, batch won't help:
It get the IP from http://checkip.dyndns.org

This is a modified code from the original it will give you a txt file called IP.txt Contain your External IP "Remember it's a VBscript"

Code: Select all

Const ForReading = 1
Const ForAppending = 8
Dim ipLog, objHTTP, strHTML, varStart
Dim varStop, strIP, strCurrIP, objFSO
Dim txtFile, strLine, objShell

' Log for tracking external IP addresses
ipLog = "IP.txt"

' Get current external IP address from web
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
objHTTP.Send()
strHTML = objHTTP.ResponseText

' Extarct IP from HTML if HTML was recieved
If strHTML <> "" Then
   varStart = InStr(1, strHTML, "Current IP Address:", vbTextCompare) + 19
   If varStart Then varStop = InStr(varStart, strHTML, "</body>", vbTextCompare)
   If varStart And varStop Then strIP = Mid(strHTML, varStart, varStop - varStart)
Else
   strIP = "Unavailable"
End If
' Remove preceeding or trailing spaces
strCurrIP = Trim(strIP)

' Check for log file and last log entry
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not (objFSO.FileExists(ipLog)) Then
   ' If log file doesn't exist create it
   Set txtFile = objFSO.CreateTextFile(ipLog, True)
   strIP = ""
Else
   ' Get last external IP address entry from log file
   Set txtFile = objFSO.OpenTextFile(ipLog, ForReading)
   Do Until txtFile.AtEndOfStream
      strLine = txtFile.ReadLine
      If Len(strLine) > 0 Then
        strIP = strLine
      End If
   Loop
   
End If
txtFile.Close

' Extarct last external IP from log file entry
If strIP <> "" Then
   varStart = 1
   varStop = InStr(varStart, strIP, ",", vbTextCompare) - 1
   If varStop Then strIP = Mid(strIP, varStart, varStop - varStart)
   ' Remove preceeding or trailing spaces
   Trim(strIP)
Else
   strIP = "Unavailable"
End If

' Check if external IP has changed
If strCurrIP = strIP Then
Else
   ' If changed log to file and display IP
   Set txtFile = objFSO.OpenTextFile(ipLog, ForAppending)
   txtFile.Write(strCurrIP)
   txtFile.Close
End If

' Clear variables
Set ipLog = Nothing
Set objHTTP = Nothing
Set strHTML = Nothing
Set varStart = Nothing
Set varStop = Nothing
Set strIP = Nothing
Set strCurrIP = Nothing
Set objFSO = Nothing
Set txtFile = Nothing
Set strLine = Nothing
Set objShell = Nothing

You can compine this code in abatch file that make the vbscript code then run it and extract the ip and pop it up in a box with OK botten using

Code: Select all

MSG * %ip%

I use it like that some times


I don't know VBscript yet. I am just learning Batch files. I don't have VB yet. So I would not know how to run It.

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

Re: batch find external ipaddress

#5 Post by Squashman » 20 Jul 2012 16:25

Open up notepad just like you would to make a batch file. Copy and paste the code he gave you into notepad and save the file as myscript.vbs
Close notepad and double click the vbscript file you just created.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: batch find external ipaddress

#6 Post by Liviu » 20 Jul 2012 16:26

Billd59 wrote:I don't know VBscript yet. I am just learning Batch files. I don't have VB yet. So I would not know how to run It.
Just save it as a text file with extension .VBS and run it at the command line. Note that this is VBScript (which comes with Windows), not VB (which sells separately).

Don't know that what you are asking for is possible using just batch and the builtin commands. As suggested above, instead of VBS you could also use something like WGET (for example the one from http://gnuwin32.sourceforge.net/packages/wget.htm) or CURL (http://curl.haxx.se/download.html).

Here is a snippet using the latter. Make sure to mind the warning on the first line. To suppress the verbose CURL output and see just the IP echo line, run with 2>nul.

Code: Select all

:: do *NOT* run more often than once every 5 minutes
:: see http://www.whatismyip.com/faq/automation.asp

@echo off
setlocal disableDelayedExpansion

set "wip="
for /f "delims=" %%a in ('curl http://automation.whatismyip.com/n09230945.asp') do set "wip=%%a"

echo public IP = %wip%

Liviu

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: batch find external ipaddress

#7 Post by Liviu » 20 Jul 2012 16:37

FWIW a different approach, simpler but only suitable for static locations (like one's home network), is to register a (free) name with dyndns.org, then ping the name and extract the (public) IP from the command output.

Liviu

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

Re: batch find external ipaddress

#8 Post by Squashman » 20 Jul 2012 17:04

Liviu wrote:FWIW a different approach, simpler but only suitable for static locations (like one's home network), is to register a (free) name with dyndns.org, then ping the name and extract the (public) IP from the command output.

Liviu

Good idea! Not sure why I did't think of that. I use NO-IP for my network.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: batch find external ipaddress

#9 Post by Liviu » 20 Jul 2012 18:13

Dyndns works even more smoothly if you happen to have a router or modem with a builtin DDNS updater, in which case there is no separate client side utility required to run in the background. Don't know whether/how it will work once ISPs start dispatching IPv6 addresses, but for home users that's probably still a few good years away. And, by that time, any batch code relying on the IPv4 syntax would have to be revisited, anyway ;-)

Liviu

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

Re: batch find external ipaddress

#10 Post by Squashman » 20 Jul 2012 18:19

Yep. My SonicWall router supports No-Ip. So I am good to go.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch find external ipaddress

#11 Post by foxidrive » 20 Jul 2012 20:55

This was mentioned in the past which I took note of:

Save this as ip.php on your web site and if it supports PHP then it will give you your IP address in a plain webpage.

<? print $_SERVER['REMOTE_ADDR']; ?>


You can then use the following code to get your IP address in a variable (untested as my webhost is down atm)
Replace server.domain.com with your webhost URL
You will need to install Wget for windows.

Code: Select all

@echo off
echo Finding your external IP address... please wait
cd /d %temp%.\
del "ip.txt" 2>nul
set "ip="
"c:\Program Files\WGET\WGET.EXE" -q -O ip.txt http://server.domain.com/ip.php
for /f %%a in (ip.txt) do if not defined ip set ip=%%a
echo Your external IP address is %IP%
echo.
pause

Billd59
Posts: 5
Joined: 02 Jul 2012 17:43

Re: batch find external ipaddress

#12 Post by Billd59 » 24 Jul 2012 09:49

Liviu wrote:
Billd59 wrote:I don't know VBscript yet. I am just learning Batch files. I don't have VB yet. So I would not know how to run It.
Just save it as a text file with extension .VBS and run it at the command line. Note that this is VBScript (which comes with Windows), not VB (which sells separately).

Don't know that what you are asking for is possible using just batch and the builtin commands. As suggested above, instead of VBS you could also use something like WGET (for example the one from http://gnuwin32.sourceforge.net/packages/wget.htm) or CURL (http://curl.haxx.se/download.html).

Here is a snippet using the latter. Make sure to mind the warning on the first line. To suppress the verbose CURL output and see just the IP echo line, run with 2>nul.

Code: Select all

:: do *NOT* run more often than once every 5 minutes
:: see http://www.whatismyip.com/faq/automation.asp

@echo off
setlocal disableDelayedExpansion

set "wip="
for /f "delims=" %%a in ('curl http://automation.whatismyip.com/n09230945.asp') do set "wip=%%a"

echo public IP = %wip%

Liviu


I will try this. Do I just copy the above and paste It to notepad with the.VBS at the end? I am trying to learn VBScript too. I just started learning.I am handicapp so It takes awhile for me to learn.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch find external ipaddress

#13 Post by foxidrive » 24 Jul 2012 10:02

Bill? Is that you?

And no, that is a batch file that you quoted and not VBscript. It uses a tool called Curl that you will have to download.



I have another solution which involves telnet and connecting to my router and getting the WAN IP address that way.
The drawback is that it too needs a third party tool, in this case to script the Telnet session, as well as a router/modem that supports telnet.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch find external ipaddress

#14 Post by foxidrive » 24 Jul 2012 10:53

I missed earlier posts in this thread.

Here's a simplification of the VBS solution wrapped in a batch file. It works here.

@echo off
>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo strHTML = objHTTP.ResponseText
>>"%temp%\ip.vbs" echo wscript.echo strHTML

for /f "tokens=7 delims=:<" %%a in ('cscript /nologo "%temp%\ip.vbs"') do set ip=%%a
echo %ip:~1%
pause

Post Reply