Class: SSH::Manager::Cli

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

Constant Summary collapse

DATABASE =
SSH::Manager::Database.new

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Cli



13
14
15
# File 'lib/ssh/manager/cli.rb', line 13

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

Instance Method Details

#add_connection(ip) ⇒ Object



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

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
  puts 'Options: '
  options = $stdin.gets.chomp
  options = '' if options == ''
  puts 'Group: '
  group = $stdin.gets.chomp
  count = 0
  created_at = Time.now.to_s
  last_time = Time.now.to_s
  DATABASE.add_new_connection(ip, user, hostname, port, note, created_at, options, count, group, last_time)
end

#check_term(ip, user) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ssh/manager/cli.rb', line 17

def check_term(ip, user)
  if CONFIG['terminal'] == "xfce4-terminal" || CONFIG['terminal'] == "gnome-terminal"
    if CONFIG['tabbed'] == 'true'
      command = "--title=#{user}@#{ip} --tab --command="
    else
      command = "--title=#{user}@#{ip} --command="
    end
    #TODO: add title --title='connection name to identify '
    #TODO: bug when no terminal is open => wants to open 2 terms
    #TODO: dnslookup
    %x(#{CONFIG['terminal']} #{command}"ssh #{user}@#{ip}")
  elsif CONFIG['terminal'] == "xterm" || CONFIG['terminal'] == "urxvt"
    %x(#{CONFIG['terminal']} -e "ssh #{user}@#{ip}")
  else
    puts "We dont support #{CONFIG['terminal']} right now"
    puts 'Check Github for further development or contributing'
  end
end

#connect_to(id) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/ssh/manager/cli.rb', line 36

def connect_to(id)
  @ip = DATABASE.get_connection_data[id.to_i-1][0]
  @user = DATABASE.get_connection_data[id.to_i-1][1]
  check_term(@ip, @user)
  #TODO: check for options
  #TODO: if db[secure_login] = false => http://linuxcommando.blogspot.de/2008/10/how-to-disable-ssh-host-key-checking.html
end

#delete(id) ⇒ Object



83
84
85
86
# File 'lib/ssh/manager/cli.rb', line 83

def delete(id)
  id = id.to_i - 1
  DATABASE.delete_connection(DATABASE.get_connection_data[id][0])
end

#list_allObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ssh/manager/cli.rb', line 88

def list_all
  cnt = 0
  connections = Hash[DATABASE.get_connection_data.collect { |x| [cnt+=1, x]}]
  puts 'ID IP             USERNAME       NOTES          GROUP'
  connections.each do |x|
    print "#{x[0]}: "
    x[1].each do |a|
      printf '%-15s', a
    end
    puts "\n"
  end
end

#multiple_connection(term) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/ssh/manager/cli.rb', line 131

def multiple_connection(term)
  DATABASE.search_for(term).each do |x|
    x.all.each do |dataset|
      check_term(dataset[:ip], dataset[:user])
      #TODO: Add terminalposition
    end
  end
end

#search_for(term) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ssh/manager/cli.rb', line 118

def search_for(term)
  puts 'IP                  USERNAME            HOSTNAME            PORT                NOTES               GROUP'
  DATABASE.search_for(term).each do |x|
    x.all.each do |cons|
      cons.values.each do |each_con|
        printf '%-20s', each_con
      end
      puts "\n"
    end
  end
  puts "All results for searchterm: #{term}"
end

#transfer_key(id) ⇒ Object



55
56
57
58
59
# File 'lib/ssh/manager/cli.rb', line 55

def transfer_key(id)
  @ip = DATABASE.get_connection_data[id.to_i-1][0]
  @user = DATABASE.get_connection_data[id.to_i-1][1]
  %x(ssh-copy-id #{@user}@#{@ip})
end

#update(id) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ssh/manager/cli.rb', line 101

def update(id)
  puts 'Entries wont change of left out blank.'
  puts 'Username: '
  user =$stdin.gets.chomp
  user = DATABASE.get_connection_data[id.to_i][1] if user == ''
  puts 'Hostname: '
  hostname = $stdin.gets.chomp
  hostname = DATABASE.get_connection_data[id.to_i][2] if hostname == ''
  puts 'port: '
  port = $stdin.gets.chomp
  port = DATABASE.get_connection_data[id.to_i][3] if port == ''
  puts 'Notes: '
  note = $stdin.gets.chomp
  note = DATABASE.get_connection_data[id.to_i][4] if note == ''
  DATABASE.update_connection(DATABASE.get_connection_data[id.to_i][0], user, hostname, port, note)
end

#update_availableObject



44
45
46
47
48
49
50
51
52
# File 'lib/ssh/manager/cli.rb', line 44

def update_available
  #Thread.new {
  new_version =%x(gem search ssh-manager).split(' ')[1]
  old_version = SSH::Manager::VERSION
  if new_version<old_version
    puts 'There is a update available -> sudo gem update ssh-manager'
  end
  #}
end