Class: Tunnel::CommandLineInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnel/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CommandLineInterface

Returns a new instance of CommandLineInterface.



5
6
7
8
9
# File 'lib/tunnel/cli.rb', line 5

def initialize( config )
	@config = config
	@target = nil
	@cancelled = false
end

Class Method Details

.parse_and_runObject



60
61
62
63
64
65
66
67
# File 'lib/tunnel/cli.rb', line 60

def self.parse_and_run
	config = Config.new
	if config.valid? 
		cli = CommandLineInterface.new( config )
		cli.parse
		cli.bore if cli.valid?
	end
end

Instance Method Details

#boreObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tunnel/cli.rb', line 31

def bore
	puts "Starting #{APP_NAME} v#{VERSION}"
	puts

	trap("INT") { @cancelled = true }
	Net::SSH.start( @target.host, @target.username ) do |session|
		forward_ports( session )
	end

	puts "Shutting down the machine."
end

#forward_ports(session) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tunnel/cli.rb', line 43

def forward_ports( session )
	begin
		puts "Opened connection to #{@target.username}@#{@target.host}:"
		@target.each_forward do |fwd|
			remote_server, port = fwd
			session.forward.local( port, remote_server, port )
			puts "\tforwarded port #{port} to #{remote_server}:#{port}"
		end
		puts "\twaiting for Ctrl-C..."
		session.loop(0.1) { not @cancelled }
		puts "\n\tCtrl-C pressed. Exiting."
	rescue Errno::EACCES
		@cancelled = true
		puts "\tCould not open all ports; you may need to sudo if port < 1000."
	end
end

#parseObject



11
12
13
14
15
16
17
18
19
# File 'lib/tunnel/cli.rb', line 11

def parse
	if ARGV.size != 1 
		print_targets "SYNTAX: tbm <target>\n\nWhere target is one of:" 
	else
		target_name = ARGV[0]
		@target = @config.get_target( target_name )
		print_targets( "Cannot find target: #{target_name}\n\nThese are the targets currently defined:" ) if @target.nil?
	end
end


21
22
23
24
# File 'lib/tunnel/cli.rb', line 21

def print_targets( message )
	puts message
	@config.each_target { |target| puts "\t#{target.name}" }
end

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/tunnel/cli.rb', line 27

def valid?
	!@target.nil?
end