net-ssh-xlogin 0.1.4 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77c1fda96670f9f13507600f76ce45f27ca0471873d71fdca476f1920e59a3ff
4
- data.tar.gz: 1edb07fce74f21a418d14b11151e81198510ea8d3872cd74aa3fa167242cd323
3
+ metadata.gz: 18c8fc6e098ac2b9b1ed5afa083f7a7afb4fa1ce04b9d8fb5dfa183e3cd5782c
4
+ data.tar.gz: 686efa8e6793a4eece0a0440d70ab622c6ee175bb6555d7e6e1b152cf9da517a
5
5
  SHA512:
6
- metadata.gz: e3b8986b8bad4a84c9a03039ece04ecdf2950557cff3bf9b6add3b5c18edc3264e4523947e06587e01fba8d8f3214febcc20a6f09b350495271bef417e37770d
7
- data.tar.gz: 101d59331e00c077d63eee827113cf6110df4085f383ebb989492daad7e932373bc308c0102e3c7ecbc99f3c5abde24dbb061b13e7a55f5b20151bf761f57073
6
+ metadata.gz: 2009700c3d15ec329126bc626f673b3e7eec66f6c47de9d0c6d3a53121d08d97058dc5a8cec7718f6dbb47474afa06add8083ca7f03334d7995ff2e3e8c9e555
7
+ data.tar.gz: f2c166bdd18fbae04e301ac1bad0e1929b536887e1be5004406ef477e2834a91baead80cfb4349d717af79d7e15a1a875c1b589fa7cbbc4c1f60b961e8a33b80
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- net-ssh-xlogin (0.1.4)
4
+ net-ssh-xlogin (0.2.0)
5
5
  bcrypt_pbkdf
6
6
  ed25519
7
7
  net-ssh
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # Net::Ssh::Xlogin
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/net/ssh/xlogin`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
3
 
7
4
  ## Installation
8
5
 
@@ -22,7 +19,54 @@ Or install it yourself as:
22
19
 
23
20
  ## Usage
24
21
 
25
- TODO: Write usage instructions here
22
+ ```: ruby
23
+ require 'net/ssh/xlogin'
24
+
25
+ Net::SSH::Xlogin.configure do
26
+ source "centos7 'exsample-server', 'ssh://vagrant:vagrant@localhost', port: 2222"
27
+ template(type: :centos7){}
28
+ end
29
+
30
+
31
+ s = Net::SSH::Xlogin.get('exsample-server', log: 'test.log')
32
+
33
+ puts s.cmd('ls')
34
+ puts s.cmd('hostname')
35
+ puts s.iplist
36
+ s.close
37
+
38
+
39
+ #=> ==== output ls ====
40
+ #=> ls
41
+ #=> [vagrant@exsample-server ~]$
42
+ #=>
43
+ #=> ==== output hostname ====
44
+ #=> hostname
45
+ #=> exsample-server
46
+ #=> [vagrant@exsample-server ~]$
47
+ #=>
48
+ #=> ==== output ifconfig ====
49
+ #=> ifconfig
50
+ #=> enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
51
+ #=> inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
52
+ #=> inet6 fe80::1f59:5849:f41f:7979 prefixlen 64 scopeid 0x20<link>
53
+ #=> ether 08:00:27:37:f8:46 txqueuelen 1000 (Ethernet)
54
+ #=> RX packets 777 bytes 99545 (97.2 KiB)
55
+ #=> RX errors 0 dropped 0 overruns 0 frame 0
56
+ #=> TX packets 565 bytes 114880 (112.1 KiB)
57
+ #=> TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
58
+ #=>
59
+ #=> lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
60
+ #=> inet 127.0.0.1 netmask 255.0.0.0
61
+ #=> inet6 ::1 prefixlen 128 scopeid 0x10<host>
62
+ #=> loop txqueuelen 1 (Local Loopback)
63
+ #=> RX packets 68 bytes 5524 (5.3 KiB)
64
+ #=> RX errors 0 dropped 0 overruns 0 frame 0
65
+ #=> TX packets 68 bytes 5524 (5.3 KiB)
66
+ #=> TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
67
+ #=>
68
+ #=> [vagrant@exsample-server ~]$
69
+ ```
26
70
 
27
71
  ## Development
28
72
 
@@ -0,0 +1,13 @@
1
+ # exsample template file
2
+ #
3
+ # - usage
4
+ # Net::SSH::Xlogin.configure do
5
+ # template 'centos7.rb'
6
+ # end
7
+
8
+
9
+ prompt(/[$%#>] ?\z/n)
10
+
11
+ bind(:iplist) do
12
+ cmd('ipconfig')
13
+ end
data/exsampes/xloginrc ADDED
@@ -0,0 +1,9 @@
1
+ # exsample source file
2
+ #
3
+ # - usage
4
+ # Net::SSH::Xlogin.configure do
5
+ # source 'xloginrc'
6
+ # end
7
+
8
+ centos7 'exsample-server', 'ssh://vagrant:vagrant@localhost', port: 2222
9
+ centos7 'exsample-server2', 'ssh://vagrant:vagrant@localhost', port: 2222, log: 'test.log'
@@ -0,0 +1,4 @@
1
+ - host: exsampe-server
2
+ host_name: localhost
3
+ password: vagrant
4
+ log: test.log
@@ -2,37 +2,64 @@ require 'yaml'
2
2
 
3
3
  require 'net/ssh/xlogin/version'
4
4
  require 'net/ssh/xlogin/factory'
5
- require 'net/ssh/xlogin/connection'
5
+ require 'net/ssh/xlogin/template'
6
6
 
7
7
  module Net::SSH::Xlogin
8
8
  class Error < StandardError; end
9
9
  class << self
10
- def get(name)
11
- session = Connection.new(name)
10
+ def get(name, **opts)
11
+ opts = factory.inventory[name].merge(opts)
12
+ type = opts[:type]
13
+ klass = factory.templates[type]
14
+ klass.build(name, **opts)
12
15
  end
13
16
 
14
- def configure(&bk)
15
- instance_eval(&bk)
17
+ def configure(&block)
18
+ instance_eval(&block)
16
19
  end
17
20
 
18
- def config(file)
19
- yaml = case file
20
- when /\.y(a)?ml$/
21
- raise Error, 'not cofnig file' unless File.exist?(file)
22
- YAML.load_file(file)
23
- when String
24
- YAML.load(YAML.dump(file))
25
- when Hash
26
- file
27
- when YAML
28
- YAML.dump(file)
29
- end
30
- factory.create(yaml)
21
+ def source(*args)
22
+ args.each do |src|
23
+ src = case src
24
+ when /\.y(a)?ml$/
25
+ factory.yaml(src)
26
+ when String
27
+ if File.exist?(src)
28
+ factory.dsl(IO.read(src))
29
+ else
30
+ factory.dsl(src)
31
+ end
32
+ when Hash
33
+ name = src.delete(:host)
34
+ factory.set_source(name, **src)
35
+ end
36
+ end
31
37
  end
32
38
 
39
+ def template(*templates, **opts, &block)
40
+ name = opts[:type]
41
+
42
+ if block
43
+ template = factory.templates[name] || Template.new(name)
44
+ template.instance_eval(&block)
45
+ factory.templates[name] = template
46
+ return
47
+ end
48
+
49
+ templates = templates.map{|path| Dir.exist?(path) ? Dir.glob(File.join(path, '*.rb')) : path }.flatten
50
+ templates.each do |path|
51
+ name ||= File.basename(path, '.rb').scan(/\w+/).join('_').to_sym
52
+ text = IO.read(path)
53
+ template = factory.templates[name] || Template.new(name)
54
+ template.instance_eval(text)
55
+
56
+ factory.templates[name] = template
57
+ end
58
+ end
59
+
60
+ private
33
61
  def factory
34
62
  @factory ||= Factory.instance
35
63
  end
36
-
37
64
  end
38
65
  end
@@ -1,34 +1,50 @@
1
- require 'net/ssh/xlogin/factory'
2
- require 'net/ssh/telnet'
3
1
  require 'net/ssh'
2
+ require 'net/ssh/telnet'
3
+ require 'net/ssh/xlogin/factory'
4
+
4
5
  module Net::SSH::Xlogin
5
6
  class Connection < Net::SSH::Telnet
6
- attr_reader :name, :user
7
- def initialize(name, **opt)
8
- @name = name
9
- @config = Net::SSH::Xlogin.factory.get(name)
10
- @user = @config.delete(:user)
11
- @ssh_options = @config.slice(*Net::SSH::VALID_OPTIONS, :password)
12
- @ssh = Net::SSH.start(@name, @user, @ssh_options.merge(config: true))
13
-
14
- args = Hash.new
15
- max_retry = opt[:retry ] || 1
16
- timeout = opt[:timeout] || 10
7
+
8
+ attr_reader :name
9
+
10
+ def initialize(template, name, **opts)
11
+ @name = name
12
+ @options = opts
13
+
14
+ log = @options[:log] || @options[:output_log]
15
+ max_retry = @options[:max_retry] || 1
16
+
17
+ @options[:timeout] ||= template.timeout
18
+ @options[:host_name] ||= name
17
19
 
18
20
  begin
19
- args['Timeout'] = timeout
20
- args['FailEOF'] = true
21
- args['Session'] = @ssh
21
+ args = Hash.new
22
+ args["Output_log"] = log if log
23
+ args["Dump_log"] = @options[:dump_log] if @options[:dump_log]
24
+ args["Prompt"] = @options[:prompt] || template.prompt_patten
25
+ args["Timeout"] = @options[:timeout] || 10
26
+ args["Waittime"] = @options[:waittime] || 0
27
+ args["Terminator"] = @options[:terminator] || LF
28
+ args["Binmode"] = @options[:binmode] || false
29
+ args["PTYOptions"] = @options[:ptyoptions] || {}
30
+
31
+ if @options[:proxy]
32
+ args["Host"] = @options[:host_name]
33
+ args["Port"] = @options[:port]
34
+ args["Username"] = @options[:user]
35
+ args["Password"] = @options[:password]
36
+ args["Proxy"] = @options[:proxy]
37
+ else
38
+ ssh_options = @options.slice(*Net::SSH::VALID_OPTIONS)
39
+ ssh_options = @options[:host] unless @options[:host_name]
40
+ args['Session'] = Net::SSH.start(nil, nil, ssh_options)
41
+ end
42
+
22
43
  super(args)
23
44
  rescue => e
24
45
  retry if (max_retry -= 1) > 0
25
46
  raise e
26
47
  end
27
48
  end
28
-
29
- def cmd(*args)
30
- res = super
31
- res.gsub(/\e\[\d{1,3}[mK]|\e\[\d{1,3};\d{1,3}[mK]|\e\]\d{1,3};/, '')
32
- end
33
49
  end
34
50
  end
@@ -1,27 +1,52 @@
1
1
  require 'singleton'
2
+ require 'uri'
2
3
 
3
4
  module Net::SSH::Xlogin
4
5
  class Factory
5
6
  include Singleton
7
+ attr_accessor :inventory
8
+ attr_accessor :templates
9
+
6
10
  def initialize
7
11
  @inventory = Hash.new
12
+ @templates = Hash.new
8
13
  end
9
14
 
10
- def create(yaml)
11
- yaml.each do |name, info|
12
- name = name
13
- info = info.reduce({}){|h, (k, v)| h = h.merge(k.to_sym => v)}
14
- set name, info
15
- end
15
+ def set_source(name, **args)
16
+ @inventory[name] = args
16
17
  end
17
18
 
18
- def set(name, args)
19
- @inventory[name] = args
19
+ def set_template(name, klass)
20
+ @templates[name] = klass
20
21
  end
21
22
 
22
- def get(name)
23
- @inventory[name]
23
+ def yaml(file)
24
+ raise Error, 'not cofnig file' unless File.exist?(file)
25
+ srcs = YAML.load_file(file)
26
+ srcs.each do |src|
27
+ src = src.inject({}){|h, (k,v)| h = h.merge(k.sym => v) }
28
+ name = src.delete(:host)
29
+ factory.source_set(name, **src)
30
+ end
24
31
  end
25
32
 
33
+ def dsl(str)
34
+ instance_eval(str)
35
+ end
36
+
37
+ def method_missing(method, *args, **options)
38
+ super unless args.size == 2
39
+
40
+ name = args.shift
41
+ uri = URI.parse(args.shift)
42
+
43
+ options[:type] = method
44
+ options[:uri] = uri
45
+ options[:host_name] = uri.host
46
+ options[:user] = uri.user
47
+ options[:password] = uri.password
48
+
49
+ set_source(name, **options)
50
+ end
26
51
  end
27
52
  end
@@ -0,0 +1,42 @@
1
+ require 'net/ssh/xlogin/connection'
2
+
3
+ module Net::SSH::Xlogin
4
+ class Template
5
+
6
+ DEFAULT_TIMEOUT = 10
7
+ DEFAULT_PROMPT = /[$%#>] ?\z/n
8
+
9
+ attr_reader :methods
10
+ attr_reader :prompts
11
+ attr_reader :timeout
12
+
13
+ def initialize(name)
14
+ @name = name
15
+ @prompts = Array.new
16
+ @timeout = DEFAULT_TIMEOUT
17
+ @methods = Hash.new
18
+ end
19
+
20
+ def prompt(expect = nil, &block)
21
+ return [[DEFAULT_PROMPT, nil]] if expect.nil? && @prompts.empty?
22
+ @prompts << [Regexp.new(expect.to_s), block] if expect
23
+ @prompts
24
+ end
25
+
26
+ def prompt_patten
27
+ Regexp.union(prompt.map(&:first))
28
+ end
29
+
30
+ def bind(name, &block)
31
+ @methods[name] = block
32
+ end
33
+
34
+ def build(name, **opts)
35
+ klass = Class.new(Net::SSH::Xlogin.const_get('Connection'))
36
+ klass.class_exec(self) do |template|
37
+ template.methods.each {|name, block| define_method(name, &block) }
38
+ end
39
+ klass.new(self, name, **opts)
40
+ end
41
+ end
42
+ end
@@ -1,7 +1,7 @@
1
1
  module Net
2
2
  module SSH
3
3
  module Xlogin
4
- VERSION = '0.1.4'
4
+ VERSION = '0.2.0'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-ssh-xlogin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ito.toshifumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-09 00:00:00.000000000 Z
11
+ date: 2019-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -108,9 +108,13 @@ files:
108
108
  - Rakefile
109
109
  - bin/console
110
110
  - bin/setup
111
+ - exsampes/centos.rb
112
+ - exsampes/xloginrc
113
+ - exsampes/xloginrc.yml
111
114
  - lib/net/ssh/xlogin.rb
112
115
  - lib/net/ssh/xlogin/connection.rb
113
116
  - lib/net/ssh/xlogin/factory.rb
117
+ - lib/net/ssh/xlogin/template.rb
114
118
  - lib/net/ssh/xlogin/version.rb
115
119
  - net-ssh-xlogin.gemspec
116
120
  homepage: https://github.com/Gen-Arch/net-ssh-xlogin