1

I need to get the active DISPLAY for any logged in user, so if they enter through SSH, they can query it.

Under Ubuntu 12.04, I used a script that, using the consolekit dbus interface, iterated through the active sessions, matching the user UID. A bit complicated, but that would give me the Display that I needed.

This is the code I used:

function obtener_display(){
        _UID=$1
        SESIONES_RAW=$(dbus-send --system --dest=org.freedesktop.ConsoleKit --print-reply /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.GetSessions)
        SESIONES=$(echo "$SESIONES_RAW" | grep "object path" | sed -r 's/^.*"(.*)".*$/\1/')
        for SESION in $SESIONES ; do
                USUARIOS_RAW=$(dbus-send --system --dest=org.freedesktop.ConsoleKit --print-reply $SESION org.freedesktop.ConsoleKit.Session.GetUnixUser)
                USUARIOS=$(echo "$USUARIOS_RAW" | grep "uint32" | sed -r 's/^.*uint32 (.*)$/\1/')
                for USUARIO in $USUARIOS ; do
                        if [ $_UID -eq $USUARIO ] ; then
                                X11DISPLAY_RAW=$(dbus-send --system --dest=org.freedesktop.ConsoleKit --print-reply $SESION org.freedesktop.ConsoleKit.Session.GetX11Display)
                                X11DISPLAY=$(echo "$X11DISPLAY_RAW" | grep "string" | sed -r 's/^.*string "(.*)"$/\1/')
                                if [ ! -z "$X11DISPLAY" ] ; then
                                        DISPLAY_VALIDO="$X11DISPLAY"
                                fi
                        fi
                done
        done

        if [ ! -z "$DISPLAY_VALIDO" ] ; then
                echo "$DISPLAY_VALIDO"
        else
                echo "FALSE"
        fi
}

And it worked like a charm! Well, kind of. At least it seemed to work properly, never heard anything bad about it. Anyways, it doesn't work under 14.04. The first method I used to get the ConsoleKit sessions returns an empty array. And every method from Manager also return empty arrays.

Is there any way I can fix this? Fix this method should do it, but any other way to get the active DISPLAY for any logged in user should also work.

Zanna
  • 72,312

1 Answers1

1

The command who gives me information about current logged in users, and their VTs and displays right away. All I need to do is find the user I want and parse the output.

who | grep $USER | grep -v tty | grep -v pts/ | cut -d' ' -f2

This will output :0, that's what I needed.

Zanna
  • 72,312