OVH Cloud OVH Cloud

Récupérer Ip internet d'un PC en réseau

2 réponses
Avatar
Help Me
Bonjour à tous

Malgres toutes mes recherches sur ce forum, je n'ai pas trouvé un procédé
vba permettant de récupérer l'ip internet d'une machine faisant partie d'un
reseau.
En effet, je souhaite récupérer l'ip du style 81.66.229.131 et non le
192.168.0.1

Auriez-vous une solution pour cela ?

Merci à vous tous

Cordialement
Michel

2 réponses

Avatar
Jacques93
Bonjour Help Me,

Un exemple de code inspiré de :

http://vbnet.mvps.org/index.html?code/internet/getpublicip.htm


D'autre site comme http://www.whatismyip.org/ renvoie l'IP publique

'-----------------------------------------------------------------
Option Explicit

Private Const ERROR_SUCCESS As Long = 0

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 Sub CommandButton1_Click()
MsgBox "IP Publique : " & 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
Kill sLocalFile
Else
GetPublicIP = "(unable to access shtml page)"
End If
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


Bonjour à tous

Malgres toutes mes recherches sur ce forum, je n'ai pas trouvé un procédé
vba permettant de récupérer l'ip internet d'une machine faisant partie d'un
reseau.
En effet, je souhaite récupérer l'ip du style 81.66.229.131 et non le
192.168.0.1

Auriez-vous une solution pour cela ?

Merci à vous tous

Cordialement
Michel





--
Cordialement,

Jacques.

Avatar
Help Me
GENIALLLLLLLL


Merci Beaucoup

Michel


"Jacques93" a écrit dans le message de news:

Bonjour Help Me,

Un exemple de code inspiré de :

http://vbnet.mvps.org/index.html?code/internet/getpublicip.htm


D'autre site comme http://www.whatismyip.org/ renvoie l'IP publique

'-----------------------------------------------------------------
Option Explicit

Private Const ERROR_SUCCESS As Long = 0

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 Sub CommandButton1_Click()
MsgBox "IP Publique : " & 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
Kill sLocalFile
Else
GetPublicIP = "(unable to access shtml page)"
End If
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


Bonjour à tous

Malgres toutes mes recherches sur ce forum, je n'ai pas trouvé un procédé
vba permettant de récupérer l'ip internet d'une machine faisant partie
d'un reseau.
En effet, je souhaite récupérer l'ip du style 81.66.229.131 et non le
192.168.0.1

Auriez-vous une solution pour cela ?

Merci à vous tous

Cordialement
Michel



--
Cordialement,

Jacques.