get "last seen" timestamp via cli?

I’ve found this old topic that explains how to parse it from the rpc output used by the browser UI:

Since it is 4 years old I though I’ll try asking if there’s some way to get this via CLI nowadays.

I’ve tried this:

syncthing cli show connections \
| jq -r '.connections | to_entries[] | "\(.key);\(.value.at)"' \
| while IFS=';' read -r id timestamp; do
    name=$(syncthing cli config devices "$id" name get)
    echo "$timestamp;$name"
done \
| sort -r

But that gives me “0001-01-01T00:00:00Z” for devices that are not currently connected, and only the current timestamp for devices that are. Not equivalent and as useful as the “last seen” timestamp available in the Web-UI.

EDITED TO ADD: Tested. I was wrong. That REST API endpoint does not appear to be exposed in the CLI.

I think you’re looking for this: GET /rest/stats/device — Syncthing documentation .

Combining that with syncthing cli show should work.

thank you. For now I ended up with this:

curl -s -H "X-API-Key: ***" http://localhost:8384/rest/stats/device \
| jq -r 'to_entries[] | "\(.value.lastSeen);\(.key)"' \
| while IFS=';' read -r timestamp id; do
    name=$(syncthing cli config devices "$id" name get)
    echo "$timestamp;$name"
done \
| sort

What I find kind of strange is that the peer I’m quering this from always lists itself too with the last seen timestamp of “1970-01-01T01:00:00+01:00”..

I’ve now added a workaround to filter away the strange output of the own peer lastSeen value:

myid=$(syncthing -device-id)
curl -s -H "X-API-Key: ***" http://localhost:8384/rest/stats/device \
| jq -r 'to_entries[] | "\(.value.lastSeen);\(.key)"' \
| while IFS=';' read -r timestamp id; do
    if [ "$id" = "$myid" ]; then
        continue
    fi
    name=$(syncthing cli config devices "$id" name get)
    echo "$timestamp;$name"
done \
| sort

Would love to get feedback for my work, maybe even suggestions for how to make it simpler or faster.

1 Like

yes, that’s what I meant. Why does it list the local peer with last seen timestamp = “1970-01-01T01:00:00+01:00”? Looks like a bug to me.

I deleted my dumb response; I didn’t read the part where you found it.

I think it’s in there because it has to have something, and that’s the beginning of the Unix epoch.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.