Class: SSH::Manager::Cli
- Inherits:
-
Object
- Object
- SSH::Manager::Cli
- Defined in:
- lib/ssh/manager/cli.rb
Constant Summary collapse
Instance Method Summary collapse
- #add_connection ⇒ Object
- #check_connection_type(connection) ⇒ Object
- #connect_to(id) ⇒ Object
- #delete(id) ⇒ Object
- #execute_command(id) ⇒ Object
-
#initialize(opts = {}) ⇒ Cli
constructor
A new instance of Cli.
- #list_all ⇒ Object
- #multiple_connection(term) ⇒ Object
- #ping(id) ⇒ Object
- #search_for(term) ⇒ Object
- #settings ⇒ Object
- #show_info(id) ⇒ Object
- #test(id) ⇒ Object
- #transfer_file(filename, id = '', dest_path = "/home/#{user}/") ⇒ Object
- #transfer_key(id) ⇒ Object
- #update(id) ⇒ Object
- #update_available ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Cli
Returns a new instance of Cli.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ssh/manager/cli.rb', line 15 def initialize(opts = {}) @options = opts @pretty_names = {:id => "ID", #TODO :connect_via :ip => "IP or hostname", :user => "Username", :hostname => "Alias", :port => "Port", :note => "Note", :options => "Options", :group => "Group", :connect_via => "Connect via (id)"} @visible_fields = [:id, :ip, :group, :note] @input_fields = [:ip, :hostname, :user, :port, :options, :note, :group, :connect_via] @column_width = 15 #TODO make this dynamic or a yaml setting end |
Instance Method Details
#add_connection ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/ssh/manager/cli.rb', line 193 def add_connection connection = {:user => ENV['USER'], :port => "22"} @input_fields.each do |key| #TODO make this a method connection[key] = %x{source #{File.dirname(__FILE__)}/ask.sh; ask '#{@pretty_names[key]}' '#{connection[key]}'}.chomp end connection[:count] = 0 connection[:created_at] = Time.now.to_s connection[:last_time] = Time.now.to_s connection[:connect_via] = nil if connection[:connect_via] == "" DATABASE.add_new_connection(connection) #TODO catch SQLite3::ConstraintException end |
#check_connection_type(connection) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/ssh/manager/cli.rb', line 31 def check_connection_type(connection) if connection.class == String "hostname" else "ip" end end |
#connect_to(id) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ssh/manager/cli.rb', line 39 def connect_to(id) id.each do |conn| conn.to_i ssh_command = "" i = conn begin connection = DATABASE.get_connection_by_id(i) ip = connection[:ip] user = connection[:user] user = ENV['USER'] if user == "" = connection[:options] via = connection[:connect_via] ssh_command = "ssh -A -t #{} #{user}@#{ip} #{ssh_command}" i = via #TODO prevent endless via-loops end while via connection[:count] += 1 connection[:last_time] = Time.now DATABASE.update_connection(connection) if CONFIG['target'] == "self" exec ssh_command elsif 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_command}") elsif CONFIG['terminal'] == "xterm" || CONFIG['terminal'] == "urxvt" %x(#{CONFIG['terminal']} -e "#{ssh_command}") else puts "We dont support #{CONFIG['terminal']} right now" puts 'Check Github for further development or contributing' end end #TODO: if db[secure_login] = false => http://linuxcommando.blogspot.de/2008/10/how-to-disable-ssh-host-key-checking.html end |
#delete(id) ⇒ Object
208 209 210 211 212 |
# File 'lib/ssh/manager/cli.rb', line 208 def delete(id) id.each do |conn| DATABASE.delete_connection(conn.to_i) end end |
#execute_command(id) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ssh/manager/cli.rb', line 132 def execute_command(id) # id.first should be the command cmd = id.shift id.each do |con| connection = DATABASE.get_connection_by_id(con.to_i) if connection[:connect_via] connect_via = DATABASE.get_connection_by_id(connection[:connect_via]) ssh = "ssh #{connect_via[:user]}@#{connect_via[:ip]}" system("#{ssh} #{cmd}") else ssh = "ssh #{connection[:user]}@#{connection[:ip]}" system("#{ssh} #{cmd}") end end end |
#list_all ⇒ Object
214 215 216 217 218 219 220 221 222 |
# File 'lib/ssh/manager/cli.rb', line 214 def list_all connections = DATABASE.get_connection_data @visible_fields.each { |f| printf "%-#{@column_width}s", @pretty_names[f] } puts "\n" connections.each do |c| @visible_fields.each { |f| printf "%-#{@column_width}s", c[f] } puts "\n" end end |
#multiple_connection(term) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/ssh/manager/cli.rb', line 249 def multiple_connection(term) @cons = [] DATABASE.search_for(term).each do |x| x.all.each do |dataset| @cons.push(dataset) end end # FAIL, first push everything in cons, then do the rest ! puts "what yer gonna do?" = {execute_command: 'execute_command', connect_to: 'connect', ping: 'ping', info: 'info', delete: 'delete'} comp = proc { |s| .values.grep(/^#{Regexp.escape(s)}/) } #Readline.completion_append_character = " " Readline.completion_proc = comp puts .values.to_s input= Readline.readline('> ', true) ids = @cons.map {|x| x[:id].to_s} input = input.chomp.gsub(/\s+\Z/, "") case input when 'execute_command' # which commands print "command: " input = STDIN.gets.chomp ids.unshift(input) self.execute_command(ids) exit when 'connect' self.connect_to(ids) exit when 'ping' # exits after first ping self.ping(ids) exit when 'info' self.show_info(ids) exit when 'delete' self.delete(ids) exit else puts "That i dont know :/" end end |
#ping(id) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ssh/manager/cli.rb', line 119 def ping(id) id.each do |con| connection = DATABASE.get_connection_by_id(con.to_i) if connection[:connect_via] connect_via = DATABASE.get_connection_by_id(connection[:connect_via]) ssh = "ssh #{connect_via[:user]}@#{connect_via[:ip]}" exec("#{ssh} ping #{connection[:ip]} -c 3") else exec("ping #{connection[:ip]} -c 3") end end end |
#search_for(term) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/ssh/manager/cli.rb', line 237 def search_for(term) puts "All results for searchterm: #{term}" @visible_fields.each { |f| printf "%-#{@column_width}s", @pretty_names[f] } print "\n" DATABASE.search_for(term).each do |x| x.all.each do |cons| @visible_fields.each { |f| printf "%-#{@column_width}s", cons[f] } puts "\n" end end end |
#settings ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/ssh/manager/cli.rb', line 153 def settings CONFIG.keys.each do |key| puts "#{key}=#{CONFIG[key]}" end ={terminal: %w(xfce4-terminal gnome-terminal urxvt), deletion_interval: %w(never), tabbed: %w(true false), self: %w(true false)} # change tabbed to mode %w(tabbed whatelse) print "Want to change? (y/n)" answer = STDIN.gets.chomp comp = proc { |s| .values.flatten.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = " " Readline.completion_proc = comp if answer == "y" CONFIG.keys.each do |key| puts "#{key}:".colorize(:green) puts "possible options are: #{[key.to_sym]}" puts "leave blank to not make any changes (TAB-Completion avalaible)" input = Readline.readline('> ', true) CONFIG[key] = input if input != "" end File.open(("#{File.join(ENV['HOME'])}/.config/sshm/settings.yml"),'w') do |h| h.write CONFIG.to_yaml end else exit end end |
#show_info(id) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ssh/manager/cli.rb', line 90 def show_info(id) @input_fields << :count @input_fields << :last_time id.each do |conn| connections = DATABASE.get_connection_by_id(conn.to_i) puts "\n" @input_fields.each do |field| if field == :connect_via && connections[field] != nil via_ip = DATABASE.get_connection_by_id(connections[field])[:ip] printf "%s: %s -|- %s ", field, connections[field], via_ip else printf "%s: %s", field, connections[field] end puts "\n" end end end |
#test(id) ⇒ Object
148 149 150 151 |
# File 'lib/ssh/manager/cli.rb', line 148 def test(id) require 'byebug' byebug end |
#transfer_file(filename, id = '', dest_path = "/home/#{user}/") ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/ssh/manager/cli.rb', line 183 def transfer_file(filename, id='', dest_path="/home/#{user}/") #TODO connect_via #TODO options connection = DATABASE.get_connection_by_id(id) user = connection[:user] user = ENV['USER'] if user == "" %x(scp #{filename} #{user}@#{connection[:ip]}:#{dest_path}) if File.file?(filename) %x(scp -r #{filename} #{@user}@#{connection[:ip]}:#{dest_path}) if File.directory?(filename) end |
#transfer_key(id) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ssh/manager/cli.rb', line 108 def transfer_key(id) #TODO connect_via #TODO options id.each do |con| connection = DATABASE.get_connection_by_id(con.to_i) user = connection[:user] user = ENV['USER'] if user == "" %x(ssh-copy-id #{user}@#{connection[:ip]}) end end |
#update(id) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/ssh/manager/cli.rb', line 224 def update(id) id.each do |con| connection = DATABASE.get_connection_by_id(con.to_i) @input_fields.each do |key| #TODO make this a method connection[key] = %x{source #{File.dirname(__FILE__)}/ask.sh; ask '#{@pretty_names[key]}' '#{connection[key]}'}.chomp end connection[:connect_via] = nil if connection[:connect_via] == "" DATABASE.update_connection(connection) #TODO catch SQLite3::ConstraintException end end |
#update_available ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/ssh/manager/cli.rb', line 80 def update_available new_version =%x(gem search ssh-manager).split(' ')[1].gsub /\((.*)\)/, '\1' old_version = SSH::Manager::VERSION if new_version<old_version puts "There is a update available #{new_version} was released. -> sudo gem update ssh-manager" else puts "Version: #{old_version} is up to date." end end |