jeanmichel-gh / expect4r

Other
29 stars 6 forks source link

Prompt not matched correctly #3

Open gleonet opened 11 years ago

gleonet commented 11 years ago

Hi,

First of all I want to thank you for your library. It will be very useful for me.

I have an issue when I try to connect to a new router which has the default banner. Expect4r matches one of the line in the banner and tries to send the login and the password before the real login prompt is ready.

The gist with the content of this banner : https://gist.github.com/gleonet/dc21a1316241474b9fae

I modified the file lib/router/cisco/ios/ios.rb at the line 16 from @ps1 = /(.)(>|#|\$)\s$/ to @ps1 = /(.)(#|\$)\s$/ and after that the login is successful but I suppose the character '>' is useful in other situation and my fix is not the good solution.

I hope this feedback could be useful for you.

Regards,

Gael Leonet

jeanmichel-gh commented 11 years ago

Hi,

Could you turn on debug before login and send me the output?

r = Ios.new exp_debug :on r.login

Thanks

gleonet commented 11 years ago

Hi,

The output with the debug is there : https://gist.github.com/gleonet/39b6f623f8687b5e7dde I cleaned the debug output and the wrong match appears at the line 102.

Thanks,

Gael

jeanmichel-gh commented 11 years ago

Hi,

You are correct: removing '>' from the reggae will create its own set of issues. How soon do you need something? How are you using expect4r?

Thanks

jeanmichel-gh commented 11 years ago

Hi Gael

Could you check if https://github.com/jesnault/expect4r/commit/de7020aa4434aac0c2374219197af09e7820c31f address your issue and let me know?

Thx Jean-Michel

gleonet commented 11 years ago

Hi Jean-Michel,

I think to use expect4r in a web front-end which will be used to facilitate the configuration of our routers during the installation. I will not be able to test the new version before Thursday afternoon but I let you know as soon as possible.

Thanks for your help.

Regards,

Gael

jeanmichel-gh commented 11 years ago

Should work:

jmes-MacBook:test jme$ irb irb(main):001:0> require 'expect4r' => false irb(main):002:0> include Expect4r => Object irb(main):003:0> ubuntu = RShell.new_ssh :hostname=>'192.168.158.250', :user=>'root' => #<Expect4r::RShell:0x007fb90a8df160 @method=:ssh, @host="192.168.158.250", @port=0, @user="root", @pwd=nil, @ps1=/.+^#\s+$/, @more=/ --More-- /, @matches=#<Set: {}>> irb(main):005:0> ubuntu.login (#Expect4r::RShell:0x007fb90a8df160) Enter your password: XXXXXXXX => [[["export COLUMNS=1024\r\n", "\e]0;root@ubuntu: ~\a\e[01;32mroot@ubuntu\e[00m:\e[01;34m~\e[00m# "], :ok]] irb(main):006:0* ios = Ios.newtelnet :hostname => "192.168.131.13", :user=>'lab', :pwd=>'lab' => #<Expect4r::Ios:0x007fb90c041ab0 @method=:telnet, @host="192.168.131.13", @port=0, @user="lab", @pwd="\x93\xB8%X\x9Dd\xD8s\xF0\x85O\xB4\xF7\x95\xB7\x01", @ps1=/(.*)(>|#|\$)\s$/, @more=/ --More-- /, @matches=#<Set: {}>> irb(main):007:0> ios.login ios.login ios.login_by_proxy ios.login_via
irb(main):008:0> ios.login_byproxy ubuntu => #<Expect4r::Ios:0x007fb90c041ab0 @method=:telnet, @host="192.168.131.13", @port=0, @user="lab", @pwd="XXXXXXXXXXX", @ps1=/(.)(>|#|\$)\s*$/, @more=/ --More-- /, @matches=#<Set: {}>, @pre_matches=[[/Cisco Configuration Professional/, #Proc:0x007fb909827c28@/Users/jme/routing/expect4r/lib/router/cisco/ios/ios.rb:39]], @proxy=#<Expect4r::RShell:0x007fb9098277f0 @method=:ssh, @host="192.168.158.250", @port=0, @user="root", @pwd="XXXXXXXXXX", @ps1=/.+^#\s+$/, @more=/ --More-- /, @matches=#<Set: {}>, @thread=#, @pid=53056, @w=#File:/dev/ttys006, @r=#File:/dev/ttys006, @pre_matches=[], @_lp_1="export COLUMNS=1024\r\n", @lp="\e]0;root@ubuntu: ~\a\e[01;32mroot@ubuntu\e[00m:\e[01;34m~\e[00m# ">, @r=#File:/dev/ttys006, @w=#File:/dev/ttys006, @pid=53056, @lp="Router#", @_lp_1="term width 0\r\n", @enable_password="\x93\xB8%X\x9Dd\xD8s\xF0\x85O\xB4\xF7\x95\xB7\x01"> irb(main):012:0> puts ios.show_run :begin=> 'banner' show run | begin banner

banner login ^C

Cisco Configuration Professional (Cisco CP) is installed on this device. This feature requires the one-time use of the username "cisco" with the password "cisco". These default credentials have a privilege level of 15.

YOU MUST USE CISCO CP or the CISCO IOS CLI TO CHANGE THESE PUBLICLY-KNOWN CREDENTIALS

Here are the Cisco IOS commands.

username privilege 15 secret 0 no username cisco

Replace and with the username and password you want to use.

IF YOU DO NOT CHANGE THE PUBLICLY-KNOWN CREDENTIALS, YOU WILL NOT BE ABLE TO LOG INTO THE DEVICE AGAIN AFTER YOU HAVE LOGGED OFF.

For more information about Cisco CP please follow the instructions in the

QUICK START GUIDE for your router or go to http://www.cisco.com/go/ciscocp

^C ! line con 0 logging synchronous line aux 0 line vty 0 login local line vty 1 4 login ! ! ! end

Router# => nil irb(main):013:0> rb(main):017:0> puts ios.show_ip_route show ip route Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 E1 - OSPF external type 1, E2 - OSPF external type 2 i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2 ia - IS-IS inter area, * - candidate default, U - per-user static route o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP

Gateway of last resort is not set

  192.168.131.0/24 is variably subnetted, 2 subnets, 2 masks

C 192.168.131.0/24 is directly connected, Ethernet0/0 L 192.168.131.13/32 is directly connected, Ethernet0/0 Router# => nil irb(main):018:0>

gleonet commented 11 years ago

Hi,

Your fix solves my previous issue but the login command still failed with another error message but when I check with connected? or by running a command the login seems to be successful.

Here is the log with the debug :on

debug: buf0: [Q]
...
debug: buf0: [QUICK START GUIDE]
debug: matching PROMPT
debug: QUICK START GUIDE

debug: buf0: [U]
...
debug: buf0: [Username:]
debug: match USERNAME
debug: Username:
debug: print: "cisco\r", io_writer: #<File:0xb3fa524>
debug: buf0: [ ]
debug: buf0: [ \r]
debug: buf0: [ \r\n]
debug: match EOL
debug: match EOL
debug:

debug: buf0: [U]
...
debug: buf0: [Username:]
debug: match USERNAME
debug: Username:
debug: print: "cisco\r", io_writer: #<File:0xb3fa524>
debug: buf0: [ ]
debug: buf0: [ c]
debug: buf0: [ ci]
debug: buf0: [ cis]
debug: buf0: [ cisc]
debug: buf0: [ cisco]
debug: buf0: [ cisco\r]
debug: buf0: [ cisco\r\n]
debug: match EOL
debug: match EOL
debug:  cisco

debug: buf0: [P]
...
debug: buf0: [Password:]
debug: match PASSWORD
debug: Password:
debug: buf0: [ ]
debug: buf0: [ C]
debug: buf0: [ C\r]
debug: buf0: [ C\r\n]
debug: match EOL
debug: match EOL
...
debug: buf0: [8]
debug: buf0: [86]
debug: buf0: [867]
debug: buf0: [867#]
debug: IO.select is NIL (TIMEOUT=2)
debug: IO.select is NIL (TIMEOUT=13)
debug: readbuf: _io_exit?Expect4r::ConnectionError: Expect4r::ConnectionError
        from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/expect/io.rb:339:in `_login'
        from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/router/cisco/ios/ios.rb:45:in `login'
        from (irb):6
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
jeanmichel-gh commented 11 years ago

Hi,

I noticed that for every match you have two debug statements ?

debug: buf0: [ cisco\r\n] debug: match EOL debug: match EOL

debug: buf0: [8] debug: buf0: [86] debug: buf0: [867] debug: buf0: [867#]

as well as two IO.select ....

debug: IO.select is NIL (TIMEOUT=2) debug: IO.select is NIL (TIMEOUT=13)

Also ps1 prompt should match "867#"

I would need access to this box to understand what's happening.

Thanks Jean-Michel

gleonet commented 11 years ago

Sorry, it will not be possible to give you an access on the router.

I rebuild the gem based on your last commit and I don't have exactly the same output for the debug than my previous comment. I'm not sure if I correctly built the gem but I still have the issue with the wrongly match.

I think the double match for EOL is due to the double characters \r \n at the end of each lines.

Here is a sample of the debug lines with my last test.

debug: buf0: [u]
...
debug: buf0: [username <myuser>]
debug: match PROMPT
debug: username <myuser>
debug: print: "enable\r", io_writer: #<File:0x0000000178eb40>

...

Username: 
Username: enable
Password: 
debug: print: "term len 0\r", io_writer: #<File:0x0000000178eb40>
debug: buf0: [\r]
debug: buf0: [\r\n]
debug: matching EOL
...
debug: buf0: [U]
...
debug: buf0: [Username: ]
debug: IO.select is NIL (TIMEOUT=13)
debug: 
debug: Username: Expect4r::ExpTimeoutError: Expect4r::ExpTimeoutError
    from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/expect/io.rb:396:in `putline'
    from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/router/cisco/ios/ios.rb:50:in `putline'
    from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/expect/io.rb:180:in `block in exp_send'
    from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/expect/io.rb:178:in `each_line'
    from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/expect/io.rb:178:in `exp_send'
    from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/router/cisco/ios/modes.rb:41:in `exec'
    from /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/router/cisco/ios/ios.rb:40:in `login'
    from (irb):10
    from /usr/bin/irb:12:in `<main>'

Thanks for your help.

Gael Leonet

jeanmichel-gh commented 11 years ago

Could you please attach to an email to jesnault@gmail.com the unedited debug log as well as /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/expect/io.rb /usr/lib/ruby/gems/1.9.1/gems/expect4r-0.0.9/lib/router/cisco/ios/ios.rb

Thanks Jean-Michel