Saturday, February 7, 2009

ssh -X replacement with NX protocol using nxproxy without using NX server

Doing ssh X11 forwarding through internet to my home is slow for me, so I come up with the following:

  1. apt-get install nxproxy for both local and remote computer
  2. Save the following dirty and ugly script as ssh-nx and put it in $HOME/bin or /usr/local/bin:


    #!/bin/bash
    #+1024 to make sure port can be opened by normal user, +4000 to make DISPLAY non-negative
    PORTNUM=$((RANDOM+5024)) #TODO makes sure the PORT is not bind by other process
    #DISPLAY and Listen Port differ by 4000 default
    RDISPLAY=$((PORTNUM-4000))
    HOSTNAME=$1
    PROGRAM=${2-\$SHELL -l}
    COOKIE=`xauth list ${DISPLAY-:0} | awk '{ print $2,$3 }'`
    COMMAND=\
    "nxproxy -C link=modem :$RDISPLAY &>/dev/null &
    xauth add :$RDISPLAY $COOKIE;\
    DISPLAY=:$RDISPLAY $PROGRAM;\
    rm /tmp/.X11-unix/X$RDISPLAY;\
    xauth remove :$RDISPLAY"
    nxproxy -S localhost:$RDISPLAY &>/dev/null &
    ssh -t -oExitOnForwardFailure=yes -L $PORTNUM:localhost:$PORTNUM $HOSTNAME $COMMAND
    #TODO kill nxproxy if ssh invoke failure (It do terminate automatically if connect to client nxproxy succesfully followed by connection termination)


  3. replace "ssh -X" with "ssh-nx", such as following:
    username@localmachine:~$ ssh-nx username@remotemachine
    username@remotemachine:~$emacs foo.c

    or

    username@localmachine:~$ ssh-nx username@remotemachine "emacs foo.c"



No comments: