expect4r 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/expect/io.rb CHANGED
@@ -248,8 +248,8 @@ module Expect4r
248
248
  _login_ :proxy=> proxy
249
249
  end
250
250
  alias :login_via :login_by_proxy
251
-
252
- def login(cmd, arg={})
251
+
252
+ def _login(cmd, arg={})
253
253
 
254
254
  return if connected?
255
255
 
@@ -324,6 +324,9 @@ module Expect4r
324
324
  [buf, ev]
325
325
  end
326
326
 
327
+ alias :login :_login
328
+
329
+
327
330
  private
328
331
 
329
332
  def login_proxy(cmd, arg)
@@ -482,165 +485,3 @@ module Kernel
482
485
  end
483
486
  exp_debug :disable
484
487
  end
485
-
486
- module Expect4r
487
- class Base
488
- include Expect4r
489
- class << self
490
- attr_reader :routers
491
- def all
492
- @arr
493
- end
494
- def add(r)
495
- @arr ||=[]
496
- @arr << r
497
- end
498
- end
499
- def initialize(*args)
500
- @matches = Set.new
501
- Base.add(self)
502
- self
503
- end
504
- end
505
-
506
- class BaseLoginObject < Base
507
- class << self
508
- # Examples:
509
- # my_mac = RShell.new_telnet '1.1.1.1', 'me', 'secret'
510
- # ios = Ios.new_telnet '1.1.1.1', 'lab', 'lab'
511
- # iox = Iox.new_telnet '1.1.1.1', 'username', 'pwd'
512
- #
513
- def new_telnet(*args)
514
- new :telnet, *args
515
- end
516
- # Examples:
517
- # my_mac = RShell.new_ssh '1.1.1.1', 'me', 'secret'
518
- # iox = Ios.new_ssh '1.1.1.1', 'lab', 'lab'
519
- # ios = Iosx.new_ssh '1.1.1.1', 'username', 'pwd'
520
- #
521
- def new_ssh(*args)
522
- new :ssh, *args
523
- end
524
- attr_reader :routers
525
- def add(r)
526
- @routers ||=[]
527
- @routers << r
528
- end
529
- end
530
- attr_reader :host, :user, :method, :port, :proxy
531
- alias :username :user
532
- alias :hostname :host
533
- # Adds a login to a Expect4r::BaseLoginObject"
534
- #
535
- # Constructor:
536
- # * <tt>new</tt> <method>, <username>, <password>, [port_number]
537
- # * <tt>new</tt> <method>, options={}
538
- #
539
- # Options are:
540
- # * <tt>:host</tt> or <tt>:hostname</tt>
541
- # * <tt>:user</tt> or <tt>:username</tt>
542
- # * <tt>:pwd</tt> or <tt>:password</tt>
543
- #
544
- # Examples:
545
- # new :telnet, :host=> '1.1.1.1', :user=> 'lab', :password=>'lab'
546
- # new :ssh, :host=> '1.1.1.1', :user=> 'jme'
547
- # new :ssh, '1.1.1.1', 'me', 'secret'
548
- #
549
- def initialize(*args)
550
- ciphered_password=nil
551
- if args.size>2 and args[1].is_a?(String)
552
- @method, host, @user, pwd = args
553
- elsif args.size == 2 and args[1].is_a?(Hash) and args[0].is_a?(Symbol)
554
- @method = args[0]
555
- host = args[1][:host] || args[1][:hostname]
556
- @user = args[1][:user]|| args[1][:username]
557
- pwd = args[1][:pwd] || args[1][:password]
558
- ciphered_password = args[1][:ciphered_password]
559
- end
560
- @host, port = host.split
561
- @port = port.to_i
562
- @pwd = if ciphered_password
563
- ciphered_password
564
- else
565
- Expect4r.cipher(pwd) if pwd
566
- end
567
- @ps1 = /(.*)(>|#|\$)\s*$/
568
- @more = / --More-- /
569
- @matches=Set.new
570
- Base.add(self)
571
- self
572
- end
573
-
574
- def spawnee
575
- case method
576
- when :telnet ; "telnet #{host} #{port if port>0}"
577
- when :ssh ; "ssh #{spawnee_username}@#{host}"
578
- else
579
- raise RuntimeError
580
- end
581
- end
582
-
583
- def spawnee_username
584
- @user
585
- end
586
-
587
- def spawnee_prompt
588
- @ps1
589
- end
590
-
591
- def dup
592
- if @pwd
593
- self.class.new @method, @host, @user, Expect4r.decipher(@pwd)
594
- else
595
- self.class.new @method, @host, @user
596
- end
597
- end
598
-
599
- private
600
-
601
- def spawnee_password
602
- if @pwd.nil?
603
- @pwd = Expect4r.cipher( ask("(#{self}) Enter your password: ") { |q| q.echo = "X" } )
604
- @asked4pwd=true
605
- end
606
- Expect4r.decipher(@pwd)
607
- end
608
-
609
- def spawnee_reset
610
- @pwd=nil if @asked4pwd
611
- end
612
-
613
- end
614
- end
615
-
616
- if __FILE__ != $0
617
-
618
- at_exit {
619
- if Expect4r::Base.all
620
- Expect4r::Base.all.each { |o| o.logout if o.respond_to? :logout }
621
- end
622
- }
623
-
624
- else
625
-
626
- require "test/unit"
627
-
628
- class Expect4r::Base
629
- def initialize
630
- Expect4r::Base.add(self)
631
- end
632
- end
633
-
634
- class Base < Test::Unit::TestCase
635
- include Expect4r
636
-
637
- def test_add
638
- assert [], Base.all
639
- Base.new
640
- assert 1, Base.all.size
641
- Base.new
642
- assert_equal 2, Base.all.size
643
- end
644
- end
645
-
646
- end
data/lib/expect4r.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'expect/io'
2
+ require 'misc/base.rb'
2
3
  require 'misc/passwd'
3
4
 
4
5
  module Expect4r
data/lib/misc/base.rb ADDED
@@ -0,0 +1,178 @@
1
+
2
+ module Expect4r
3
+ class Base
4
+ include Expect4r
5
+ class << self
6
+ attr_reader :routers
7
+ def all
8
+ @arr
9
+ end
10
+ def add(r)
11
+ @arr ||=[]
12
+ @arr << r
13
+ end
14
+ end
15
+ def initialize(*args)
16
+ @matches = Set.new
17
+ Base.add(self)
18
+ self
19
+ end
20
+ end
21
+
22
+ class BaseLoginObject < Base
23
+ class << self
24
+ # Examples:
25
+ # my_mac = RShell.new_telnet '1.1.1.1', 'me', 'secret'
26
+ # ios = Ios.new_telnet '1.1.1.1', 'lab', 'lab'
27
+ # iox = Iox.new_telnet '1.1.1.1', 'username', 'pwd'
28
+ #
29
+ def new_telnet(*args)
30
+ new :telnet, *args
31
+ end
32
+ # Examples:
33
+ # my_mac = RShell.new_ssh '1.1.1.1', 'me', 'secret'
34
+ # iox = Ios.new_ssh '1.1.1.1', 'lab', 'lab'
35
+ # ios = Iosx.new_ssh '1.1.1.1', 'username', 'pwd'
36
+ #
37
+ def new_ssh(*args)
38
+ new :ssh, *args
39
+ end
40
+ attr_reader :routers
41
+ def add(r)
42
+ @routers ||=[]
43
+ @routers << r
44
+ end
45
+ end
46
+ attr_reader :host, :user, :method, :port, :proxy
47
+ alias :username :user
48
+ alias :hostname :host
49
+ # Adds a login to a Expect4r::BaseLoginObject"
50
+ #
51
+ # Constructor:
52
+ # * <tt>new</tt> <method>, <username>, <password>, [port_number]
53
+ # * <tt>new</tt> <method>, options={}
54
+ #
55
+ # Options are:
56
+ # * <tt>:host</tt> or <tt>:hostname</tt>
57
+ # * <tt>:user</tt> or <tt>:username</tt>
58
+ # * <tt>:pwd</tt> or <tt>:password</tt>
59
+ #
60
+ # Examples:
61
+ # new :telnet, :host=> '1.1.1.1', :user=> 'lab', :password=>'lab'
62
+ # new :ssh, :host=> '1.1.1.1', :user=> 'jme'
63
+ # new :ssh, '1.1.1.1', 'me', 'secret'
64
+ #
65
+ def initialize(*args)
66
+ ciphered_password=nil
67
+ if args.size>2 and args[1].is_a?(String)
68
+ @method, host, @user, pwd = args
69
+ @host, port = host.split
70
+ @port = (port || 0).to_i
71
+ elsif args.size == 2 and args[1].is_a?(Hash) and args[0].is_a?(Symbol)
72
+ @method = args[0]
73
+ @host = args[1][:host] || args[1][:hostname]
74
+ port = args[1][:port]
75
+ @port = (port || 0).to_i
76
+ @user = args[1][:user]|| args[1][:username]
77
+ pwd = args[1][:pwd] || args[1][:password]
78
+ ciphered_password = args[1][:ciphered_password]
79
+ else
80
+ raise
81
+ end
82
+ @pwd = if ciphered_password
83
+ ciphered_password
84
+ else
85
+ Expect4r.cipher(pwd) if pwd
86
+ end
87
+ @ps1 = /(.*)(>|#|\$)\s*$/
88
+ @more = / --More-- /
89
+ @matches=Set.new
90
+ Base.add(self)
91
+ self
92
+ end
93
+
94
+ attr_writer :host, :username, :port
95
+
96
+ def method=(arg)
97
+ case arg
98
+ when :telnet, :ssh
99
+ @method= arg
100
+ else
101
+ raise ArgumentError
102
+ end
103
+ end
104
+
105
+
106
+ def spawnee
107
+ case method
108
+ when :telnet ; "telnet #{host} #{port if port>0}"
109
+ when :ssh ; "ssh #{spawnee_username}@#{host} #{port if port>0}"
110
+ else
111
+ raise RuntimeError
112
+ end
113
+ end
114
+
115
+ def spawnee_username
116
+ @user
117
+ end
118
+
119
+ def spawnee_prompt
120
+ @ps1
121
+ end
122
+
123
+ def dup
124
+ if @pwd
125
+ self.class.new @method, @host, @user, Expect4r.decipher(@pwd)
126
+ else
127
+ self.class.new @method, @host, @user
128
+ end
129
+ end
130
+
131
+ private
132
+
133
+ def spawnee_password
134
+ if @pwd.nil?
135
+ @pwd = Expect4r.cipher( ask("(#{self}) Enter your password: ") { |q| q.echo = "X" } )
136
+ @asked4pwd=true
137
+ end
138
+ Expect4r.decipher(@pwd)
139
+ end
140
+
141
+ def spawnee_reset
142
+ @pwd=nil if @asked4pwd
143
+ end
144
+
145
+ end
146
+ end
147
+
148
+ if __FILE__ != $0
149
+
150
+ at_exit {
151
+ if Expect4r::Base.all
152
+ Expect4r::Base.all.each { |o| o.logout if o.respond_to? :logout }
153
+ end
154
+ }
155
+
156
+ else
157
+
158
+ require "test/unit"
159
+
160
+ class Expect4r::Base
161
+ def initialize
162
+ Expect4r::Base.add(self)
163
+ end
164
+ end
165
+
166
+ class Base < Test::Unit::TestCase
167
+ include Expect4r
168
+
169
+ def test_add
170
+ assert [], Base.all
171
+ Base.new
172
+ assert 1, Base.all.size
173
+ Base.new
174
+ assert_equal 2, Base.all.size
175
+ end
176
+ end
177
+
178
+ end
@@ -29,6 +29,7 @@ class Expect4r::Ios < ::Expect4r::BaseLoginObject
29
29
  end
30
30
 
31
31
  def enable_password
32
+ return unless @pwd || @enable_password
32
33
  @enable_password ||= @pwd # FIXME
33
34
  Expect4r.decipher(@pwd) # password is ciphered ...
34
35
  end
@@ -76,7 +76,7 @@ module Modes
76
76
  # returns *true* if router is in <tt>:user</tt> mode, *false* otherwise.
77
77
  #
78
78
  def user?
79
- @lp =~ /.+>\s*$/
79
+ @lp.split.last =~ /.+>\s*$/
80
80
  end
81
81
 
82
82
  def to_config
data/lib/router/common.rb CHANGED
@@ -11,7 +11,7 @@ module Common
11
11
  end
12
12
 
13
13
  def io_escape_char_cb
14
- putc "\n" if console?
14
+ putc "\r" if console?
15
15
  end
16
16
 
17
17
  end
@@ -56,7 +56,7 @@ class Expect4r::J < ::Expect4r::BaseLoginObject
56
56
  @matches << [/Exit with uncommitted changes.+\(yes\)/, 'yes']
57
57
  output = putline "commit", arg
58
58
  if /error: configuration check-out failed/.match(output.join)
59
- rollack
59
+ rollback
60
60
  raise SemanticError.new(self.class.to_s, output)
61
61
  end
62
62
  output
@@ -131,7 +131,7 @@ def to_shell
131
131
  end
132
132
 
133
133
  def logged_as_root?
134
- @_is_root_ ||= @lp =~ /root@/
134
+ ! (@_is_root_ ||= @lp =~ /root@/).nil?
135
135
  end
136
136
 
137
137
  def to_exec
data/lib/router/modes.rb CHANGED
@@ -22,8 +22,8 @@ module Modes
22
22
  login unless connected?
23
23
  case mode
24
24
  when :exec ; exec?
25
- when :shell ; shell?
26
25
  when :config ; config?
26
+ when :shell ; shell?
27
27
  else
28
28
  _mode_?
29
29
  end
@@ -32,8 +32,8 @@ module Modes
32
32
  login unless connected?
33
33
  case mode
34
34
  when :exec ; to_exec
35
- when :shell ; to_shell
36
35
  when :config ; to_config
36
+ when :shell ; to_shell
37
37
  end
38
38
  end
39
39
  def _mode_?
@@ -37,7 +37,8 @@ module Ping
37
37
  #++
38
38
  def ping(host, arg={}, &on_error)
39
39
 
40
- pct_success = arg.delete(:pct_success) || 99
40
+ pct_success = arg.delete(:pct_success) || 99
41
+ raise_on_error = arg.delete(:on_error_raise) || true
41
42
 
42
43
  output = exp_send(ping_cmd(host, arg), arg)
43
44
 
@@ -63,7 +64,9 @@ module Ping
63
64
  if on_error
64
65
  on_error.call(self)
65
66
  else
66
- raise ::Expect4r::Router::Error::PingError.new(@host, host, pct_success, $1.to_i, tx, rx, output)
67
+ if raise_on_error
68
+ raise ::Expect4r::Router::Error::PingError.new(@host, host, pct_success, $1.to_i, tx, rx, output)
69
+ end
67
70
  end
68
71
 
69
72
  end
@@ -19,5 +19,13 @@ module Show
19
19
  end
20
20
  end
21
21
 
22
+ # count | match | no-match | no-more | more
23
+ def method_missing(name, *args, &block)
24
+ @invalid_inputs ||=[]
25
+ super if @invalid_inputs.include?(name.to_s)
26
+ cmd = name.to_s.split('_').join(' ')
27
+ cmd += ""
28
+ end
29
+
22
30
  end
23
31
  end
@@ -0,0 +1,13 @@
1
+
2
+ class User
3
+ def in_role?(role)
4
+ true
5
+ end
6
+ end
7
+
8
+ describe User do
9
+ it "should be in any role assigned to it" do
10
+ user = User.new
11
+ user.should be_in_role("assigned role")
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expect4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-20 00:00:00.000000000Z
12
+ date: 2013-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: highline
16
- requirement: &70302466268900 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: 1.5.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70302466268900
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.0
25
30
  description: A Ruby Library for interacting with IOS, IOS-XR, and JUNOS CLI.
26
31
  email: jesnault@gmail.com
27
32
  executables: []
@@ -37,6 +42,7 @@ files:
37
42
  - examples/jex2.rb
38
43
  - lib/expect/io.rb
39
44
  - lib/expect4r.rb
45
+ - lib/misc/base.rb
40
46
  - lib/misc/passwd.rb
41
47
  - lib/misc/shell.rb
42
48
  - lib/router/cisco/common/common.rb
@@ -61,6 +67,7 @@ files:
61
67
  - lib/router/vyatta/vyatta.rb
62
68
  - test/expect4r_test.rb
63
69
  - test/misc/passwd_test.rb
70
+ - test/misc/rspec.rb
64
71
  - test/router/cisco/iox/iox_test.rb
65
72
  homepage: http://github.com/jesnault/expect4r
66
73
  licenses: []
@@ -86,11 +93,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
93
  version: '0'
87
94
  requirements: []
88
95
  rubyforge_project:
89
- rubygems_version: 1.8.10
96
+ rubygems_version: 1.8.24
90
97
  signing_key:
91
98
  specification_version: 3
92
99
  summary: Expect4r
93
100
  test_files:
94
101
  - test/expect4r_test.rb
95
102
  - test/misc/passwd_test.rb
103
+ - test/misc/rspec.rb
96
104
  - test/router/cisco/iox/iox_test.rb