xlogin 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 100ba79a46574b64637b2dff6480b75353acc21d
4
+ data.tar.gz: a9aa7094fed1a2a3d6b7515e4cfcb5bf94f211fb
5
+ SHA512:
6
+ metadata.gz: 80ba1c2c142dd80fe5c8291fe95baf927d063ee6089489957b6c57011851074de96f3cac6e5fa1f1bdf1fc3a6d4df958c54c29d345689f6239301542d61498f5
7
+ data.tar.gz: 6deea9b37a0d41d3c2794be95a93d0b6a484f6495887d86b0b67b85b05dc8dcfc2a98e6109c19d10bcf1845a4cd05945c912c02d3516b19cc967462580143c7d
data/.gitignore ADDED
@@ -0,0 +1,58 @@
1
+
2
+ # Created by https://www.gitignore.io/api/ruby
3
+
4
+ ### Ruby ###
5
+ *.gem
6
+ *.rbc
7
+ /.config
8
+ /coverage/
9
+ /InstalledFiles
10
+ /pkg/
11
+ /spec/reports/
12
+ /spec/examples.txt
13
+ /test/tmp/
14
+ /test/version_tmp/
15
+ /tmp/
16
+
17
+ # Used by dotenv library to load environment variables.
18
+ # .env
19
+
20
+ ## Specific to RubyMotion:
21
+ .dat*
22
+ .repl_history
23
+ build/
24
+ *.bridgesupport
25
+ build-iPhoneOS/
26
+ build-iPhoneSimulator/
27
+
28
+ ## Specific to RubyMotion (use of CocoaPods):
29
+ #
30
+ # We recommend against adding the Pods directory to your .gitignore. However
31
+ # you should judge for yourself, the pros and cons are mentioned at:
32
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
33
+ #
34
+ # vendor/Pods/
35
+
36
+ ## Documentation cache and generated files:
37
+ /.yardoc/
38
+ /_yardoc/
39
+ /doc/
40
+ /rdoc/
41
+
42
+ ## Environment normalization:
43
+ /.bundle/
44
+ /vendor/bundle
45
+ /lib/bundler/man/
46
+
47
+ # for a library or gem, you might want to ignore these files since the code is
48
+ # intended to run in multiple environments; otherwise, check them in:
49
+ # Gemfile.lock
50
+ # .ruby-version
51
+ # .ruby-gemset
52
+
53
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
54
+ .rvmrc
55
+
56
+ # End of https://www.gitignore.io/api/ruby
57
+ /lib/xlogin/firmwares/*
58
+ !/lib/xlogin/firmwares/vyos.rb
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at haccht@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xlogin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 haccht
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # xlogin
2
+ rancid clogin alternative.
3
+
4
+ ## Usage
5
+
6
+ ネットワークデバイスへのログインを自動化するツール群。
7
+ `xlogin/firmares`にファームウェア仕様を記述することで対象機器を拡大可能。
8
+
9
+
10
+ 各個別装置毎のログインのための認証情報は`~/.xloginrc`へ記述しておく。
11
+ `.xloginrc`のフォーマットはDSL形式で下記の通り
12
+
13
+ ~~~
14
+ #hosttype hostname telnet_uri_scheme options
15
+ vyos 'vyos01', 'telnet://vagrant:vagrant@127.0.0.1:2200'
16
+ vyos 'vyos02', 'telnet://vagrant:vagrant@127.0.0.1:2201'
17
+ ~~~
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'xlogin'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install xlogin
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xlogin. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/cmd_exec ADDED
@@ -0,0 +1,122 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'readline'
5
+ require 'stringio'
6
+ require 'thread'
7
+ require 'xlogin'
8
+
9
+ module Xlogin
10
+
11
+ class ThreadPool
12
+
13
+ def initialize(size)
14
+ @jobs = Array.new
15
+ @lock = Queue.new
16
+
17
+ @size = size
18
+ @size.times { @lock.push :token }
19
+
20
+ begin yield(self) ensure join end if block_given?
21
+ end
22
+
23
+ def run
24
+ @jobs << Thread.new do
25
+ token = @lock.pop
26
+ yield
27
+ @lock.push token
28
+ end
29
+ end
30
+
31
+ def join
32
+ @jobs.each { |job| job.join }
33
+ end
34
+
35
+ end
36
+
37
+ class CLI
38
+
39
+ def self.getopts(args)
40
+ options = Hash.new
41
+
42
+ opt = OptionParser.new
43
+ opt.on('-e', '--enable', 'Try to gain enable priviledge.') { |v| options[:e] = v }
44
+ opt.on('-l', '--log', 'Enable logging.') { |v| options[:l] = v }
45
+ opt.on('-H', 'Display hostnames for each response.') { |v| options[:H] = v }
46
+
47
+ opt.on('-f FILE', 'Read target hostnames from FILE.') { |v| options[:f] = v }
48
+ opt.on('-r FILE', 'Load extention library.') { |v| options[:r] = v }
49
+
50
+ opt.on('-x VALUE', 'Read commands from VALUE.') { |v| options[:x] = v }
51
+ opt.on('-c VALUE') { |v| options[:c] = v }
52
+
53
+ opt.on('-p VALUE', 'Specify concurrency pool size.') { |v| options[:p] = v }
54
+ opt.on('-i VALUE', 'Specify interval time [sec].') { |v| options[:i] = v }
55
+
56
+ opt.banner += ' HOST...'
57
+ opt.summary_width = 16
58
+
59
+ self.class.module_eval do
60
+ define_method(:usage) do |msg = nil|
61
+ puts opt.to_s
62
+ puts "error: #{msg}" if msg
63
+ exit 1
64
+ end
65
+ end
66
+
67
+ opt.parse!(args)
68
+ return options, args
69
+ end
70
+
71
+ def self.run(args = ARGV)
72
+ options, args = getopts(args)
73
+ usage if options[:x].nil? and options[:c].nil?
74
+ usage if options[:f].nil? && args.empty?
75
+
76
+ if options[:r]
77
+ options[:r].split(':').each { |file| require file }
78
+ end
79
+
80
+ size = (options[:p] || 1).to_i
81
+ interval = (options[:i] || 0).to_i
82
+ commands = ((options[:x] == '-') ? $stdin.read : options[:x]).split(/[;\n]/) if options[:x]
83
+
84
+ nodes = args.dup
85
+ nodes += IO.readlines(options[:f]).map(&:chomp) if options[:f]
86
+
87
+ ThreadPool.new(size) do |pool|
88
+ nodes.each do |node|
89
+ next if node =~ /^\s*#/
90
+
91
+ pool.run do
92
+ begin
93
+ resp = StringIO.new
94
+
95
+ loggers = []
96
+ loggers << $stdout unless size > 1
97
+ loggers << (URI.regexp =~ node ? URI(node).host : node) + ".log" if options[:l]
98
+
99
+ session = Xlogin.get(node, log: loggers)
100
+ session.enable if options[:e] && session.respond_to?(:enable)
101
+
102
+ if options[:x]
103
+ ['', *commands].map { |command| resp.print session.cmd(command) }
104
+ elsif options[:c]
105
+ resp.puts session.send(options[:c].to_sym)
106
+ end
107
+
108
+ content = resp.string
109
+ content = content.lines.map { |line| "#{node}: #{line}" }.join if options[:H]
110
+
111
+ $stdout.puts content if size > 1
112
+ rescue => e
113
+ $stderr.puts "Something goes wrong with '#{node}' - #{e}"
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ Xlogin::CLI.run if $0 == __FILE__
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xlogin"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/xlogin ADDED
@@ -0,0 +1,67 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'readline'
5
+ require 'xlogin'
6
+
7
+ module Xlogin
8
+ class CLI
9
+ def self.getopts(args)
10
+ options = Hash.new
11
+
12
+ opt = OptionParser.new
13
+ opt.on('-e', '--enable', 'Try to gain enable priviledge.') { |v| options[:e] = v }
14
+ opt.on('-l', '--log', 'Enable logging.') { |v| options[:l] = v }
15
+ opt.on('-r FILE', 'Load extention library.') { |v| options[:r] = v }
16
+
17
+ opt.on( '--list', 'List all devices.') { |v| options[:list] = v }
18
+
19
+ opt.banner += ' HOST'
20
+ opt.summary_width = 16
21
+
22
+ self.class.module_eval do
23
+ define_method(:usage) do |msg = nil|
24
+ puts opt.to_s
25
+ puts "error: #{msg}" if msg
26
+ exit 1
27
+ end
28
+ end
29
+
30
+ opt.parse!(args)
31
+ return options, args
32
+ end
33
+
34
+ def self.run(args = ARGV)
35
+ options, args = getopts(args)
36
+
37
+ if options[:r]
38
+ options[:r].split(':').each { |file| require file }
39
+ end
40
+
41
+ if options[:list]
42
+ factory = FirmwareFactory.new
43
+ puts factory.list.map { |e| "#{e[:name]}\t#{e[:type]}" }
44
+ exit 0
45
+ end
46
+
47
+ target = args.shift
48
+ usage unless target
49
+
50
+ loggers = [$stdout]
51
+ loggers.push("#{target}.log") if options[:l]
52
+
53
+ puts "Trying #{target}..."
54
+ puts "Escape character is '^]'."
55
+
56
+ session = Xlogin.get(target, log: loggers)
57
+ session.enable if options[:e] && session.respond_to?(:enable)
58
+ session.interact!
59
+ rescue => e
60
+ $stderr.puts("#{e}\n\n")
61
+ raise
62
+ end
63
+
64
+ end
65
+ end
66
+
67
+ Xlogin::CLI.run if $0 == __FILE__
data/lib/xlogin.rb ADDED
@@ -0,0 +1,50 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'xlogin/firmware'
4
+ require 'xlogin/firmware_factory'
5
+ require 'xlogin/version'
6
+
7
+ module Xlogin
8
+
9
+
10
+ SourceDir = [
11
+ File.join(File.dirname(__FILE__), 'xlogin'),
12
+ ENV['HOME'],
13
+ ENV['XLOGIN_HOME'],
14
+ Dir.pwd
15
+ ]
16
+
17
+ class GeneralError < StandardError; end
18
+
19
+ class << self
20
+ def configure(name)
21
+ firmware = Xlogin::FirmwareFactory[name] || Xlogin::Firmware.new
22
+ yield firmware if block_given?
23
+
24
+ Xlogin::FirmwareFactory.register(name, firmware)
25
+ end
26
+
27
+ def alias(new_name, original_name)
28
+ firmware = Xlogin::FirmwareFactory[original_name]
29
+ raise Xlogin::GeneralError.new("'#{original_name}' not found") unless firmware
30
+
31
+ Xlogin::FirmwareFactory.register(new_name, firmware)
32
+ end
33
+
34
+ def get(nodename, opts = {})
35
+ @factory ||= Xlogin::FirmwareFactory.new
36
+ session = @factory.build(nodename, opts)
37
+
38
+ if block_given?
39
+ begin yield session ensure session.close end
40
+ else
41
+ session
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ Directory = File.join(File.dirname(__FILE__), 'xlogin', 'firmwares')
48
+ Xlogin::FirmwareFactory.register_dir(Directory)
49
+
50
+ end