Table of Contents

Launch WinSCP session at present working directory from within KiTTY

Problem Description

I've used KiTTY with zmodem for many years to quickly inject or fetch files from remote SSH hosts. There was simply no quicker way to get this job done until the zmodem code got broken, abandonned and finally removed following the KiTTY v0.71 release. The last known KiTTY version with working zmodem transfer is v0.70.0.10, available here, which lacks some compatibility bugfixes with recent Windows versions and some security fixes.

Since then I had to launch WinSCP using SHIFT + F3 which would connect to the remote host I had connected to using KiTTY. However, by default WinSCP connects to the home directory of the user. So with each WinSCP session I'd have to navigate to my current directory on my own, this can be a major hassle depending on the complexity of the directory structure. Furthermore, unlike using rz.exe and sz.exe (with lrzsz installed on the host), which easily uploads/downloads files through intermediate hosts, a launched WinSCP session will only connect to the original bastion host and not to the final destination host that I have proxied to.

Solution

I looked into the KiTTY source, realized that KiTTY is listening to some control characters to interact with WinSCP and hacked something together. While testing, I realized that tmux and screen are not passing these through. After digging through tmux and screen sources, I found out about their similar, but still different device control string approaches which would allow this to work.

The following script, when executed on the remote host through a KiTTY ssh connection, will check if the user is likely running tmux or screen and will then launch a WinSCP session to the FQDN hostname of the host. It will use the current SSH username and auto-navigate to the present working directory. This will even work if you have used an intermediate host to proxy yourself to a destination host if both hosts use the same SSH port. You will always connect to the host where this script is being executed.

This will only work if your SSH hosts have proper FQDN hostnames configured and the DNS server which you are using is able to resolve all these hostnames to reachable IP addresses.

You will need this script on all hosts you are connecting to. I save this script at /usr/local/bin/winscp:

#!/bin/bash
if [ -n "$STY" ]; # we're in a local screen session
then
  printf '\eP\e\e]0;__wt:%s:%s:%s\a\e\\' "$(hostname -f)" "$USER" "$PWD"
elif [[ $TERM = screen* ]] || [ -n "$TMUX" ]; # either connected remotely while in tmux/screen or locally in tmux
then
  printf '\ePtmux;\e\e]0;__wt:%s:%s:%s\a\e\\' "$(hostname -f)" "$USER" "$PWD"
else
  printf '\e]0;__wt:%s:%s:%s\a\e\\' "$(hostname -f)" "$USER" "$PWD"
fi

Now, when I am in a remote session, I will simply type the command winscp and WinSCP opens right where I want it to be.

Known Issues

Notes