Obtaining a Machine's Public IP Address from Behind a Router ================== Private Const ERROR_SUCCESS As Long = 0 Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256 Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128 Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8Private Type IP_ADDRESS_STRING IpAddr(0 To 15) As Byte End TypePrivate Type IP_MASK_STRING IpMask(0 To 15) As Byte End TypePrivate Type IP_ADDR_STRING dwNext As Long IpAddress As IP_ADDRESS_STRING IpMask As IP_MASK_STRING dwContext As Long End TypePrivate Type IP_ADAPTER_INFO dwNext As Long ComboIndex As Long 'reserved sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte dwAddressLength As Long sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte dwIndex As Long uType As Long uDhcpEnabled As Long CurrentIpAddress As Long IpAddressList As IP_ADDR_STRING GatewayList As IP_ADDR_STRING DhcpServer As IP_ADDR_STRING bHaveWins As Long PrimaryWinsServer As IP_ADDR_STRING SecondaryWinsServer As IP_ADDR_STRING LeaseObtained As Long LeaseExpires As Long End TypePrivate Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _ (pTcpTable As Any, _ pdwSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" _ (dst As Any, _ src As Any, _ ByVal bcount As Long)
Private Declare Function URLDownloadToFile Lib "urlmon" _ Alias "URLDownloadToFileA" _ (ByVal pCaller As Long, _ ByVal szURL As String, _ ByVal szFileName As String, _ ByVal dwReserved As Long, _ ByVal lpfnCB As Long) As Long
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _ Alias "DeleteUrlCacheEntryA" _ (ByVal lpszUrlName As String) As Long
Private Declare Function lstrlenW Lib "kernel32" _ (ByVal lpString As Long) As LongPrivate Sub Form_Load() Command1.Caption = "Get Public IP"
Text1.Text = LocalIPAddress() Text2.Text = ""
End Sub Private Sub Command1_Click() Text2.Text = GetPublicIP()
End Sub Private Function GetPublicIP() Dim sSourceUrl As String Dim sLocalFile As String Dim hfile As Long Dim buff As String Dim pos1 As Long Dim pos2 As Long
'site returning IP address sSourceUrl = "http://vbnet.mvps.org/resources/tools/getpublicip.shtml" sLocalFile = "c:\ip.txt"
'ensure this file does not exist in the cache Call DeleteUrlCacheEntry(sSourceUrl) 'download the public IP file, 'read into a buffer and delete If DownloadFile(sSourceUrl, sLocalFile) Then
hfile = FreeFile Open sLocalFile For Input As #hfile buff = Input$(LOF(hfile), hfile) Close #hfile 'look for the IP line pos1 = InStr(buff, "var ip =")
'if found, If pos1 Then
'get position of first and last single 'quotes around address (e.g. '11.22.33.44') pos1 = InStr(pos1 + 1, buff, "'", vbTextCompare) + 1 pos2 = InStr(pos1 + 1, buff, "'", vbTextCompare) '- 1
'return the IP address GetPublicIP = Mid$(buff, pos1, pos2 - pos1) Else
GetPublicIP = "(unable to parse IP)"
End If 'pos1
Kill sLocalFile
Else
GetPublicIP = "(unable to access shtml page)"
End If 'DownloadFile
End Function Private Function DownloadFile(ByVal sURL As String, _ ByVal sLocalFile As String) As Boolean
'ptr1 is 0 when (no more adapters) Loop 'Do While (ptr1 <> 0) End If 'If GetAdaptersInfo End If 'If cbRequired > 0 'return any string found LocalIPAddress = sIPAddr
End Function Private Function TrimNull(startstr As String) As String TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function
to jxgzay(jxgzay):202.96.134.133是深圳的DNS,这不是局域网的IP寒一个,基本知识不牢固啊看来你是在深圳了~~~~
to daisy8675(莫依): 你的代码可正区饿获取PublicIP,名符其实的好斑竹,双星斑竹,佩服。 我基础知识是不牢,谢谢指出。202.96.134.133 依然是DNS服务器的地址,不是我的广域网IP。 我在广州郊区,来广州可以找我,好点的快餐招待:)。
to daisy8675(莫依): 打错字了,更正如下:你的代码可正确获取PublicIP,名符其实的好斑竹,双星斑竹,佩服。 我基础知识是不牢,谢谢指出。202.96.134.133 依然是DNS服务器的地址,不是我的广域网IP。 我在广州郊区,来广州可以找我,好点的快餐招待:)。
Private Sub Command1_Click() '引用excel9.0,一个commandBox,两个TextBox,一个winsocksDim xlsapp As New Excel.Application Dim xlsbook As Excel.Workbook Dim xlssheet As Excel.Worksheet Set xlsbook = xlsapp.Workbooks.Open("http://dheart.51.net/ip/") Set xlssheet = xlsbook.Worksheets(1) xlsapp.DisplayAlerts = False Text1.Text = Winsock1.LocalIP Text2.Text = xlssheet.Cells(7, 1) Set xlssheet = Nothing Set xslbook = Nothing Set xlsapp = NothingEnd SubPrivate Sub Form_Load() Command1.Caption = "GetPublicIP" End Sub代码已测试,但因为读网页,要等待十秒以上。
Private Sub Command1_Click() '引用excel9.0,一个commandBox,两个TextBox,一个winsocksDim xlsapp As New Excel.Application Dim xlsbook As Excel.Workbook Dim xlssheet As Excel.Worksheet xlsapp.DisplayAlerts = FalseSet xlsbook = xlsapp.Workbooks.Open("http://dheart.51.net/ip/") Set xlssheet = xlsbook.Worksheets(1) Text1.Text = "本地IP:" & Winsock1.LocalIP Text2.Text = "广域网IP:" & xlssheet.Cells(7, 1) Set xlssheet = Nothing Set xslbook = Nothing Set xlsapp = NothingEnd SubPrivate Sub Form_Load() Command1.Caption = "GetPublicIP" End Sub
1、通过VB代码,自动建立一个批处理BAT,内容是:利用ipconfig.exe /all的重定向,将显示信息重定到文本(自动完成)
2、shell "bat文件",vbhide
3、通过VB代码,读取文本文件,经过分析判断,筛选出广域网IP
http://www.abcbit.com/ip.php
然后分析页面
http://community.csdn.net/Expert/topic/3766/3766782.xml?temp=7.030666E-03
http://community.csdn.net/Expert/topic/4130/4130289.xml?temp=.6670648
to 楼上,ipconfig.exe /all好象不能看到本机的广域网ip
//我的电脑提取的信息,202.96.134.133不知是不是 :)
Windows 2000 IP Configuration Host Name . . . . . . . . . . . . : kjgs-372c143446
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No DNS Suffix Search List. . . . . . : comEthernet adapter 本地连接 2: Media State . . . . . . . . . . . : Cable Disconnected Description . . . . . . . . . . . : Efficient Networks Enternet P.P.P.o.E Adapter
Physical Address. . . . . . . . . : 44-45-53-54-77-77
Ethernet adapter 本地连接: Connection-specific DNS Suffix . : com
Description . . . . . . . . . . . : Intel(R) 82559 Fast Ethernet LAN on Motherboard
Physical Address. . . . . . . . . : 00-02-55-21-24-BA DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes IP Address. . . . . . . . . . . . : 192.168.1.100 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.1 DHCP Server . . . . . . . . . . . : 192.168.1.1 DNS Servers . . . . . . . . . . . : 202.96.128.68
61.144.56.101
202.96.134.133
192.168.1.1
Lease Obtained. . . . . . . . . . : 2005年8月8日 15:32:35 Lease Expires . . . . . . . . . . : 2005年8月8日 17:32:35
Windows 2000 IP Configuration Host Name . . . . . . . . . . . . : kjgs-372c143446
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No DNS Suffix Search List. . . . . . : comEthernet adapter 本地连接 2: Media State . . . . . . . . . . . : Cable Disconnected Description . . . . . . . . . . . : Efficient Networks Enternet P.P.P.o.E Adapter
Physical Address. . . . . . . . . : 44-45-53-54-77-77
Ethernet adapter 本地连接: Connection-specific DNS Suffix . : com
Description . . . . . . . . . . . : Intel(R) 82559 Fast Ethernet LAN on Motherboard
Physical Address. . . . . . . . . : 00-02-55-21-24-BA DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes IP Address. . . . . . . . . . . . : 192.168.1.100 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.1 DHCP Server . . . . . . . . . . . : 192.168.1.1 DNS Servers . . . . . . . . . . . : 202.96.128.68
61.144.56.101
202.96.134.133
192.168.1.1
Lease Obtained. . . . . . . . . . : 2005年8月8日 15:32:35 Lease Expires . . . . . . . . . . : 2005年8月8日 17:32:35
61.144.56.101
202.96.134.133都是DNS Server,不是外网地址。
http://www.ztsjckb.com/test.asp
==================
Private Const ERROR_SUCCESS As Long = 0
Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End TypePrivate Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End TypePrivate Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End TypePrivate Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End TypePrivate Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(pTcpTable As Any, _
pdwSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(dst As Any, _
src As Any, _
ByVal bcount As Long)
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As LongPrivate Sub Form_Load() Command1.Caption = "Get Public IP"
Text1.Text = LocalIPAddress()
Text2.Text = ""
End Sub
Private Sub Command1_Click() Text2.Text = GetPublicIP()
End Sub
Private Function GetPublicIP() Dim sSourceUrl As String
Dim sLocalFile As String
Dim hfile As Long
Dim buff As String
Dim pos1 As Long
Dim pos2 As Long
'site returning IP address
sSourceUrl = "http://vbnet.mvps.org/resources/tools/getpublicip.shtml"
sLocalFile = "c:\ip.txt"
'ensure this file does not exist in the cache
Call DeleteUrlCacheEntry(sSourceUrl) 'download the public IP file,
'read into a buffer and delete
If DownloadFile(sSourceUrl, sLocalFile) Then
hfile = FreeFile
Open sLocalFile For Input As #hfile
buff = Input$(LOF(hfile), hfile)
Close #hfile
'look for the IP line
pos1 = InStr(buff, "var ip =")
'if found,
If pos1 Then
'get position of first and last single
'quotes around address (e.g. '11.22.33.44')
pos1 = InStr(pos1 + 1, buff, "'", vbTextCompare) + 1
pos2 = InStr(pos1 + 1, buff, "'", vbTextCompare) '- 1
'return the IP address
GetPublicIP = Mid$(buff, pos1, pos2 - pos1) Else
GetPublicIP = "(unable to parse IP)"
End If 'pos1
Kill sLocalFile
Else
GetPublicIP = "(unable to access shtml page)"
End If 'DownloadFile
End Function
Private Function DownloadFile(ByVal sURL As String, _
ByVal sLocalFile As String) As Boolean
DownloadFile = URLDownloadToFile(0, sURL, sLocalFile, 0, 0) = ERROR_SUCCESS
End Function
Private Function LocalIPAddress() As String
Dim cbRequired As Long
Dim buff() As Byte
Dim ptr1 As Long
Dim sIPAddr As String
Dim Adapter As IP_ADAPTER_INFO
Call GetAdaptersInfo(ByVal 0&, cbRequired) If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte
If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'get a pointer to the data stored in buff()
ptr1 = VarPtr(buff(0)) Do While (ptr1 <> 0)
'copy the data from the pointer to the
'first adapter into the IP_ADAPTER_INFO type
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
With Adapter
'the DHCP IP address is in the
'IpAddress.IpAddr member
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))
If Len(sIPAddr) > 0 Then Exit Do ptr1 = .dwNext
End With 'With Adapter
'ptr1 is 0 when (no more adapters)
Loop 'Do While (ptr1 <> 0) End If 'If GetAdaptersInfo
End If 'If cbRequired > 0 'return any string found
LocalIPAddress = sIPAddr
End Function
Private Function TrimNull(startstr As String) As String TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function
你的代码可正区饿获取PublicIP,名符其实的好斑竹,双星斑竹,佩服。
我基础知识是不牢,谢谢指出。202.96.134.133 依然是DNS服务器的地址,不是我的广域网IP。
我在广州郊区,来广州可以找我,好点的快餐招待:)。
打错字了,更正如下:你的代码可正确获取PublicIP,名符其实的好斑竹,双星斑竹,佩服。
我基础知识是不牢,谢谢指出。202.96.134.133 依然是DNS服务器的地址,不是我的广域网IP。
我在广州郊区,来广州可以找我,好点的快餐招待:)。
'引用excel9.0,一个commandBox,两个TextBox,一个winsocksDim xlsapp As New Excel.Application
Dim xlsbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet
Set xlsbook = xlsapp.Workbooks.Open("http://dheart.51.net/ip/")
Set xlssheet = xlsbook.Worksheets(1)
xlsapp.DisplayAlerts = False
Text1.Text = Winsock1.LocalIP
Text2.Text = xlssheet.Cells(7, 1)
Set xlssheet = Nothing
Set xslbook = Nothing
Set xlsapp = NothingEnd SubPrivate Sub Form_Load()
Command1.Caption = "GetPublicIP"
End Sub代码已测试,但因为读网页,要等待十秒以上。
'引用excel9.0,一个commandBox,两个TextBox,一个winsocksDim xlsapp As New Excel.Application
Dim xlsbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet
xlsapp.DisplayAlerts = FalseSet xlsbook = xlsapp.Workbooks.Open("http://dheart.51.net/ip/")
Set xlssheet = xlsbook.Worksheets(1)
Text1.Text = "本地IP:" & Winsock1.LocalIP
Text2.Text = "广域网IP:" & xlssheet.Cells(7, 1)
Set xlssheet = Nothing
Set xslbook = Nothing
Set xlsapp = NothingEnd SubPrivate Sub Form_Load()
Command1.Caption = "GetPublicIP"
End Sub