Monday, July 26, 2021

#1 2021-07-15 08:16:17 am

Scripted
Member
Registered: 2021-07-12
Posts: 9

Is Network Computer ON?

I run Applescripts within Microsoft Word that transfer documents  between my Mac Pro 2019 and my wife's iMac 2019 over LAN.  But when the iMac is not on, the script hangs up Word for about a minute before it generates an error message.

Please provide me with a script to check to see if her iMac is on before attempting to send/receive files.  All help is appreciated!

Offline

 

#2 2021-07-16 08:27:00 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1954

Re: Is Network Computer ON?

Interesting question, but I've never tried testing remote access on a real-world remote computer. Therefore, the following has not been tested by me. This is just my guess. I myself will be interested to know if this works:

Applescript:


try
   set remoteComputerIsOn to exists machine "eppc://192.163.1.2.3"
on error number -1728
   set remoteComputerIsOn to false
end try


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-07-16 10:17:08 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Is Network Computer ON?

To check if a server or remote computer is active most people use ping.

Here I ping the localhost 127.0.0.1, the parameter is count of 5

Applescript:

do shell script "ping -c 5 127.0.0.1"

If you ping a remote IP of machine that is off...

You will get something like this:

5 packets transmitted, 0 packets received, 100.0% packet loss


if you are the expert, who will you call if its not your imagination.

Offline

 

#4 2021-07-16 10:48:04 am

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 133

Re: Is Network Computer ON?

An alternative shell script would be to use the 'networksetup' command line tool, to get info about your ethernet connection.
Like this.

Applescript:


set ethernetInfo to do shell script "networksetup -getinfo ethernet"

Which will give you a list of info about the ethernet ports connection.
If their is an ethernet connected system, then you will get a different info list, of which there should be an 'IP address', which you can retrieve like this.

Applescript:


set ethernetIPAddress to do shell script "networksetup -getinfo ethernet | grep 'IP address' | awk -F ':' '{print $NF}'"

Not tested, as I have no other system available at present to connect to the ethernet port.

Regards Mark

Last edited by Mark FX (2021-07-16 10:52:24 am)

Offline

 

#5 2021-07-16 11:56:44 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 75

Re: Is Network Computer ON?

How do you connect to your wife's computer? Does it show up in the sidebar when it's connected (under one of the sharing types, e.g. bonjour, connected servers)? There should be some info that would help determine a simple and reliable method of detection, like a hostname or IP address.

Offline

 

#6 2021-07-16 12:25:32 pm

Scripted
Member
Registered: 2021-07-12
Posts: 9

Re: Is Network Computer ON?

Thank you all for your help.  I am in the middle of something.  I will try all and report back. 

I connect my wife's computer using ethernet and it does show up in Finder.  But I have to open finder each time, scroll down to the Network section to see if she's logged in. 

But I do this a few times a day and it has become annoying.  It would be nice for my Applescript/VB macros to not try the operation if her system is OFF.

Offline

 

#7 2021-07-17 11:42:20 am

Scripted
Member
Registered: 2021-07-12
Posts: 9

Re: Is Network Computer ON?

Guys, thanks again for taking the time to provide solutions.  Here's what I found:

KniazidisR's solution returned "Can't get machine "eppc..." error

Mark FX's solution returned "ethernet is not a recognized network service..." error

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Fredrik71 solution which was the simplest worked:

set remoteComputerIsOn to do shell script "ping -c 1 192.168.1.71"

Returns:

"PING 192.168.1.71 (192.168.1.71): 56 data bytes
64 bytes from 192.168.1.71: icmp_seq=0 ttl=64 time=0.423 ms

--- 192.168.1.71 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.423/0.423/0.423/0.000 ms"

As you can see I modified it to ping only once.  If remoteComputerIsOn contains "0 packets received" it means my wife's computer is OFF.

Thank you again everyone!

Offline

 

#8 2021-07-17 12:10:09 pm

Scripted
Member
Registered: 2021-07-12
Posts: 9

Re: Is Network Computer ON?

I jumped the gun!  Pinging works in the script editor but not in word. Since it takes about 10-15 seconds for Applescript to return the ping results when the remote system is OFF, I get an error message in Word!

Can I change the routine so the script waits 3" for the ping results then aborts the shell command if nothing returned yet?

Offline

 

#9 2021-07-17 03:46:01 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Is Network Computer ON?

Sure...

Something like this will stop your script if your wife computer is off

Applescript:

try
   do shell script "ping -c 1 -t 2 127.0.0.2"
on error the error_message
   set the error_text to "Error: " & ". " & the error_message
   display dialog the error_text buttons {"OK"} default button 1 giving up after 3
   return the error_text
end try

-- This command will not be executed if do shell script return error
display dialog "hello world"


if you are the expert, who will you call if its not your imagination.

Offline

 

#10 2021-07-18 09:40:13 am

Scripted
Member
Registered: 2021-07-12
Posts: 9

Re: Is Network Computer ON?

Thank you Fredrik!  The addition of the error trapping code did the trick for me. I didn't even need your dialog box.  Here's the final Applescript which is inside a file called "networking.scpt":

on IsRemoteComputerON(return_value)
    try
        set return_value to do shell script "ping -c 1 127.0.0.2"
    on error the error_message
        set the return_value to "Error: " & ". " & the error_message
        return the return_value
    end try
end IsRemoteComputerON

Here's the calling VB script function in Microsoft Word:

Function IsRemoteComputerON() As Boolean
    Dim return_value As String
    return_value = AppleScriptTask("Networking.scpt", "IsRemoteComputerON", return_value)
    IsRemoteComputerON = InStr(return_value, "0 packets received") <> 0
End Function

According to Microsoft rules Networking.scpt must reside in the following folder:
~/Library/Application Scripts/com.microsoft.word

IsRemoteComputerON() returns True if my wife's computer is ON.  It takes 11 seconds on my system to get a response for the shell operation if her computer is OFF.  It takes about a second to get a response if her system is ON.

Thank you again for your help Fredrik!

Offline

 

#11 2021-07-18 11:49:05 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Is Network Computer ON?

Great it working for you, I guess you did change 127.0.0.2 to the IP of your wife computer wink
I only used that to get a error... so my approach work the way you want.

Maybe if you could run shell script in words it could maybe be faster for you.
But I also include the parameters -t 2 if you look at man ping you could see what that parameter
does. And that will I guess from your 11 second to become less...

-t timeout  Specify a timeout, in seconds, before ping exits regardless of how many packets have been received.

It means if you set it to 2 or 3 it will give up after 2 or 3 seconds.
And in return your responds will be faster.


if you are the expert, who will you call if its not your imagination.

Offline

 

#12 2021-07-19 11:59:10 am

Scripted
Member
Registered: 2021-07-12
Posts: 9

Re: Is Network Computer ON?

Fredrik, you're the man!  You actually answered my previous question about the timeout!  The addition of your timeout switch worked like a charm!  The Word VBScript returns and error message after about 2.5" when the remote computer is OFF.

Thanks again for everything!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)