Page 1 of 1

Script to Detect the Display

Posted: Tue 24 Mar 2020, 05:08
by s243a
I created a script to detect the display. I created it because if I ssh into a computer then do a chroot after I ssh, the "DISPLAY" variable isn't set in the chroot. My script works by looping through entries in "xauth list" and testing each display with the "xset" command.

I should probably first check if the current display variable is valid and use that.

For example if the current display variable of the host is:

Code: Select all

puppypc1365/unix:10
then in the chroot the following should work

Code: Select all

export DISPLAY=:10
but would the following also work:

Code: Select all

puppypc1365:10
if we set the hostname in the chroot to "puppypc1365"

I think that if we add an entry starting with "127.0.0.1:10" to the ,Xauthority file by using the xhost (or xauth?) command then we can use TCP sockets instead of unix sockets. Well we can provided the ssh server uses the option "X11UseLocalhost no"

Anyway, here is the script:

Code: Select all

function get_display(){
  if [ "$BIND_X11_SOCKETS" = yes ]; then
	  xauth list | grep "$(hostname)" | grep unix | \
	    while read line; do
	      export DISPLAY=:"$(echo "$line" | sed 's/^.*[:]\([0-9.]*\)\([[:space:]]\).*/\1/g')"
	      if [ xset q &>/dev/null ]; then #The display is valid
	        break
	      fi
	    done  
	    echo "$DISPLAY"
  else #else branch not tested
	  xauth list | grep "$(hostname)" | grep -v unix | \
	    while read line; do
	      export DISPLAY=:"$(echo "$line" | sed 's%^.*[/]\([0-9.]*\)\([[:space:]]\).*%\1%g')"
	      if [ xset q &>/dev/null ]; then #The display is valid
	        break
	      fi
	    done  
	    echo "$DISPLAY"    
  fi
}
...
    if [ "$BIND_X11_SOCKETS" = yes ]; then
      SB=/mnt/sb/fakeroot
      mkdir -p $SB/tmp/.X11-unix
      mount --bind /tmp/.X11-unix $SB/tmp/.X11-unix 
      
      cp /root/.Xauthority $FAKEROOT/root/.Xauthority 
    fi
    export DISPLAY="$(get_display)"
https://pastebin.com/eT6LYsBq

This script is a modified version of the one in my psandbox project.