Created
October 4, 2012 01:18
-
-
Save i4fumi/3830929 to your computer and use it in GitHub Desktop.
mosh-ruby
This file contains 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/local/bin/ruby | |
# encoding: utf-8 | |
require 'syck' | |
require 'net/ssh' | |
def usage | |
puts "usage: #{File.basename($0)} user@ip_address:ssh_port" | |
exit 1 | |
end | |
def mosh_client(ip_address, user, passwd, ssh_port) | |
mosh_key = nil | |
mosh_port = nil | |
Net::SSH.start(ip_address, user, :password=>passwd, :port=>ssh_port) do |ssh| | |
ssh.open_channel do |channel| | |
channel.request_pty do |ch, success| | |
raise "could not obtain pty" unless success | |
end | |
channel.exec("env LANG=ja_JP.UTF-8 mosh-server") do |ch, success| | |
raise "could not execute command" unless success | |
channel.on_data do |ch, data| | |
puts data | |
if data =~ /MOSH CONNECT (\d+) (.+)$/ | |
mosh_port = $1 | |
mosh_key = $2.chomp | |
end | |
end | |
channel.on_extended_data do |ch, type, data| | |
puts data | |
end | |
end | |
end | |
ssh.loop | |
end | |
passwd.clear | |
if mosh_key | |
command = "env MOSH_KEY=#{mosh_key} mosh-client #{ip_address} #{mosh_port}" | |
puts command | |
exec command | |
end | |
end | |
def main | |
if ARGV.count != 1 | |
usage | |
end | |
unless ARGV[0] =~ /^(\w+)@(\d+\.\d+\.\d+\.\d+):(\d+)$/ | |
usage | |
end | |
user = $1 | |
ip_address = $2 | |
ssh_port = $3 | |
print "password: " | |
system "stty -echo" | |
passwd = $stdin.gets.to_s.chop | |
system "stty echo" | |
print "\n" | |
mosh_client(ip_address, user, passwd, ssh_port) | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment