Skip to content
Home

How to find Hostname and open ports for an IP-Address in MacOS

Sample files to download

VBA code in MacOS

Sub Ping()
'clear the status column
Range("B2:B1000").Clear
Dim hostIp As String
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
hostIp = ActiveSheet.Cells(i, 1).Value
If Not hostIp = "" Then
Dim objShell, returnCode
'VBA Code for Excel in Windows OS
' Set objShell = CreateObject("wscript.shell")
' returnCode = objShell.Run("ping -n 1 -w 1000 " & hostIp, 0, True)
'VBA Code for Excel in Mac OS - tested in MacOS Version 10.15
' Run an AppleScript with VB
' https://docs.microsoft.com/en-us/office/vba/office-mac/applescripttask
returnCode = AppleScriptTask("ping.applescript", "ping", hostIp)
If returnCode = "OK" Then
ActiveSheet.Cells(i, 2).Value = "Online"
ActiveSheet.Cells(i, 2).Font.Color = vbGreen
ActiveSheet.Cells(i, 3).Value = Now
Else
ActiveSheet.Cells(i, 2).Value = "Offline"
ActiveSheet.Cells(i, 2).Font.Color = vbRed
End If
End If
Next
End Sub

Supporting applescript

-- Function to get hostname from IP address using nslookup
on getHostNameUsingNsLookup(ipAddress)
try
-- Use the "do shell script" command to run a shell command
set command to "nslookup " & ipAddress
set nslookupOutput to do shell script command
-- Use regex to extract the hostname
set regexPattern to "name\\s*=\\s*(\\S+)"
set hostName to do shell script "echo " & quoted form of nslookupOutput & " | grep -o -E \"" & regexPattern & "\" | awk '{print $NF}'"
if hostName is not equal to "" then
return hostName
else
return "NOT OK"
end if
on error errMsg
return "Error: " & errMsg
end try
end getHostNameUsingNsLookup
-- Example usage
-- set ipAddress to "192.168.0.144" -- Replace with the IP address you want to look up
-- set resultMessage to getHostNameUsingNsLookup(ipAddress)
-- display dialog resultMessage
-- Function to get hostname from IP address using dig
on getHostNameUsingDig(ipAddress)
try
-- Use the "do shell script" command to run a shell command
set command to "dig +short -x " & ipAddress
set digOutput to do shell script command
-- Trim leading and trailing whitespace
set hostName to do shell script "echo " & quoted form of digOutput & " | xargs"
if hostName is not equal to "" then
return hostName
else
return "NOT OK"
end if
on error errMsg
return "Error: " & errMsg
end try
end getHostNameUsingDig
-- Example usage
-- set ipAddress to "192.168.0.144" -- Replace with the IP address you want to look up
-- set resultMessage to getHostNameUsingDig(ipAddress)
-- display dialog resultMessage