中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-29 01:51
47,813 topics / 349,918 posts / today 0 new / 48,278 members
DOS批处理 & 脚本技术(批处理室) » Batch Script for Quickly Obtaining Public Network IP
Printable Version  2,842 / 19
Floor1 tvzml Posted 2008-03-30 22:39
初级用户 Posts 67 Credits 157
Due to personal needs to obtain the external network IP, the original code method for obtaining from www.ip138.com found online has become invalid. The method provided by the forum takes too long to obtain, which is not very ideal for practical applications. The following is what I wrote when I first learned VBS. It is very fast, but the script is not simplified. I hope experts can help optimize the code.

Working principle of the code:
Download http://www.ip138.com/ip2city.asp, take the 13 characters to the right of "Your IP address is: " and save it to the IP.TXT in the Windows directory. The display method is what I learned from these two lines. Use <to read the content of IP.TXT and display it. Don't laugh at me for being inexperienced. One day I will surpass you. Finally, get %n% which is the value of the external network IP address. This is of great use to me.

As for whether it is original, I think there is no so-called originality. We are not born knowing VBS, but we can call code to work for us, as long as it is practical and effective.

@echo off
echo On Error Resume next >checkip.vbs
echo Url="http://www.ip138.com/ip2city.asp" >>checkip.vbs
echo Set NP = Createobject("Microsoft.XMLHTTP") >>checkip.vbs
echo NP.Open "GET", url, False >>checkip.vbs
echo NP.Send >>checkip.vbs
echo Data=NP.responsebody >>checkip.vbs
echo Set NP = Nothing >>checkip.vbs
echo Data = bytes2BSTR(Data) >>checkip.vbs
echo Here = InstrRev(Data, "您的IP地址是:", -1,0) >>checkip.vbs
echo Data = Mid(Data,Here+9,13) >>checkip.vbs
echo Hers = InstrRev(data, "<",-1,0) >>checkip.vbs
echo Set ICEhack = CreateObject("InternetExplorer.Application") >>checkip.vbs
echo ICEhack.Navigate("about:blank") >>checkip.vbs
echo If Hers ^<^> 0 Then >>checkip.vbs
echo ICEhack.document.parentwindow.clipboardData.SetData "text", Mid(Data,1,Hers-1) >>checkip.vbs
echo Set WshSHell = WScript.CreateObject("WScript.Shell") >>checkip.vbs
echo Set FSO = CreateObject("Scripting.FileSystemObject") >>checkip.vbs
echo Set SCF = FSO.OpenTextFile(FSO.BuildPath(FSO.GetSpecialFolder(0),"ip.txt"),2,True) >>checkip.vbs
echo SCF.Write(Data) >>checkip.vbs
echo SCF.Close >>checkip.vbs
echo Else >>checkip.vbs
echo ICEhack.document.parentwindow.clipboardData.SetData "text", Data >>checkip.vbs
echo Set WshSHell = WScript.CreateObject("WScript.Shell") >>checkip.vbs
echo Set FSO = CreateObject("Scripting.FileSystemObject") >>checkip.vbs
echo Set SCF = FSO.OpenTextFile(FSO.BuildPath(FSO.GetSpecialFolder(0),"ip.txt"),2,True) >>checkip.vbs
echo SCF.Write(Data) >>checkip.vbs
echo SCF.Close >>checkip.vbs
echo End If >>checkip.vbs
echo ICEhack.Quit >>checkip.vbs
echo fso.DeleteFile(WScript.ScriptName) >>checkip.vbs
echo Function bytes2BSTR(vIn) >>checkip.vbs
echo strReturn = "" >>checkip.vbs
echo For i = 1 To LenB(vIn) >>checkip.vbs
echo ThisCharCode = AscB(MidB(vIn,i,1)) >>checkip.vbs
echo If ThisCharCode ^< ^&H80 Then >>checkip.vbs
echo strReturn = strReturn ^& Chr(ThisCharCode) >>checkip.vbs
echo Else >>checkip.vbs
echo NextCharCode = AscB(MidB(vIn,i+1,1)) >>checkip.vbs
echo strReturn = strReturn ^& Chr(CLng(ThisCharCode) * ^&H100 + CInt(NextCharCode)) >>checkip.vbs
echo i = i + 1 >>checkip.vbs
echo End If >>checkip.vbs
echo Next >>checkip.vbs
echo bytes2BSTR = strReturn >>checkip.vbs
echo End Function >>checkip.vbs
checkip.vbs
set/p n=<%windir%\ip.txt&cls
echo External network IP: %n%
pause>nul

