ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Get IP address for a remote computer based on its hostname (https://www.excelbanter.com/excel-programming/353281-get-ip-address-remote-computer-based-its-hostname.html)

stabilo

Get IP address for a remote computer based on its hostname
 
I'd like to get, with VBA, the IP address(es) if a computer based on its
hostname :

eg. Cell "a1" = PCXXX
Cell "a2" = XX.XX.XX.XX

I have seen some example that put the result of a ping into a text file,
then pars this text file and put in cells the ip address into a cell.

Is there another way ? I'm using Excel 2003

sebastienm

Get IP address for a remote computer based on its hostname
 
Hi
try:
'----------------------------------------
Sub test()
Dim v As Variant
Dim Filename As String, f As String
Dim ip As String

Filename = "E:\iptemp"
'Create command file
f = CreateCommandFile(Filename, "www.yahoo.com")
'run the command in silent mode
Shell f, vbHide
'get info from output file
v = GetIpInfo(Filename & ".txt")
MsgBox v

'add code here to delete files
End Sub

'Creates a .bat file containing ping. Output into file FileName & ".txt"
Function CreateCommandFile(Filename As String, ip As String)
Dim n As Long, File As String, s As String

File = Filename & ".bat"
s = "ping " & ip & " " & Filename & ".txt"
n = FreeFile()

Open File For Output Access Write As #n
Print #n, s
Close #n

CreateCommandFile = File
End Function

'Get ip info
' need to add code in case of no reply during the ping
Function GetIpInfo(Filename As String)
Dim n As Long, s As String

n = FreeFile()
Open Filename For Input Access Read As #n
Line Input #n, s
Line Input #n, s
Line Input #n, s 'data on third row
Close #n

'extract info within the 2 brackets [ ] of text file
n = Strings.InStr(s, "[")
s = Strings.Right(s, Len(s) - n)
n = Strings.InStr(s, "]")
s = Strings.Left(s, n - 1)

GetIpInfo = s
End Function
'---------------------------------------------------------
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"stabilo" wrote:

I'd like to get, with VBA, the IP address(es) if a computer based on its
hostname :

eg. Cell "a1" = PCXXX
Cell "a2" = XX.XX.XX.XX

I have seen some example that put the result of a ping into a text file,
then pars this text file and put in cells the ip address into a cell.

Is there another way ? I'm using Excel 2003



All times are GMT +1. The time now is 10:53 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com