Check conncetion to AFP server through bash

afpbashcommand lineNetworkosx-server

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?

Best Answer

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.