From: Matt Z. <mzagrabe@d.umn.edu> - 2007-03-21 18:55:10
|
Hello, I am experiencing some strange behavior when combining Expect with Net::Telnet when telnet'ing to Cisco 1924 switches. Scenario: Cisco Gear 1924's newer than 1924's +----------+---------------------+ Net::Telnet | works | works | Expect spawning system telnet | works | works | Expect using Net::Telnet | no work | works | +----------+---------------------+ The reason I am concerned with using Net::Telnet with Expect is I have designed my program to be multithreaded and from what I read Net::Telnet is thread safe whereas IO-Tty is not. The symptoms of what happens when using Expect and Net::Telnet to telnet to a 1924 is this: telnet negotiation (non printable characters) happens and then it looks like no other traffic is sent from the remote side. I am currently combing through tcpdump and wireshark analysis of this, but I thought I would send an email to the list to see if anyone had any ideas in the meantime. Here is some code that demonstrates what is happening: #!/usr/bin/perl use strict; use Expect; use Net::Telnet (); print "\n\n-----------START OF Net::Telnet-----------\n"; my $t =3D new Net::Telnet("ghAS1.d.umn.edu"); my @lines =3D $t->waitfor('/Selection.*/'); print "@lines\n"; print "\n\n-----------END OF Net::Telnet-----------\n"; print "\n\n-----------START OF Expect with Net::Telnet-----------\n"; my $e =3D new Net::Telnet("ghAS1.d.umn.edu"); my $exp =3D Expect->exp_init($e); $exp->log_stdout(1); &expect_selection($exp); print "\n\n-----------END OF Expect with Net::Telnet-----------\n"; print "\n\n-----------START OF Expect with system telnet-----------\n"; my $exp =3D Expect->spawn("telnet ghAS1.d.umn.edu"); $exp->log_stdout(1); &expect_selection($exp); print "\n\n-----------END OF Expect with system telnet-----------\n"; sub expect_selection { my $exp =3D shift; $exp->expect(5, [ qr/Selection.*/, sub { my $fh =3D shift; $fh->send('k'); undef; } ]); } # end of code output from the code follows: -----------START OF Net::Telnet----------- Catalyst 1900 Management Console Copyright (c) Cisco Systems, Inc. 1993-1999 All rights reserved. Enterprise Edition Software Ethernet Address: 00-10-F6-24-A8-40 PCA Number: 73-2239-03 PCA Serial Number: SAD01452031 Model Number: WS-C1924-EN System Serial Number: FAA0148Z051 ------------------------------------------------- 3 user(s) now active on Management Console. User Interface Menu [M] Menus [K] Command Line Enter Selection: -----------END OF Net::Telnet----------- -----------START OF Expect with Net::Telnet----------- =EF=BF=BD=EF=BF=BD=03=EF=BF=BD=EF=BF=BD=03=EF=BF=BD -----------END OF Expect with Net::Telnet----------- -----------START OF Expect with system telnet----------- Trying 10.0.0.1 Will send login name and/or authentication information. Connected to c1924.domain.org (10.0.0.1). Escape character is '^]'. Catalyst 1900 Management Console Copyright (c) Cisco Systems, Inc. 1993-1999 All rights reserved. Enterprise Edition Software Ethernet Address: 00-10-F6-24-A8-40 PCA Number: 73-2239-03 PCA Serial Number: SAD01452031 Model Number: WS-C1924-EN System Serial Number: FAA0148Z051 ------------------------------------------------- 5 user(s) now active on Management Console. User Interface Menu [M] Menus [K] Command Line Enter Selection: -----------END OF Expect with system telnet----------- --=20 Matt Zagrabelny - mzagrabe@d.umn.edu - (218) 726 8844 University of Minnesota Duluth Information Technology Systems & Services PGP key 1024D/84E22DA2 2005-11-07 Fingerprint: 78F9 18B3 EF58 56F5 FC85 C5CA 53E7 887F 84E2 2DA2 He is not a fool who gives up what he cannot keep to gain what he cannot lose. -Jim Elliot |
From: Bryan B. <br...@bu...> - 2007-03-21 20:30:22
|
> Hello, > > I am experiencing some strange behavior when combining Expect with > Net::Telnet when telnet'ing to Cisco 1924 switches. > > Scenario: > > Cisco Gear > > 1924's newer than 1924's > +----------+---------------------+ > Net::Telnet | works | works | > Expect spawning system telnet | works | works | > Expect using Net::Telnet | no work | works | > +----------+---------------------+ > > The reason I am concerned with using Net::Telnet with Expect is I have > designed my program to be multithreaded and from what I read Net::Telnet > is thread safe whereas IO-Tty is not. > I have used threading with expect on top of Net::Telnet successfully. Spawning a system telnet will, obviously, not work. > The symptoms of what happens when using Expect and Net::Telnet to telnet > to a 1924 is this: > > telnet negotiation (non printable characters) happens and then it looks > like no other traffic is sent from the remote side. I am currently > combing through tcpdump and wireshark analysis of this, but I thought I > would send an email to the list to see if anyone had any ideas in the > meantime. > > Here is some code that demonstrates what is happening: > #!/usr/bin/perl > > use strict; > use Expect; > use Net::Telnet (); > > print "\n\n-----------START OF Net::Telnet-----------\n"; > > my $t = new Net::Telnet("ghAS1.d.umn.edu"); > my @lines = $t->waitfor('/Selection.*/'); > print "@lines\n"; > > print "\n\n-----------END OF Net::Telnet-----------\n"; > print "\n\n-----------START OF Expect with Net::Telnet-----------\n"; > > my $e = new Net::Telnet("ghAS1.d.umn.edu"); > my $exp = Expect->exp_init($e); > $exp->log_stdout(1); > &expect_selection($exp); > Try this bit of code: -- snip --> my $e = new Net::Telnet(Prompt => '/Selection.*/'); $e->open("ghAS1.d.umn.edu"); my $exp = Expect->exp_init($e); $exp->log_stdout(1); &expect_selection($exp); <-- end -- The open() method should finish the handshaking for you. Also, if you have to log in, it might help to do that within the Net::Telnet object prior to expecting on top: $e->login($user, $password); Hope that helps. Bryan http://sourceforge.net/projects/rover |
Re: [Expectperl-discuss] strange behavior with Net::Telnet, Expect,
and Cisco 1924 Switches - SOLVED
From: Matt Z. <mzagrabe@d.umn.edu> - 2007-03-22 18:26:55
|
> The reason I am concerned with using Net::Telnet with Expect is I have > designed my program to be multithreaded and from what I read Net::Telnet > is thread safe whereas IO-Tty is not. >=20 > The symptoms of what happens when using Expect and Net::Telnet to telnet > to a 1924 is this: >=20 > telnet negotiation (non printable characters) happens and then it looks > like no other traffic is sent from the remote side. I am currently > combing through tcpdump and wireshark analysis of this, but I thought I > would send an email to the list to see if anyone had any ideas in the > meantime. Solved... All thanks goes to Bryan Bueter who did all the leg-work on this one. As he said (off list), Net::Telnet needed to be coaxed into getting past the telnet handshaking and negotiations, then it could be passed to Expect. The important line to note below is the 'waitfor'. Then it is safe to hand the file handle over to Expect. Working code follows: #!/usr/bin/perl use strict; use Expect; use Net::Telnet (); print "\n\n-----------START OF Expect with Net::Telnet-----------\n"; my $e =3D new Net::Telnet(); $e->open("c1924.domain.org"); my @lines =3D $e->waitfor('/User Interface Menu/'); my $exp =3D Expect->exp_init($e); $exp->log_stdout(1); &expect_selection($exp); $exp->expect(5,'-re','>\z'); $exp->hard_close(); print "\n\n-----------END OF Expect with Net::Telnet-----------\n"; sub expect_selection { my $exp =3D shift; $exp->expect(5, [ qr/Selection.*/, sub { my $fh =3D shift; $fh->send('k'); undef; } ]); } --=20 Matt Zagrabelny - mzagrabe@d.umn.edu - (218) 726 8844 University of Minnesota Duluth Information Technology Systems & Services PGP key 1024D/84E22DA2 2005-11-07 Fingerprint: 78F9 18B3 EF58 56F5 FC85 C5CA 53E7 887F 84E2 2DA2 He is not a fool who gives up what he cannot keep to gain what he cannot lose. -Jim Elliot |