Class: SSH::Manager::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Cli

Returns a new instance of Cli.



10
11
12
# File 'lib/ssh/manager/cli.rb', line 10

def initialize(opts = {})
  @options = opts
end

Instance Method Details

#add_connection(ip) ⇒ Object



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

def add_connection(ip)
  puts "Username: "
  user =$stdin.gets.chomp
  user = 'root' if user == ''
  puts "Hostname: "
  hostname = $stdin.gets.chomp
  puts "port: "
  port = $stdin.gets.chomp
  port = "22" if port == ''
  puts "Notes: "
  note = $stdin.gets.chomp
  SSH::Manager::Database.new.add_new_connection(ip, user, hostname, port, note)
end

#connect_to(id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ssh/manager/cli.rb', line 14

def connect_to(id)
  ip = SSH::Manager::Database.new.get_connection_data[id.to_i-1][0]
  user = SSH::Manager::Database.new.get_connection_data[id.to_i-1][1]
  if CONFIG['terminal'] == "xfce4-terminal" || "gnome-terminal"
    %x(#{CONFIG['terminal']} --command="ssh #{user}@#{ip}")
  elsif CONFIG['terminal'] == "xterm"
    %x(#{CONFIG['terminal']} -C "ssh #{user}@#{ip}")
  else
    puts "We dont support #{CONFIG['terminal']} right now"
    puts "Check Github for further development or contributing"
  end



end

#delete(id) ⇒ Object



44
45
46
47
# File 'lib/ssh/manager/cli.rb', line 44

def delete(id)
  id = id.to_i - 1
  SSH::Manager::Database.new.delete_connection(SSH::Manager::Database.new.get_connection_data[id][0])
end

#list_allObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ssh/manager/cli.rb', line 49

def list_all
  cnt = 0
  # TODO: add indentation functionality with stringlenght etc..
  connections = Hash[SSH::Manager::Database.new.get_connection_data.collect { |x| [cnt+=1, x]}]
  puts "ID IP                  USERNAME            HOSTNAME            PORT                NOTES"
  connections.each do |x|
    print "#{x[0]}: "
    x[1].each do |a|
      printf "%-20s", a
    end
    puts "\n"
  end

end

#search_for(term) ⇒ Object



80
81
82
83
84
85
# File 'lib/ssh/manager/cli.rb', line 80

def search_for(term)
  SSH::Manager::Database.new.search_for(term).each do |x|
    puts x.all
  end
  puts "All results for searchterm: #{term}"
end

#update(id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ssh/manager/cli.rb', line 64

def update(id)
  puts "Username: "
  user =$stdin.gets.chomp
  user = SSH::Manager::Database.new.get_connection_data[id.to_i][1] if user == ''
  puts "Hostname: "
  hostname = $stdin.gets.chomp
  hostname = SSH::Manager::Database.new.get_connection_data[id.to_i][2] if hostname == ''
  puts "port: "
  port = $stdin.gets.chomp
  port = SSH::Manager::Database.new.get_connection_data[id.to_i][3] if port == ''
  puts "Notes: "
  note = $stdin.gets.chomp
  note = SSH::Manager::Database.new.get_connection_data[id.to_i][4] if note == ''
  SSH::Manager::Database.new.update_connection(SSH::Manager::Database.new.get_connection_data[id.to_i][0], user, hostname, port, note)
end