[ Last edited by tvzml on 2008-3-30 at 10:57 PM ]
Floor2 tvzml Posted 2008-03-30 22:41
初级用户 Posts 67 Credits 157
On Error Resume next
Url="http://www.ip138.com/ip2city.asp"
Set NP = Createobject("Microsoft.XMLHTTP")
NP.Open "GET", url, False
NP.Send
Data=NP.responsebody
Set NP = Nothing
Data = bytes2BSTR(Data)
Here = InstrRev(Data, "您的IP地址是:", -1,0)
Data = Mid(Data,Here+9,13)
Hers = InstrRev(data, "<",-1,0)
Set ICEhack = CreateObject("InternetExplorer.Application")
ICEhack.Navigate("about:blank")
If Hers <> 0 Then
ICEhack.document.parentwindow.clipboardData.SetData "text", Mid(Data,1,Hers-1)
Set WshSHell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SCF = FSO.OpenTextFile(FSO.BuildPath(FSO.GetSpecialFolder(0),"ip.txt"),2,True)
SCF.Write(Data)
SCF.Close
Else
ICEhack.document.parentwindow.clipboardData.SetData "text", Data
Set WshSHell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SCF = FSO.OpenTextFile(FSO.BuildPath(FSO.GetSpecialFolder(0),"ip.txt"),2,True)
SCF.Write(Data)
SCF.Close
End If
ICEhack.Quit
fso.DeleteFile(WScript.ScriptName)
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
Floor3 electronixtar Posted 2008-03-31 03:13
铂金会员 Posts 2,672 Credits 7,493
The code is really long~~
Floor4 zh159 Posted 2008-03-31 13:25
金牌会员 Posts 1,467 Credits 3,687
Is it that long?
Floor5 tvzml Posted 2008-03-31 14:43
初级用户 Posts 67 Credits 157
Originally posted by zh159 at 2008-3-31 01:25 PM:
Is it that long?



Test succeeded, thank you, learned.
Floor6 topmcs Posted 2008-04-02 12:35
初级用户 Posts 16 Credits 25 From 湖南
Is there any practical use? Still hope to be pointed out
Floor7 tvzml Posted 2008-04-02 14:34
初级用户 Posts 67 Credits 157
The obtained external network IP address can be used in other configuration files.
Floor8 1073 Posted 2008-04-02 22:09
初级用户 Posts 12 Credits 29
There are indeed many experts here
Floor9 xiaoweivip Posted 2008-04-03 12:23
贫困用户 Posts 1 Credits -4
Floor10 slore Posted 2008-04-03 20:26
铂金会员 Posts 2,478 Credits 5,212
Set objrcl = CreateObject("rcbdyctl.Setting")
MsgBox objrcl.GetIPAddress
Set objrcl = Nothing

I don't know how this speed is. Hehe~
Floor11 plp626 Posted 2008-04-03 22:00
银牌会员 Posts 1,020 Credits 2,278
When can I be so proficient in using VBS?
Floor12 lyaa Posted 2008-04-13 22:01
初级用户 Posts 17 Credits 40
How to automatically upload the obtained address to the FTP space in the form of a file
Floor13 zh159 Posted 2008-04-13 22:54
金牌会员 Posts 1,467 Credits 3,687
Originally posted by slore at 2008-4-3 20:26:
Set objrcl = CreateObject("rcbdyctl.Setting")
MsgBox objrcl.GetIPAddress
Set objrcl = Nothing

How about the speed here?~

Dude, please read the question carefully
Floor14 tvzml Posted 2008-04-13 22:56
初级用户 Posts 67 Credits 157
Originally posted by lyaa at 2008-4-13 10:01 PM:
How to automatically upload the obtained address to an FTP space as a file.

You can use FTP commands to upload ip.txt.

@echo open 192.168.0.252>ftpcmd
@echo user user pwd>>ftpcmd
@echo prompt>>ftpcmd
@echo binary>>ftpcmd
@echo put ip.txt>>ftpcmd
@echo bye>>ftpcmd
@ftp -n<ftpcmd
Floor15 slore Posted 2008-04-14 13:03
铂金会员 Posts 2,478 Credits 5,212
Originally posted by zh159 at 2008-4-13 22:54:

Dude, please read the question clearly



I see it clearly...
It is suggested that you test...
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023