#!/bin/sh # # x-app-open - open remote X app windows on Windows desktop with Cygwin # # Copyright (C) 2013 Marko Myllynen # This program is free software; it is released under the GPLv3. # # Installation instructions for Cygwin and this script: # # 1) Install from http://www.cygwin.com/ using 32 or 64-bit setup*.exe # 2) During installation, select at least these additional packages: # # keychain openssh psmisc xorg-server xterm # # 3) Place this script in C:\cygwin\bin # 4) Create a shortcut on Windows desktop with # a) "Start in" being the bin path 3) above # b) "Target" being # # C:\cygwin\bin\run.exe -p /bin /bin/sh -c "/bin/x-app-open host application" # # In case a user name needs to be specified use the form user@host. # SSH_OPTS="-oBatchMode=yes -oConnectTimeout=20 -oStrictHostKeyChecking=no" # Launch XWin if not there already XPID=$(ps -ef | grep /bin/XWin | grep -v grep | awk '{print $2}') if [ "$XPID" = "" ]; then run cygstart --hide /bin/XWin :0 -clipboard -multiwindow XPID=$(ps -ef | grep /bin/XWin | grep -v grep | awk '{print $2}') fi export DISPLAY=$HOSTNAME:0 # Setup SSH keys if needed while [ ! -f "$HOME/.ssh/id_rsa" ]; do xterm +tb -T "Setting up SSH keys..." -e \ "echo Generating SSH keys, enter a new passphrase, at least 5 characters.;\ ssh-keygen -f \"$HOME/.ssh/id_rsa\" > /dev/null; \ echo Copying SSH public key to the remote host...; \ ssh-copy-id -i \"$HOME/.ssh/id_rsa.pub\" \ -oStrictHostKeyChecking=no $1 2>&1 | grep -v INFO" done # Add SSH keys to ssh-agent with keychain if needed [[ -f "$HOME/.keychain/$HOSTNAME-sh" ]] && \ . "$HOME/.keychain/$HOSTNAME-sh" > /dev/null 2>&1 ssh-add -l 2>/dev/null | grep -q "$HOME/.ssh/id_rsa" if [ $? -ne 0 ]; then cygstart --hide xterm +tb -T '"Loading SSH keys..."' -e "keychain -q \"$HOME/.ssh/id_rsa\"" sleep 3 while true; do sleep 1 pstree -aA | grep -q -- "[-]xterm +tb -T Loading SSH keys..." || break done [[ -f "$HOME/.keychain/$HOSTNAME-sh" ]] && \ . "$HOME/.keychain/$HOSTNAME-sh" > /dev/null 2>&1 fi # Do the actual work ssh $SSH_OPTS -X $1 "${@:2}" > /dev/null 2>&1 if [ $? -eq 255 ]; then # Connection failed, perhaps a new host, try to copy the key xterm +tb -T "Setting up SSH keys..." -e \ "echo Copying SSH public key to the remote host...; \ ssh-copy-id -i \"$HOME/.ssh/id_rsa.pub\" \ -oStrictHostKeyChecking=no $1 2>&1 | grep -v INFO" ssh $SSH_OPTS -X $1 "${@:2}" > /dev/null 2>&1 fi # Terminate XWin if no active connections left sleep 1 sessions=$(pstree -aA | grep "[s]h $0" | wc -l) if [ $sessions -le 2 ]; then /bin/kill $XPID > /dev/null 2>&1 ; /bin/kill -f $XPID > /dev/null 2>&1 fi