sshez 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cec6ed214de47ce94ced8d951f0d17d5396b3281
4
- data.tar.gz: 340865e6e375fe4389f3a7a2e2e2860e75bcf6e0
3
+ metadata.gz: 81d4df50a28eadb6dc1d260312afc882db0991c6
4
+ data.tar.gz: 398b00d1e2b4546b27ff77d24f091c9000c93cf7
5
5
  SHA512:
6
- metadata.gz: ef73d905d7aad616af3035a4e2efb4c7d391701aaacd1ee02afc813454794598a72faf9236bfe96a0f1fef2ce2aa5dbce40d36231020877b035bd90f9ba0bb61
7
- data.tar.gz: 2c4d2dd8e939aecf1cfd5d78ecd331fa2d11fba4f1ad1fbab6be4eb17e5816c518f737f4e19b0aaafc5856b9758a55082feec367a5874488ea92aac08e1cba1b
6
+ metadata.gz: 08716750ba791d2b50da690f287cf8658f36f8509c9c1abd06bb21d6123b0ec86ae8ef4b1ab02fdb785027c3378a2d8ea1337d9e368575c5e02af286498c5eca
7
+ data.tar.gz: 99fb3da310dfc3d3187ec5a8ebbfc48e37b059a3600c54b758e92e9d39373a8737d1b567aeab04dfd0d43edfb3a110c5e675a6c7c0fece2a6e3c7ffec141f83d
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ ### Unreleased
2
+
3
+ ### 0.3.0 - 2015-12-14
4
+
5
+ * enhancements
6
+ * Added `list` for listing aliases
7
+ * Added `sshez remove` as another way to remove aliases
8
+
9
+ * bug fixes
10
+ * Fixed permissions bug for CentOS
11
+ * Fixed user experience issues
12
+
13
+ ### 0.2.0 - 2015-12-06
14
+
15
+ * enhancements
16
+ * Added the `remove` feature
17
+
18
+ ### 0.1.0 - 2015-12-05
19
+
20
+ * Initial release
data/README.md CHANGED
@@ -5,43 +5,37 @@ If you have multiple servers that you access on daily bases! sshez helps you con
5
5
 
6
6
  ### Example
7
7
 
8
+ Add an alias `mw_server`
9
+
8
10
  sshez mw_server root@74.125.224.72 -p 120
9
11
 
10
12
  you will be able to use
11
13
 
12
14
  ssh mw_server
13
-
14
-
15
15
 
16
16
  ## Installation
17
17
 
18
- Add this line to your application's Gemfile:
19
-
20
- ```ruby
21
- gem 'sshez'
22
- ```
23
-
24
- And then execute:
25
-
26
- $ bundle
27
-
28
- Or install it yourself as:
29
-
30
18
  $ gem install sshez
31
19
 
32
20
  ## Usage
33
21
 
34
- try sshez -h
22
+ sshez -h
35
23
 
36
- Usage: sshez aliase user@ip [options]
24
+ Usage:
25
+ sshez <alias> (role@host|-r) [options]
26
+ sshez remove <alias>
27
+ sshez list
37
28
 
38
29
  Specific options:
39
30
  -p, --port PORT Specify a port
40
31
  -i, --identity_file [key] Add identity
32
+ -r, --remove Remove handle
33
+ -t, --test Writes nothing
41
34
 
42
35
  Common options:
36
+ -l, --list List aliases
37
+ -v, --version Show version
43
38
  -h, --help Show this message
44
- --version
45
39
 
46
40
  ## Development
47
41
 
