3

Is there any way to programmatically check if a connection is established to an afp server? I have tried pinging the server but that always results in a timeout.

So far the only workaround that I've gotten to work is:

#!/bin/bash
open "afp://website.com/afpDir/"
until [[ -d /Volumes/afpDir/ ]]; do
    echo 'waiting for connections'
    sleep 1
done

However, this only works if I specifically check for the afpDir directory while it is open in Finder. Is there a reliable way to to check if there is a connection to that afp server or perhaps even finding a reliable ip for the server?

  • 1
    What does the mount command return on your system - both when the AFP server is connected and disconnected? – Scot Oct 26 '17 at 21:50
  • @Scot mount gives me //username@website.com/homedir on /Volumes/homedir (afpfs, nodev, nosuid, quarantine, mounted by localuser) and it's gone when I unmount. – Micks Ketches Oct 27 '17 at 17:05
  • So, using mount and parsing the results is another possibility. – Scot Oct 27 '17 at 22:37
  • @Scot absolutely, it's a good idea. – Micks Ketches Oct 27 '17 at 22:47
1

To check if a connection to an AFP server - with the fixed listening port 548 - is established use:

netstat -naf inet | grep [.]548[\ ].*ESTAB*

or with quotes:

netstat -naf inet | grep '[.]548[\ ].*ESTAB*'

with -naf inet: no name resolution, all sockets and restrict to IPv4

and [.]548[\ ].*ESTAB* (grep for an) <IP-address>.548 ESTABLISHED. The [.] is required to exclude ports like 2548 or 10548. [\ ] excludes ports like 5480 or 5481. The .* concatenates everything (tab/spaces) between 548 and ESTAB*.

An example result looks like this:

tcp4       0      0  192.168.1.24.54340       192.168.1.2.548         ESTABLISHED

192.168.1.24 is the client's IP address and 192.168.1.2 the server's IP address then.

  • hmm I'm getting a no matches found for netstat -naf inet | grep [.]548[\ ].*ESTAB* even when the server is confirmed to be mounted. – Micks Ketches Oct 27 '17 at 17:12
  • I did howerver get a *.548 match. Do you mind briefly explaining the use of the star and brackets? I was under the assumption that brackets was just a regex OR match for single chars. – Micks Ketches Oct 27 '17 at 17:25
  • @MicksKetches The [.] is required to exclude ports like 2548 or 10548. [\ ] excludes ports like 5480 or 5481. The .* concatenates everything (tab/spaces) between 548 and ESTAB*. – klanomath Oct 27 '17 at 17:31
  • Update: I did get a desired outcome if I wrapped the statement in quotes like so: netstat -naf inet | grep '[.]548[\ ].*ESTAB*' I hope that was what you meant. – Micks Ketches Oct 27 '17 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .