Skip to content

Instantly share code, notes, and snippets.

@lazywei
Created May 27, 2014 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazywei/e8efa718fe689d3ae52e to your computer and use it in GitHub Desktop.
Save lazywei/e8efa718fe689d3ae52e to your computer and use it in GitHub Desktop.
Automatically create port forwarding regarding to iPython Kernel. Used with vim-ipython
#!/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