From 7fa9892b754b5aef7877a10a87cf875089c5f92c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 7 Apr 2013 21:51:14 -0700 Subject: [PATCH] Send SSH keep-alive packets [GH-516] --- CHANGELOG.md | 2 ++ config/default.rb | 1 + plugins/communicators/ssh/communicator.rb | 24 +++++++++++++++++++++-- plugins/kernel_v2/config/ssh.rb | 3 +++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 105142d10..659a246b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,8 @@ IMPROVEMENTS: - Detect PuTTY Link SSH client on Windows and show an error. [GH-1518] - `vagrant ssh` in Cygwin won't output DOS path file warnings. - Add `--rtcuseutc on` as a sane default for VirtualBox. [GH-912] + - SSH will send keep-alive packets every 5 seconds by default to + keep connections alive. Can be disabled with `config.ssh.keep_alive`. [GH-516] BUG FIXES: diff --git a/config/default.rb b/config/default.rb index e38414480..1b3ea2092 100644 --- a/config/default.rb +++ b/config/default.rb @@ -3,6 +3,7 @@ Vagrant.configure("2") do |config| config.ssh.username = "vagrant" config.ssh.guest_port = 22 + config.ssh.keep_alive = true config.ssh.max_tries = 100 config.ssh.timeout = 30 config.ssh.forward_agent = false diff --git a/plugins/communicators/ssh/communicator.rb b/plugins/communicators/ssh/communicator.rb index 852034d74..17cd54dcf 100644 --- a/plugins/communicators/ssh/communicator.rb +++ b/plugins/communicators/ssh/communicator.rb @@ -295,8 +295,28 @@ module VagrantPlugins end end - # Wait for the channel to complete - channel.wait + begin + keep_alive = nil + + if @machine.config.ssh.keep_alive + # Begin sending keep-alive packets while we wait for the script + # to complete. This avoids connections closing on long-running + # scripts. + keep_alive = Thread.new do + loop do + sleep 5 + @logger.debug("Sending SSH keep-alive...") + connection.send_global_request("keep-alive@openssh.com") + end + end + end + + # Wait for the channel to complete + channel.wait + ensure + # Kill the keep-alive thread + keep_alive.kill if keep_alive + end # Return the final exit status return exit_status diff --git a/plugins/kernel_v2/config/ssh.rb b/plugins/kernel_v2/config/ssh.rb index 083efb743..eef85af1f 100644 --- a/plugins/kernel_v2/config/ssh.rb +++ b/plugins/kernel_v2/config/ssh.rb @@ -7,6 +7,7 @@ module VagrantPlugins attr_accessor :forward_x11 attr_accessor :guest_port attr_accessor :host + attr_accessor :keep_alive attr_accessor :max_tries attr_accessor :port attr_accessor :private_key_path @@ -19,6 +20,7 @@ module VagrantPlugins @forward_x11 = UNSET_VALUE @guest_port = UNSET_VALUE @host = UNSET_VALUE + @keep_alive = UNSET_VALUE @max_tries = UNSET_VALUE @port = UNSET_VALUE @private_key_path = UNSET_VALUE @@ -32,6 +34,7 @@ module VagrantPlugins @forward_x11 = false if @forward_x11 == UNSET_VALUE @guest_port = nil if @guest_port == UNSET_VALUE @host = nil if @host == UNSET_VALUE + @keep_alive = false if @keep_alive == UNSET_VALUE @max_tries = nil if @max_tries == UNSET_VALUE @port = nil if @port == UNSET_VALUE @private_key_path = nil if @private_key_path == UNSET_VALUE