From: expect <ex...@ih...> - 2003-03-19 03:14:36
|
On Thu, 13 Mar 2003 13:43:55 -0000 "Kemp, Donald" <dk...@em...> wrote: > Hi, > I have a problem I do not understand ..... I am using Expect to interact > between a Router and a user. This router is accessed by telnetting to one > router and then using this router to Dial the second Router. Having connect > to the second router I then "interact" with it using the code below > (interact_code). One of the things that you can do when in this mode is to > "enable" a logging facility (ctlr-x l). If "logging" in NOT enabled, then > you see only one copy of what you type, i.e. if you typed "show" you see > "show". However if "logging" IS enabled then you see 2 copies of everything > you type, i.e. if you typed "show" you see "sshhooww". What I do not > understand is why? Answer to first question: (as root) perl -MCPAN -e 'install (Net::Telnet)' Then use Net::Telnet Answer to second question: yes Seriously, try the Net::Telnet package and detecting "NO CARRIER" is what expect does so well! hth > > Sub-Routine Interact > sub interact { > my $telnet = shift; > my $fh; > my $ec; > > $fh=IO::File->new; > $fh->fdopen(fileno(STDIN),"r"); > > my $outfile = new IO::File; > $outfile->IO::File::fdopen(STDOUT,'w'); > > while (1) { > my $in_object = Expect-> exp_init($fh); > my $out_object = Expect-> exp_init($outfile); > $out_object-> manual_stty(1); > if ($_logfile) { > $telnet-> set_group($out_object,$_logfile); > } > else { > $telnet-> set_group($out_object); > } > $in_object-> set_group($telnet); > $in_object-> set_seq($ConfigData::escape_character,undef); > $telnet-> manual_stty(1); > Expect::interconnect($telnet,$in_object); > $in_object -> set_group(undef); > > print $telnet " \010"; # make the next test work... > # if there is something waiting in > # the handle after we are writing > # some nonsense to it, > # it is not closed. :-) > if (Expect::test_handles(0,$telnet)) { > #print STDERR "\n*** CONNECTION CLOSED ***\n"; > last; # connection is closed, exit > # HEY, THIS REALLY WORKS!!! > } > > system "stty", '-icanon', 'eol', "\001"; > $char = getc; > system "stty", 'icanon', 'eol', '^@'; # ASCII null > print "\010 \010"; > if ($char eq '?' || $char eq 'h') { > &hotkey_help($telnet); > } > elsif ($char eq 'l') { > &logging($telnet); > } > elsif ($char eq 'q') { > last; > } > } > system "stty", 'sane'; # clean up; this can not hurt > } > > Sub-Routine Logging > sub logging { > my $telnet = shift; > my $logfilename = shift; > > if ($_logfilehandle) { > $telnet-> set_group(undef); > close $_logfilehandle; > print STDERR "\n*** Logging to $_logfilename disabled. > ***\n"; > undef($_logfile); > undef($_logfilename); > undef($_logfilehandle); > print $telnet "\r"; > return; > } > > if (!$logfilename) { > $_logfilename = sprintf "/tmp/log-%s-%s-%s",$routername, > &Perm::get_user_name, > &POSIX::strftime("%Y%m%d-%H:%M:%S",localtime(time)); > > # if it was defined but empty, use the default and don't > ask. > if (!defined($logfilename)) { > print STDERR "\n*** Log to which file? ". > "Enter file name or hit Enter to > accept". > "\n*** the default file name: > $_logfilename\n"; > my $input = <STDIN>; > chomp($input); > if ($input) { > $input =~ /^(.*?)$/; > $_logfilename = $1; > } > } > } > else { > $_logfilename = $logfilename; > } > > my $pid = fork(); > if ($pid > 0) { > waitpid(0,$pid); > select(undef, undef, undef, 0.25); # sleep for 1/4 > second > } > elsif ($pid == 0) { # create the log file as the real > user... > $EFFECTIVE_USER_ID = $REAL_USER_ID; > local (*F); > open F,">$_logfilename"; > close F; > chmod 0660,$_logfilename; > exit 1; > } > $_logfilehandle = new IO::File $_logfilename, > O_WRONLY|O_APPEND|O_CREAT; > > if (!$_logfilehandle) { > print STDERR "*** Can not log to $_logfilename,\n*** $!\n"; > return; > } > print STDERR "*** Logging to $_logfilename\n"; > $_logfile = Expect-> exp_init($_logfilehandle); > $_logfile-> manual_stty(1); > $telnet-> set_group($_logfilehandle); > $telnet-> log_group(1); > print $telnet "\r"; > } > > > A second query I have is when I terminate the connection to the second > router (issue "exit") the modem gives me "NO CARRIER", is it possible to > detect this in the "interact" sub-routine?? > > > Best Regards, > Donald.E.S.Kemp > AT&T Labs > Network Management Solutions, > Building 6000, Langstone Technology Park, > Langstone Road, Havant, Hampshire, > United Kingdom, > PO9 1SA > Phone: +44(0)23 9222 8424 > email: dk...@em... > > This message and any attachments to it contain confidential business > information exclusively intended for the recipients. Please do not forward > or distribute it to anyone else. If you have received this e-mail in error, > please call [+44 (0)23 92228424] to report the error and then delete this > message from your system. > > > > > > > > ------------------------------------------------------- > This SF.net email is sponsored by:Crypto Challenge is now open! > Get cracking and register here for some mind boggling fun and > the chance of winning an Apple iPod: > http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > > -- "One morning I shot an elephant in my pajamas. How he got into my pajamas I'll never know." -- Grouch Marx 0Oo. Rick Hubbell RHu...@ih... http://iHubbell.com .oO0 |