Created
May 27, 2014 12:03
-
-
Save lazywei/e8efa718fe689d3ae52e to your computer and use it in GitHub Desktop.
Automatically create port forwarding regarding to iPython Kernel. Used with vim-ipython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/ssh' | |
require 'optparse' | |
require 'json' | |
if ARGV.length != 1 | |
puts "need kernel file" | |
abort | |
end | |
PORT_NAMES = %w(stdin_port control_port hb_port shell_port iopub_port) | |
kernel_file = ARGV[0] | |
data = nil | |
ports = "" | |
if File.exist?(kernel_file) | |
data = JSON.parse(IO.read(kernel_file)) | |
else | |
Net::SSH.start('sc', 'sc2014s37') do |ssh| | |
output = ssh.exec!("cat ~/.ipython/profile_default/security/#{kernel_file}") | |
File.open(kernel_file, 'w') do |f| | |
f.puts output | |
end | |
data = JSON.parse(output) | |
end | |
end | |
ports = "" | |
PORT_NAMES.each do |name| | |
port = data[name] | |
ports = "#{ports} -L #{port}:localhost:#{port}" | |
end | |
puts "You can now connect with: --existing #{kernel_file}" | |
system("echo --existing #{kernel_file} | pbcopy") | |
system("ssh sc #{ports}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment