How to use Screen Sharing if I don't know the remote Mac's IP address

I have the credentials for the Mac, but there is nobody in the room with the machine to check the public IP address.

enter image description here

If I know the public IP, I can connect to the mac. Unfortunately the ISP provides only dynamic IPs, so I guess it changed recently.

Is there any other way to gain access?


Ah! Yeah... your options at this point are limited. The ISP might help you as it's their DHCP that assigned the address; they'd be the only ones that would know that.

This has happened to me, and as my ISP is comcast, there is no help.

In the future :), what you need is "Dynamic DNS"; available free from several sources. I use FreeDNS. It's kind of funky, but seems to work reliably.

The other alternative (to dynamic DNS) is a "roll your own" approach that would require some script-writing on your part. If you're up for that, let me know & we'll go from there.


The screen sharing service uses TCP port 5900. If indeed you have arranged so that you can connect to it if you only know the IP, that port should appear as open from outside.

Now, if you know what dynamic IP address it had before it changed, you can check WHOIS database to see the whole IP range that address belongs in. Chances are good that the new IP would belong to the same range, though that will depend totally on how your ISP allocates the addresses.

Now, after that, you could scan that whole subnet using e.g. nmap, looking for open port 5900. But before you do that, check on the legality of port scanning in whatever country you are currently in. In some countries it is legal if done without criminal intent, in other countries port scanning may be always illegal.


On the remote computer, save this following AppleScript code in a new Script Editor.App document (Script Editor.App is located here... /Applications/Utilities/Script Editor.app) as a "Stay Open" application. Before saving this document as an application, make sure you enter your current external IP address and the email address in the first two lines of the code.

To summarize what the AppleScript code does is simply... Every 15 minutes it checks your current external IP address against the IP address you set in the first line of the code. If those two IP addresses are not the same, the script will go ahead and send a new email (to the email address you set in the second line of the code) with the new updated external IP address.

Then just like opening any other application in Finder, double-clicking your newly created app will run the new app.

property ipAddress : "123.456.7.89" -- Insert Current External IP Address (Set Only Once)

property toRecipient : "[email protected]" --Email To Get IP Has Changed Messages

on idle
    set theIP to do shell script "curl ifconfig.co"

    if theIP is not equal to ipAddress then
        tell application "Mail"
            set newEmail to (make new outgoing message)
            set content of newEmail to theIP
            set subject of newEmail to "IP Address Has Changed"
            tell newEmail
                make new to recipient with properties {address:toRecipient}
                send
            end tell
        end tell
        set ipAddress to theIP
    end if

    return (15 * minutes) -- Waits 15 min. to check IP again
end idle

Don't forget to add this new AppleScript application, in System Preferences, to the list of apps allowed to control your computer.


UPDATE

Although the above script seems to work, after several runs and minor changes to the code for testing purposes, I decided to create an alternate solution. Because of the new security settings in macOS Mojave and AppleScript's inability to reliably update and maintain its Global and Persistent property values across runs, I feel this alternate solution is much more reliable.

Step 1: On the remote computer, in Script Editor.app, save this AppleScript code as a .scpt file and name it "Get Current External IP.scpt"

Be sure you change the value of property toRecipient to your email address

property ipAddress : "1.1.1.1" -- Not Necessary To Set
property toRecipient : "[email protected]" --Email To Get IP Has Changed Messages

set emailIsRunning to application "Mail" is running -- Checks If Mail Is Running

try
    set lastLoggedIP to readFile()
    set theIP to do shell script "curl ifconfig.co"
    if theIP is not equal to lastLoggedIP then
        sendNewEmail()
        writeToTheFile()
    end if
on error errMsg number errNum
    if errNum is -43 then -- The Error If File For readFile() Doesn't Exist Yet
        set theIP to do shell script "curl ifconfig.co"
        if theIP is not equal to ipAddress then
            sendNewEmail()
            writeToTheFile() -- Creates The "/private/tmp/IP Logger.txt" File
        end if
    else
        activate
        display dialog "An error has occurred:" & return & errMsg & " " & errNum giving up after 10
    end if
end try

delay 20 -- Allows For Completion Of Any Incoming Or Outgoing Mail Messages
if not emailIsRunning then quit application "Mail" -- Quits Mail If Wasn't Already Running

to sendNewEmail() -- Sends New Email Whenever IP Changes
    set theIP to do shell script "curl ifconfig.co"
    tell application "Mail"
        set newEmail to (make new outgoing message)
        set content of newEmail to theIP
        set subject of newEmail to "IP Address Has Changed"
        tell newEmail
            make new to recipient with properties {address:toRecipient}
            send
        end tell
    end tell
    set ipAddress to theIP
end sendNewEmail

on readFile() -- Reads IP Logger.txt To Check For IP Changes 
    set theFile1 to alias "private:tmp:IP Logger.txt"
    set lastLoggedIP to last item of (read theFile1 as text using delimiter linefeed)
end readFile

on writeToTheFile() -- Logs Every IP Change To File
    set theIP to do shell script "curl ifconfig.co"
    set theFile to "/private/tmp/IP Logger.txt"
    set theText to theIP
    try
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    on error errMsg number errNum
        close access theFile
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    end try
end writeToTheFile

Step 2: On the remote computer, in Script Editor.app, save this following AppleScript code as a "stay open" application file. I named it "IP Changed Email Notify.app".

If you did not save "Get Current External IP.scpt" to your Documents folder in Step 1 from above, then you must set its location in property currentExternalIP in the following code.

property currentExternalIP : alias ((path to documents folder as text) & "Get Current External IP.scpt")

run script currentExternalIP

on idle
    run script currentExternalIP
    return (15 * minutes)
end idle

The first time you run the new stay open AppleScript app (IP Changed Email Notify.app) from outside of Script Editor.app, you will be presented with this following dialog...

enter image description here

Just click "OK" and since all of the work is actually being done by "Get Current External IP.scpt" there should be no more future dialogs or alerts to deal with.

Step 3: (OPTIONAL) On the remote computer, add this newly created AppleScript application (IP Changed Email Notify.app) to your "Login Items" in the Users And Groups System Preferences panel. Now every time the computer restarts, IP Changed Email Notify.app will run automatically and any time the external IP address of that computer changes, you will get an email notification.


If the person on the other Mac is signed in with their Apple ID, you can simply start a iMessage session, click on the Details button and click on the Screen Sharing icon to start a screen sharing session.

enter image description here

You can both ask them to share their screen or invite to share your screen. This approach works seemlessly without needing an explicit IP address.

You'll need someone to be present physically near the machine to approve the screen sharing request though.