Dushkin dot org
21
Nov '06

Automatically set X display to client machine when SSHing

— dushkin
@12:17

At home I have a server running Linux and a MacBook.

Every now and then, I want to run an X11 program. VNC takes up my whole screen, and therefor it’s counter productive, but there’s another way.

Mac OS X has an optional X11 server. After installing it, programs could connect to it and coexist in the Aqua environment.

The thing is that while my server doesn’t change IP, my MacBook does, since I often alternate between wi-fi and ethernet, and even then I can’t be bothered to set it every time. So basically, the IP always changes.

For this purpose, I made this useful script. I’ve tested it on zsh, but it should work in Bash as well, I believe. I put the following lines in my .zshrc:

if [ $SSH_CLIENT ]
then
export DISPLAY=`echo $SSH_CLIENT | sed ’s/ [0-9]* [0-9]*$//’`:0
fi

Basically, if the variable $SSH_CLIENT is defined, it takes that variable, nips the last two figures (one being the source port and the other being the destination port), which leaves us only with the IP of the machine we’re SSHing from. Pretty useful. Now all you need to do is put xhost the_ip, where the_ip is the IP of the machine you want to connect to you, in your .xinitrc to automatically allow it to connect to you.

It werks!

Tagged as: , , ,

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Denmark License. © Adi Ron 2007

Disclaimer: Nothing here is true.