Availability Anywhere Part 11 — Keeping a channel fresh

This is the eleventh part of the Availability Anywhere series. For your convenience you can find other parts in the table of contents in Part 1 – Connecting to SSH tunnel automatically in Windows

We already have our Channel Bonder for aggregating links. However, if all of them go down briefly we end up with a lag. Ideally, we’d like to refresh the connection as soon as possible.

There are 2 network stack registry settings controlling how fast the connection is dropped or restored. They are Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpMaxConnectRetransmissions and Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpMaxDataRetransmissions. By setting them you can control how many retries for keep alive packets are allowed. However, to change them we need to restart the machine. What’s worse, we can’t tune them easily.

What we can do instead is we can track if our SSH connection is alive and then restore it easily if needed. This won’t solve all the issues as mstsc will keep its own reconnection time but will help significantly to keep the channel alive.

First, you start with configuring your kitty to authenticate automatically, reconnect on failures etc. You also configure it to run the following command after connecting to the server:

This will effectively print something like bash: k: command not found. It doesn’t matter what you print as long as we get some visible output.

Next, you configure kitty to write the output to a log file. We want to check that file often to detect when the tunnel got lag. In that case we kill kitty and restart it to refresh the port forwarding as early as possible. This will briefly stop RDP and other services going through the channel but that doesn’t make things worse as they wouldn’t work anyway. Let’s use PowerShell

That’s it. Run this script and your kitty will get restarted as soon as we detect the lag. Tune your retries count and sleep times as you wish.