Class: SSHCon

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

Constant Summary collapse

CONF_PATH =
File.expand_path '~/.con'
CONF_LINE_FIELDS =
%w(shortcut conn_desc host user port knock unknock)
DEFAULT_PORT =
"22"

Instance Method Summary collapse

Constructor Details

#initializeSSHCon

Returns a new instance of SSHCon.



10
11
12
# File 'lib/con_ssh.rb', line 10

def initialize
  @conn_confs = {}
end

Instance Method Details

#run(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/con_ssh.rb', line 14

def run *args
  # setup
  if args[0] == 'setup'
    ign_arg_warn args, 1
    install_sample_conf

  # knock / unknock
  elsif ['knock', 'unknock'].include? args[1] 
    ign_arg_warn args, 2
    parse_conf
    conn_conf = @conn_confs[args[0]]
    unless conn_conf
      warn "Shorcut #{args[0]} not found. See #{CONF_PATH}."
      exit 1
    end
    ports = conn_conf[args[1]]
    if ports.nil? || ports.empty?
      warn "#{conn_conf.conn_desc} has no #{args[1]} configuration."
      exit 1
    end
    knock conn_conf.host, ports

  # help
  elsif ['-h'].include? args[0]
    ign_arg_warn args, 1
    print_help

  # connect
  elsif args[0]
    ign_arg_warn args, 1
    parse_conf
    conn_conf = @conn_confs[args[0]]
    unless conn_conf
      warn "Shorcut #{args[0]} not found. See #{CONF_PATH}."
      exit 1
    end
    ssh conn_conf

  # fail
  else
    warn "Invalid arguments."
    print_help
    exit 1
  end
end