@@ -63,10 +57,9 @@ The gem is available as open source under the terms of the [MIT License](http://
63
57
  ## Missing
64
58
 
65
59
  * All the other options in [ssh documentation](http://linux.die.net/man/5/ssh_config)
66
- * Add -r option to remove aliases
67
60
 
68
61
  ## Credit
69
62
 
70
- Mohamed Ossama who gave me this idea
63
+ Mohamed Osama who gave me this idea
71
64
 
72
65
 
data/lib/sshez.rb CHANGED
@@ -1,163 +1,13 @@
1
- require "sshez/version"
1
+ require 'sshez/params'
2
+ require 'sshez/config_file'
3
+ require 'sshez/exec'
4
+ require 'sshez/version'
2
5
  require 'optparse'
3
6
  require 'ostruct'
4
7
 
5
8
  module Sshez
6
- # Your code goes here...
7
-
8
-
9
- class Params
10
-
11
- #
12
- # Return a structure describing the options.
13
- #
14
- def self.parse(args)
15
- # The options specified on the command line will be collected in *options*.
16
- # We set default values here.
17
- options = OpenStruct.new
18
-
19
- opt_parser = OptionParser.new do |opts|
20
- opts.banner = "Usage: sshez alias (role@host|-r) [options]"
21
-
22
- opts.separator ""
23
- opts.separator "Specific options:"
24
-
25
- # Mandatory argument.
26
- opts.on("-p", "--port PORT",
27
- "Specify a port") do |port|
28
- options.port = port
29
- end
30
-
31
- # Optional argument; multi-line description.
32
- opts.on("-i", "--identity_file [key]",
33
- "Add identity") do |key_path|
34
- options.identity_file = key_path
35
- end
36
-
37
- opts.on("-r", "--remove", "Remove handle") do
38
- options.remove = true
39
- end
40
-
41
- opts.on("-t", "--test", "writes nothing") do
42
- options.test = true
43
- end
44
-
45
- opts.separator ""
46
- opts.separator "Common options:"
47
-
48
- # No argument, shows at tail. This will print an options summary.
49
- # Try it and see!
50
- opts.on_tail("-h", "--help", "Show this message") do
51
- puts opts
52
- end
53
-
54
- # Another typical switch to print the version.
55
- opts.on_tail("--version", "Show version") do
56
- puts ::Version.join('.')
57
- end
58
- end
59
-
60
- opt_parser.parse!(args)
61
- options
62
- end # parse()
63
-
64
- end # class Params
65
-
66
- class ConfigFile
67
- def self.append(name, user, host, options)
68
- output = "Adding\n"
69
- config_append = form(name, user, host, options)
70
- output += "" + config_append
71
- unless options.test
72
- file_path = File.expand_path('~')+"/.ssh/config"
73
- file = File.open(file_path, "a+")
74
- file.write(config_append)
75
- file.close
76
- end
77
- output += "to #{file_path}\n"
78
- output += "try ssh #{name} \n"
79
-
80
- output
81
- end
82
-
83
- def self.form(name, user, host, options)
84
- retuned = "\n"
85
- retuned += "Host #{name}\n"
86
- retuned += " HostName #{host}\n"
87
- retuned += " User #{user}\n"
88
-
89
- if options.port
90
- retuned += " Port #{options.port}\n"
91
- end
92
-
93
- if options.identity_file
94
- retuned += " IdentityFile #{options.identity_file}\n"
95
- end
96
- retuned
97
-
98
- end
99
-
100
- def self.remove(name)
101
- output = ""
102
- started_removing = false
103
- file_path = File.expand_path('~')+"/.ssh/config"
104
- file = File.open(file_path, "r")
105
- new_file = File.open(file_path+"temp", "w")
106
- file.each do |line|
107
- if line.include?("Host #{name}")|| started_removing
108
- if started_removing && line.include?("Host ") && !line.include?(name)
109
- started_removing = false
110
- else
111
- output += line
112
- started_removing = true
113
- end
114
- else
115
- new_file.write(line)
116
- end
117
- end
118
- file.close
119
- new_file.close
120
- File.delete(file_path)
121
- File.rename(file_path+"temp", file_path)
122
-
123
- if output.empty?
124
- return "could not find host (#{name})"
125
- else
126
- return output
127
- end
128
-
129
- end
130
-
131
-
132
-
133
- end
134
-
135
- class Exec
136
-
137
- def process(args)
138
- # parse the params to get the options
139
- if (args.length < 2 || !args[1].include?("@") || args[0][0] == '-')
140
- if args[0] == "-h"
141
- Params.parse(args)
142
- return nil
143
- elsif ['-r', "--remove"].include?(args[1])
144
- return ConfigFile.remove(args[0])
145
- end
146
- output = %Q|Invalid input
147
- Use -h for help|
148
- return output
149
- end
150
- options = Params.parse(args)
151
-
152
-
153
- unless options.remove
154
- user, host = args[1].split("@")
155
- output = ConfigFile.append(args[0], user, host, options)
156
- output + "Done!"
157
- end
158
-
159
- end
160
9
 
10
+ def self.version
11
+ return Sshez::VERSION
161
12
  end
162
-
163
13
  end
@@ -0,0 +1,83 @@
1
+ module Sshez
2
+ class ConfigFile
3
+ FILE_PATH = File.expand_path('~') + "/.ssh/config"
4
+
5
+ def self.append(name, user, host, options)
6
+ output = "Adding\n"
7
+ config_append = form(name, user, host, options)
8
+ output += config_append
9
+ unless options.test
10
+ file = File.open(FILE_PATH, "a+")
11
+ file.write(config_append)
12
+ file.close
13
+ puts "Successfully added `#{name}` as an alias for `#{user}@#{host}`"
14
+ system "chmod 600 #{FILE_PATH}"
15
+ end
16
+ output += "to #{FILE_PATH}\n"
17
+ output += "try ssh #{name} \n"
18
+
19
+ output
20
+ end
21
+
22
+ def self.form(name, user, host, options)
23
+ retuned = "\n"
24
+ retuned += "Host #{name}\n"
25
+ retuned += " HostName #{host}\n"
26
+ retuned += " User #{user}\n"
27
+
28
+ options.file_content.each_pair do |key, value|
29
+ retuned += value
30
+ end
31
+ retuned
32
+
33
+ end
34
+
35
+ def self.remove(name)
36
+ output = ""
37
+ started_removing = false
38
+ file = File.open(FILE_PATH, "r")
39
+ new_file = File.open(FILE_PATH+"temp", "w")
40
+ file.each do |line|
41
+ if line.include?("Host #{name}")|| started_removing
42
+ if started_removing && line.include?("Host ") && !line.include?(name)
43
+ started_removing = false
44
+ else
45
+ output += line
46
+ started_removing = true
47
+ end
48
+ else
49
+ new_file.write(line)
50
+ end
51
+ end
52
+ file.close
53
+ new_file.close
54
+ File.delete(FILE_PATH)
55
+ File.rename(FILE_PATH + "temp", FILE_PATH)
56
+ system "chmod 600 #{FILE_PATH}"
57
+
58
+ if output.empty?
59
+ return "could not find host (#{name})"
60
+ else
61
+ return output
62
+ end
63
+ end
64
+
65
+ def self.list
66
+ file = File.open(FILE_PATH, "r")
67
+ servers = []
68
+ file.each do |line|
69
+ if line.include?("Host ")
70
+ servers << line.sub('Host', '')
71
+ end
72
+ end
73
+ file.close
74
+ if servers.empty?
75
+ puts "No aliases added"
76
+ else
77
+ puts "Listing aliases:"
78
+ servers.each{|x| puts "\t- #{x}"}
79
+ end
80
+ return servers.join(',')
81
+ end
82
+ end
83
+ end
data/lib/sshez/exec.rb ADDED
@@ -0,0 +1,42 @@
1
+ module Sshez
2
+ class Exec
3
+ def process(args)
4
+ # parse the params to get the options
5
+ if args.length == 0
6
+ args[0] = '-h'
7
+ elsif args[0] == 'list'
8
+ return ConfigFile.list
9
+ elsif args[0] == 'remove'
10
+ if args[1]
11
+ return ConfigFile.remove(args[1])
12
+ else
13
+ output %Q|Select an alias to remove `sshez remove alias`.\n Use `sshez list` to list your aliases|
14
+ puts output
15
+ return output
16
+ end
17
+ end
18
+
19
+ if (args.length < 2 || !args[1].include?("@") || args[0][0] == '-')
20
+ if args.any? { |x| ['-h', '--help', '-l', '--list'].include?(x) }
21
+ Params.parse(args)
22
+ return
23
+ elsif ['-r', "--remove"].include?(args[1])
24
+ return ConfigFile.remove(args[0])
25
+ elsif ['-v', '--version'].include?(args[0])
26
+ puts Sshez.version
27
+ return Sshez.version
28
+ end
29
+ output = %Q|Invalid input. Use -h for help|
30
+ puts output
31
+ return output
32
+ end
33
+ options = Params.parse(args)
34
+
35
+ unless options.remove
36
+ user, host = args[1].split("@")
37
+ output = ConfigFile.append(args[0], user, host, options)
38
+ output + "Done!"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,64 @@
1
+ module Sshez
2
+ class Params
3
+ #
4
+ # Return a structure describing the options.
5
+ #
6
+ def self.parse(args)
7
+ # The options specified on the command line will be collected in *options*.
8
+ # We set default values here.
9
+ options = OpenStruct.new
10
+ options.file_content = OpenStruct.new
11
+
12
+ opt_parser = OptionParser.new do |opts|
13
+ opts.banner = "Usage:\n\tsshez <alias> (role@host|-r) [options]\n\tsshez remove <alias>\n\tsshez list"
14
+
15
+ opts.separator ""
16
+ opts.separator "Specific options:"
17
+
18
+ # Mandatory argument.
19
+ opts.on("-p", "--port PORT",
20
+ "Specify a port") do |port|
21
+ options.port = port
22
+ options.file_content.port_text = " Port #{options.port}\n"
23
+ end
24
+
25
+ # Optional argument; multi-line description.
26
+ opts.on("-i", "--identity_file [key]",
27
+ "Add identity") do |key_path|
28
+ options.identity_file = key_path
29
+ options.file_content.identity_file_text = " IdentityFile #{options.identity_file}\n"
30
+ end
31
+
32
+ opts.on("-r", "--remove", "Remove handle") do
33
+ options.remove = true
34
+ end
35
+
36
+ opts.on("-t", "--test", "Writes nothing") do
37
+ options.test = true
38
+ end
39
+
40
+ opts.separator ""
41
+ opts.separator "Common options:"
42
+
43
+ opts.on("-l", "--list", "List aliases") do
44
+ Sshez::ConfigFile.list
45
+ return
46
+ end
47
+
48
+ # Another typical switch to print the version.
49
+ opts.on("-v", "--version", "Show version") do
50
+ puts Sshez.version
51
+ return
52
+ end
53
+
54
+ opts.on_tail("-h", "--help", "Show this message") do
55
+ puts opts
56
+ puts "\n"
57
+ end
58
+ end
59
+
60
+ opt_parser.parse!(args)
61
+ options
62
+ end # parse()
63
+ end # class Params
64
+ end
data/lib/sshez/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sshez
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'sshez'
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sshez do
4
+
5
+ subject { Sshez::Exec.new }
6
+ it 'has a version number' do
7
+ expect(Sshez::VERSION).not_to be nil
8
+ end
9
+
10
+ describe '#process' do
11
+ let(:input) { 'google root@74.125.224.72 -p 80 -t' }
12
+ let(:output) { subject.process(input.split(" ")) }
13
+
14
+ it 'begins with what it does' do
15
+ expect(output).to start_with "Adding"
16
+ end
17
+
18
+ it 'prints what it appends' do
19
+ expect(output).to match /Host google/i
20
+ expect(output).to match /HostName 74.125.224.72/i
21
+ expect(output).to match /Port 80/i
22
+ end
23
+
24
+ it 'always appends "Done" if succeeds' do
25
+ expect(output).to end_with 'Done!'
26
+ end
27
+ end
28
+
29
+ describe "fails" do
30
+ let(:input) { 'root@74.125.224.72' }
31
+ let(:output) { subject.process(input.split(" ")) }
32
+
33
+ it 'and printes start' do
34
+ expect(output).to start_with "Invalid input"
35
+ end
36
+
37
+ it 'and then asks for help at the end' do
38
+ expect(output).to end_with 'Use -h for help'
39
+ end
40
+
41
+ end
42
+
43
+ describe "help works" do
44
+ let(:input) { '-h' }
45
+ let(:output) { subject.process(input.split(" ")) }
46
+
47
+ it 'prints but outputs nothing' do
48
+ expect(output).to eq nil
49
+ end
50
+ end
51
+
52
+ describe "remove" do
53
+ before { subject.process(%w{google root@74.12.32.42 -p 30}) }
54
+ let(:input) { "google -r" }
55
+ let(:output) { subject.process(input.split(" ")) }
56
+
57
+ it 'ends with 30' do
58
+ expect(output).to end_with "30\n"
59
+ end
60
+ end
61
+
62
+ describe "list works" do
63
+ before { subject.process(%w{google root@74.12.32.42 -p 30}) }
64
+ let(:input) { "list" }
65
+ let(:output) { subject.process(input.split(" ")) }
66
+ after { subject.process(%w{google -r}) }
67
+
68
+ it 'contains added alias' do
69
+ expect(output).to end_with "google\n"
70
+ end
71
+ end
72
+
73
+ describe "version works" do
74
+ let(:input) { "-v" }
75
+ let(:output) { subject.process(input.split(" ")) }
76
+
77
+ it 'matches gem version' do
78
+ expect(output).to end_with "#{Sshez.version}"
79
+ end
80
+ end
81
+ end
data/sshez.gemspec CHANGED
@@ -6,22 +6,21 @@ require 'sshez/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "sshez"
8
8
  spec.version = Sshez::VERSION
9
- spec.authors = ["Immortal Friday"]
10
- spec.email = ["khaled.gomaa.90@gmail.com"]
9
+ spec.authors = ["Immortal Friday", "Oss"]
10
+ spec.email = ["khaled.gomaa.90@gmail.com", "mohamed.o.alnagdy@gmail.com"]
11
11
 
12
12
  spec.summary = %q{Easy ssh config handling}
13
- spec.description = %q{will interface your ssh config file
14
- Allowing you to add aliases to your server and do a lot of cool stuff}
13
+ spec.description = %q{No more remembering ips users or ports! Sshez interfaces your ssh config file
14
+ Allowing you to add aliases for your ssh connections to easily access them.}
15
15
  spec.homepage = "https://github.com/GomaaK/sshez"
16
16
  spec.license = "MIT"
17
17
 
18
-
19
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
- spec.executables = ["sshez"]
19
+ spec.test_files = Dir["spec/**/*"]
21
20
  spec.require_paths = ["lib"]
21
+ spec.executables = ["sshez"]
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.10"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
- spec.add_development_dependency "rspec"
26
-
25
+ spec.add_development_dependency "rspec", "~> 3.4"
27
26
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshez
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Immortal Friday
8
+ - Oss
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-12-06 00:00:00.000000000 Z
12
+ date: 2015-12-14 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -42,21 +43,22 @@ dependencies:
42
43
  name: rspec
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
- - - ">="
46
+ - - "~>"
46
47
  - !ruby/object:Gem::Version
47
- version: '0'
48
+ version: '3.4'
48
49
  type: :development
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - ">="
53
+ - - "~>"
53
54
  - !ruby/object:Gem::Version
54
- version: '0'
55
+ version: '3.4'
55
56
  description: |-
56
- will interface your ssh config file
57
- Allowing you to add aliases to your server and do a lot of cool stuff
57
+ No more remembering ips users or ports! Sshez interfaces your ssh config file
58
+ Allowing you to add aliases for your ssh connections to easily access them.
58
59
  email:
59
60
  - khaled.gomaa.90@gmail.com
61
+ - mohamed.o.alnagdy@gmail.com
60
62
  executables:
61
63
  - sshez
62
64
  extensions: []
@@ -65,6 +67,7 @@ files:
65
67
  - ".gitignore"
66
68
  - ".rspec"
67
69
  - ".travis.yml"
70
+ - CHANGELOG.md
68
71
  - CODE_OF_CONDUCT.md
69
72
  - Gemfile
70
73
  - LICENSE
@@ -75,7 +78,12 @@ files:
75
78
  - bin/setup
76
79
  - bin/sshez
77
80
  - lib/sshez.rb
81
+ - lib/sshez/config_file.rb
82
+ - lib/sshez/exec.rb
83
+ - lib/sshez/params.rb
78
84
  - lib/sshez/version.rb
85
+ - spec/spec_helper.rb
86
+ - spec/sshez_spec.rb
79
87
  - sshez.gemspec
80
88
  - tasks/rspec.rake
81
89
  homepage: https://github.com/GomaaK/sshez
@@ -102,4 +110,6 @@ rubygems_version: 2.4.5
102
110
  signing_key:
103
111
  specification_version: 4
104
112
  summary: Easy ssh config handling
105
- test_files: []
113
+ test_files:
114
+ - spec/sshez_spec.rb
115
+ - spec/spec_helper.rb