sshman 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/exe/sshman +10 -0
  3. data/lib/sshman/sshman.rb +226 -0
  4. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa9cdb191d79a0fb683e6c8555ab82246f55c1de0f567263112c5d9ad4d152e5
4
+ data.tar.gz: 21520da596d46d025f346e910127edc57a88367f57711ebd161caf319edfccac
5
+ SHA512:
6
+ metadata.gz: c54efd16ff02c2cbad5d32aeefc9d2578c0fbcae98aa107fe5549b9a63fca5231a02f494273bb4685097926052ad9ae20f83151f1e889b377b0972bb17f4f6c6
7
+ data.tar.gz: d97c7b5ef6d2201f0ba0e9c4988f8a1e58704147bdce3c797509787b65225b699c0dca3f3401cca877d3fc99ae180f97e9b5e8b8eb01eeaeb320ddf9b987c29d
data/exe/sshman ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/sshman/sshman"
3
+ # Check if any arguments are passed (i.e., inline commands)
4
+ if ARGV.empty?
5
+ # No arguments passed, start the interactive mode
6
+ Sshman::CLI.start_interactive
7
+ else
8
+ # Pass command-line arguments for inline commands (e.g., `sshman list`)
9
+ Sshman::CLI.start(ARGV)
10
+ end
@@ -0,0 +1,226 @@
1
+ # lib/sshman/sshman.rb
2
+
3
+ require 'csv'
4
+ require 'io/console'
5
+
6
+ module Sshman
7
+ class CLI
8
+ # This method handles inline commands
9
+ def self.start(argv)
10
+ case argv[0]
11
+ when 'list'
12
+ new.list_servers
13
+ when 'add'
14
+ new.add_server
15
+ when 'edit'
16
+ new.edit_server
17
+ when 'delete'
18
+ new.delete_server
19
+ when 'connect'
20
+ new.connect_to_server
21
+ when 'help'
22
+ new.display_help
23
+ when 'version'
24
+ new.version
25
+ else
26
+ puts "Unknown command: #{argv[0]}. Use 'sshman help' for a list of commands."
27
+ end
28
+ end
29
+
30
+ # Display the current version
31
+
32
+ def version
33
+ puts "sshman version 0.2.1"
34
+ end
35
+
36
+ # This method starts the interactive menu (default)
37
+ def self.start_interactive
38
+ new.main
39
+ end
40
+
41
+ # Main interactive loop
42
+ def main
43
+ ensure_csv_file
44
+
45
+ loop do
46
+ puts "\n#{YELLOW}Options:#{RESET_COLOR} list, add, edit, delete, connect, help, quit"
47
+ print "#{CYAN}Choose an option: #{RESET_COLOR}"
48
+ option = gets.chomp.downcase.strip
49
+
50
+ case option
51
+ when 'list'
52
+ list_servers
53
+ when 'add'
54
+ add_server
55
+ when 'edit'
56
+ edit_server
57
+ when 'delete'
58
+ delete_server
59
+ when 'connect'
60
+ connect_to_server
61
+ when 'help'
62
+ display_help
63
+ when 'quit'
64
+ puts "#{GREEN}Goodbye!#{RESET_COLOR}"
65
+ break
66
+ else
67
+ puts "#{RED}Unknown option '#{option}'. Type 'help' for usage instructions.#{RESET_COLOR}"
68
+ end
69
+ end
70
+ end
71
+
72
+ # Ensure the CSV file exists and is secure
73
+ def ensure_csv_file
74
+ unless File.exist?(SERVERS_CSV)
75
+ File.open(SERVERS_CSV, 'w') { |file| file.puts "alias,hostname,port,username,ssh_key" }
76
+ puts "#{GREEN}Created servers file at #{SERVERS_CSV}.#{RESET_COLOR}"
77
+ end
78
+ File.chmod(0600, SERVERS_CSV) # Restrict file permissions for security
79
+ end
80
+
81
+ # Display help information
82
+ def display_help
83
+ puts <<-HELP
84
+ Usage: sshman [COMMAND]
85
+ Commands:
86
+ list List all saved servers
87
+ add Add a new server configuration
88
+ edit Edit an existing server
89
+ delete Delete a server by its alias
90
+ connect Connect to a server by its alias
91
+ version Display the current version
92
+ help Display this help information
93
+ HELP
94
+ end
95
+
96
+ # List all servers in a formatted table with colors
97
+ def list_servers
98
+ if File.zero?(SERVERS_CSV)
99
+ puts "#{YELLOW}No servers found. Add some using the 'add' option.#{RESET_COLOR}"
100
+ return
101
+ end
102
+
103
+ puts "#{CYAN}%-15s %-25s %-6s %-15s %-30s#{RESET_COLOR}" % ["Alias", "Hostname", "Port", "Username", "SSH Key"]
104
+ puts "-" * 95
105
+
106
+ CSV.foreach(SERVERS_CSV, headers: true) do |row|
107
+ puts "%-15s %-25s %-6s %-15s %-30s" % [row['alias'], row['hostname'], row['port'], row['username'], row['ssh_key']]
108
+ end
109
+ end
110
+
111
+ # Add a new server to the CSV file
112
+ def add_server
113
+ puts "#{CYAN}Adding a new server...#{RESET_COLOR}"
114
+ print "Alias: "
115
+ alias_name = gets.chomp.strip
116
+ return puts "#{RED}Alias cannot be empty!#{RESET_COLOR}" if alias_name.empty?
117
+
118
+ print "Hostname or IP: "
119
+ hostname = gets.chomp.strip
120
+ return puts "#{RED}Hostname cannot be empty!#{RESET_COLOR}" if hostname.empty?
121
+
122
+ print "Port (default 22): "
123
+ port = gets.chomp.strip
124
+ port = '22' if port.empty?
125
+
126
+ print "Username: "
127
+ username = gets.chomp.strip
128
+ return puts "#{RED}Username cannot be empty!#{RESET_COLOR}" if username.empty?
129
+
130
+ print "Path to SSH key (leave blank if none): "
131
+ ssh_key = gets.chomp.strip
132
+
133
+ CSV.open(SERVERS_CSV, 'a+') do |csv|
134
+ csv << [alias_name, hostname, port, username, ssh_key]
135
+ end
136
+
137
+ puts "#{GREEN}Server '#{alias_name}' added successfully!#{RESET_COLOR}"
138
+ end
139
+
140
+ # Edit an existing server's details
141
+ def edit_server
142
+ print "Enter alias of the server to edit: "
143
+ alias_name = gets.chomp.strip
144
+ servers = CSV.table(SERVERS_CSV)
145
+
146
+ server = servers.find { |row| row[:alias] == alias_name }
147
+ unless server
148
+ puts "#{RED}No server found with alias '#{alias_name}'.#{RESET_COLOR}"
149
+ return
150
+ end
151
+
152
+ print "New Hostname (leave blank to keep current: #{server[:hostname]}): "
153
+ new_hostname = gets.chomp.strip
154
+ server[:hostname] = new_hostname unless new_hostname.empty?
155
+
156
+ print "New Port (leave blank to keep current: #{server[:port]}): "
157
+ new_port = gets.chomp.strip
158
+ server[:port] = new_port unless new_port.empty?
159
+
160
+ print "New Username (leave blank to keep current: #{server[:username]}): "
161
+ new_username = gets.chomp.strip
162
+ server[:username] = new_username unless new_username.empty?
163
+
164
+ print "New SSH Key Path (leave blank to keep current: #{server[:ssh_key]}): "
165
+ new_ssh_key = gets.chomp.strip
166
+ server[:ssh_key] = new_ssh_key unless new_ssh_key.empty?
167
+
168
+ CSV.open(SERVERS_CSV, 'w') do |csv|
169
+ csv << servers.headers
170
+ servers.each { |row| csv << row }
171
+ end
172
+
173
+ puts "#{GREEN}Server '#{alias_name}' updated successfully!#{RESET_COLOR}"
174
+ end
175
+
176
+ # Delete a server from the CSV file
177
+ def delete_server
178
+ print "Enter alias of the server to delete: "
179
+ alias_name = gets.chomp.strip
180
+ servers = CSV.table(SERVERS_CSV)
181
+
182
+ if servers.delete_if { |row| row[:alias] == alias_name }
183
+ CSV.open(SERVERS_CSV, 'w') do |csv|
184
+ csv << servers.headers
185
+ servers.each { |row| csv << row }
186
+ end
187
+ puts "#{GREEN}Server '#{alias_name}' deleted successfully!#{RESET_COLOR}"
188
+ else
189
+ puts "#{RED}No server found with alias '#{alias_name}'.#{RESET_COLOR}"
190
+ end
191
+ end
192
+
193
+ # Connect to a server using SSH
194
+ def connect_to_server
195
+ print "Enter alias of the server to connect: "
196
+ alias_name = gets.chomp.strip
197
+ server = nil
198
+
199
+ CSV.foreach(SERVERS_CSV, headers: true) do |row|
200
+ if row['alias'] == alias_name
201
+ server = row
202
+ break
203
+ end
204
+ end
205
+
206
+ unless server
207
+ puts "#{RED}No server found with alias '#{alias_name}'.#{RESET_COLOR}"
208
+ return
209
+ end
210
+
211
+ ssh_command = "ssh #{server['username']}@#{server['hostname']} -p #{server['port']}"
212
+ ssh_command += " -i #{server['ssh_key']}" unless server['ssh_key'].to_s.empty?
213
+
214
+ puts "#{CYAN}Connecting to #{alias_name}...#{RESET_COLOR}"
215
+ exec ssh_command
216
+ end
217
+
218
+ # Constants for coloring terminal output
219
+ RESET_COLOR = "\033[0m"
220
+ RED = "\033[31m"
221
+ GREEN = "\033[32m"
222
+ YELLOW = "\033[33m"
223
+ CYAN = "\033[36m"
224
+ SERVERS_CSV = File.expand_path("~/.sshman_servers.csv")
225
+ end
226
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sshman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - SubnetMasked
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-10-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple terminal-based SSH manager to add, list, delete, and connect
14
+ to SSH servers.
15
+ email:
16
+ - 'bjorke@startmail.com '
17
+ executables:
18
+ - sshman
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - exe/sshman
23
+ - lib/sshman/sshman.rb
24
+ homepage: https://github.com/subnetmasked/sshman
25
+ licenses:
26
+ - GPL-3.0
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.0.3.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A terminal-based SSH manager
47
+ test_files: []