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
  • 69,223
  • 56
  • 216
  • 327
Jorge Suárez de Lis
  • 2,826
  • 3
  • 21
  • 34
  • It does indeed returning an empty array. BTW what does `env` print out at the ssh-console? Doesn't include the active display information? – Khurshid Alam Jan 07 '15 at 07:20
  • One other thing you can do is coping display information to a file at boot. For that put `env | grep DISPLAY > $HOME/.active_display` at the end of the `.bashrc` file of the remote machine you want to ssh into. Next time the remote user boots Display information will be available in `.active_display` file. – Khurshid Alam Jan 07 '15 at 07:26
  • No, env doesn't give me anything. I ended up with a much simpler solution. I'll post it as an answer. – Jorge Suárez de Lis Jan 09 '15 at 08:34

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
  • 69,223
  • 56
  • 216
  • 327
Jorge Suárez de Lis
  • 2,826
  • 3
  • 21
  • 34
  • your command gives empty result. And `who` doesn't print the word `tty` on 14.04. However this works for me: `who | grep $USER | grep -v pts/ | awk -F' ' '{print $2}'` – Khurshid Alam Jan 09 '15 at 13:04
  • regarding the above comment, the exact output depends on the terminal emulator if I'm not wrong. (in MATE terminal... `who | sed 's/.*(\(.*\))$/\1/'` perhaps... Anyway, the `who` command does seem reliably to have the information you want, so +1 – Zanna Feb 26 '17 at 23:19