sshwitch 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/bin/sshwitch +174 -0
  2. metadata +47 -0
@@ -0,0 +1,174 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ require 'optparse'
4
+
5
+ include FileUtils
6
+
7
+ HOME_PATH = File.expand_path('~')+'/'
8
+ SSH_PATH = HOME_PATH + '.ssh/'
9
+ SWITCH_FILE = SSH_PATH+'.sshwitch'
10
+
11
+ def get_current
12
+ begin
13
+ File.open(SWITCH_FILE).first
14
+ rescue
15
+ File.new(SWITCH_FILE, 'w')
16
+ nil
17
+ end
18
+ end
19
+
20
+ def list
21
+ begin
22
+ Dir[SSH_PATH+'*/'].map { |a| File.basename(a) }
23
+ end
24
+ end
25
+
26
+ def switch(name)
27
+ if File.readable?(SSH_PATH+name+"/id_rsa") && File.readable?(SSH_PATH+name+"/id_rsa.pub")
28
+ begin
29
+ cp(SSH_PATH+name+"/id_rsa", SSH_PATH+"id_rsa", :verbose => true)
30
+ cp(SSH_PATH+name+"/id_rsa.pub", SSH_PATH+"id_rsa.pub", :verbose => true)
31
+ File.open(SWITCH_FILE, 'w') {|f| f.write(name) }
32
+ puts "Changed key pair to: " + name
33
+ rescue => e
34
+ puts "Could not copy, check if you have permission to write on #{SSH_PATH}"
35
+ puts e.message
36
+ end
37
+ else
38
+ puts "Could not read key pair in #{SSH_PATH+name}"
39
+ puts "Check if key pair exists in #{SSH_PATH+name}\n\n"
40
+ puts "If not you can create a new one with: \"sshwitch -n #{name}\""
41
+ puts "Or backup the current key pair in #{SSH_PATH} with: \"sshwitch -b #{name}\""
42
+ end
43
+ end
44
+
45
+ def new(name)
46
+ if File.directory? SSH_PATH+name
47
+ puts "#{SSH_PATH+name} already exists, skipping"
48
+ else
49
+ puts "Creating key pair in #{SSH_PATH+name}"
50
+ begin
51
+ mkdir SSH_PATH+name
52
+ `ssh-keygen -t rsa -f #{SSH_PATH+name}/id_rsa`
53
+ rmdir SSH_PATH+name
54
+ rescue => e
55
+ puts "Could not create key pair, check if you have permission to write on #{SSH_PATH+name}"
56
+ puts e.message
57
+ end
58
+ end
59
+ end
60
+
61
+ def backup(name)
62
+ if File.directory? SSH_PATH+name
63
+ puts "#{SSH_PATH+name} already exists, skipping"
64
+ else
65
+ puts "Copying current key pair in #{SSH_PATH} to #{SSH_PATH+name}"
66
+ begin
67
+ mkdir SSH_PATH+name
68
+ cp(SSH_PATH+"/id_rsa", SSH_PATH+name+"id_rsa", :verbose => true)
69
+ cp(SSH_PATH+"/id_rsa.pub", SSH_PATH+name+"id_rsa.pub", :verbose => true)
70
+ rescue => e
71
+ puts "Could not backup key pair, check if you have permission to write on #{SSH_PATH+name}"
72
+ puts e.message
73
+ end
74
+ end
75
+ end
76
+
77
+ def rename(oldname, newname)
78
+ if File.directory? SSH_PATH+newname
79
+ puts "#{SSH_PATH+newname} already exists, cannot rename"
80
+ else
81
+ begin
82
+ mv SSH_PATH+oldname, SSH_PATH+newname, :verbose => true
83
+ if get_current == oldname
84
+ File.open(SWITCH_FILE, 'w') {|f| f.write(newname) }
85
+ puts "Renamed the current key"
86
+ end
87
+ rescue => e
88
+ puts e.message
89
+ puts "Could not rename"
90
+ end
91
+ end
92
+ end
93
+
94
+ def delete(name)
95
+ begin
96
+ Dir.glob(File.join(SSH_PATH+name, "*")).each {|file| rm file, :verbose => true}
97
+ rmdir SSH_PATH+name, :verbose => true
98
+ rescue => e
99
+ puts e.message
100
+ end
101
+ end
102
+
103
+ options = {}
104
+
105
+ option_parser = OptionParser.new do |opts|
106
+ exec_name = File.split($0)[1]
107
+ opts.banner = "Switch and manage key pairs in #{SSH_PATH} \n\n"
108
+ opts.banner += "Default usage: #{exec_name} key_pair_name \n\n"
109
+ opts.banner += "Advanced usage: #{exec_name} [option]\n"
110
+
111
+ opts.on("-c", "--current", "Get name of current key pair") do
112
+ options[:current] = true
113
+ end
114
+
115
+ opts.on("-n NEW", "--new", "New key pair name") do |new|
116
+ options[:new] = new
117
+ end
118
+
119
+ opts.on("-b BACKUP", "--backup", "Backup key pair in #{SSH_PATH}") do |backup|
120
+ options[:backup] = backup
121
+ end
122
+
123
+ opts.on("-r OLDNAME,NEWNAME", "--rename", Array, "Rename a key pair") do |rename_names|
124
+ if rename_names.count == 2
125
+ puts "Rename #{rename_names[0]} to #{rename_names[1]}"
126
+ options[:oldname] = rename_names[0]
127
+ options[:newname] = rename_names[1]
128
+ else
129
+ puts "Wrong number of arguments for Rename (#{rename_names.count} for 2)"
130
+ end
131
+ end
132
+
133
+ opts.on("-l", "--list", "Get list of key pairs in #{SSH_PATH} \n\n") do
134
+ options[:list] = true
135
+ end
136
+
137
+ opts.on("-d DELETE", "--delete", "Delete key pair \n\n") do |delete|
138
+ options[:delete] = delete
139
+ end
140
+ end.parse!
141
+
142
+ #If no options and no name param print current key
143
+ if options.empty? && ARGV.empty?
144
+ puts get_current
145
+ #If no options and name param use default behavior switch()
146
+ elsif options.empty? && ARGV.count > 0
147
+ name = ARGV.shift
148
+ if get_current.nil?
149
+ puts "WARNING! This will overwrite the key pair in #{SSH_PATH}, press Y to confirm"
150
+ if (gets.chr.upcase == 'Y')
151
+ switch(name)
152
+ end
153
+ elsif get_current == name
154
+ puts "#{name} is the current key pair"
155
+ else
156
+ switch(name)
157
+ end
158
+ #If other options...
159
+ elsif options[:current]
160
+ puts get_current
161
+ elsif options[:list]
162
+ puts list
163
+ elsif options[:new]
164
+ new(options[:new])
165
+ elsif options[:backup]
166
+ backup(options[:backup])
167
+ elsif options[:delete]
168
+ puts "WARNING! This will delete the directory and all files in #{SSH_PATH+options[:delete]}, press Y to confirm"
169
+ if (gets.chr.upcase == 'Y')
170
+ delete(options[:delete])
171
+ end
172
+ elsif options[:oldname] && options[:newname]
173
+ rename(options[:oldname], options[:newname])
174
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sshwitch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Agustín Leñero
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Manage different sets of keys in your home folder
15
+ email: agush@roca9.com
16
+ executables:
17
+ - sshwitch
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/sshwitch
22
+ homepage: http://rubygems.org/gems/sshwitch
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Manage your ssh keys
47
+ test_files: []