Net::Telnet needs line of code added for fhopen to work withcygwin-perl and IO::Pty module in MSWin

2002-02-26 Thread Brian . Kelly



Hi Jay,

 My name is Brian Kelly, and I've been working with your Net::Telnet module
for over 4
years. First I'd like to say THANKS  It's been a fabulous tool for me - as
I'm sure it has for
thousands upon thousands of folks around the globe.
 Lately I've been writing an automation wrapper that relies very heavily on
your module.
With the recent evolution of the Cygwin Unix layer for MSWin operating systems,
telneting to a
Windows box is now easy, robust and FRE!!. New possibilities are now opening
up to truly
integrate whole networks with a common set of scripts and tools that use telnet,
ftp, and ssh as
a means of communicating. Of course, with the fabulous documentation provided by
Lincoln
Stein in his book "Network Programming with Perl",  I'm sure there are many like
myself who
are working in this direction with heterogenius environments.
 Recently, the IO:Pty module was successfully ported to work with
cygwin-perl. Now it
is truly possible to use your fhopen method (per Lincoln Stein's chapter on your
Telnet module)
to do Expect-like control of interactive programs using your Telnet module on
MSWin OS's.

 There's just one slihhht  problem ...

 It seems that on a MSWin OS there is no way to truly escape the infamous
CR\LF.

 In your "print" subroutine, you "attempt" to do this with the following
line of code:


sub print {

my ($self) = shift;
my (
$data,
$endtime,
$fh,
$len,
$nfound,
$nwrote,
$offset,
$ready,
$stream,
$timed_out,
$timeout,
);

$stream = *$self->{net_telnet};
$stream->{timedout} = '';
$stream->{num_wrote} = 0;
return $self->error("print failed: handle is closed")
unless $stream->{opened};

## Try to send any waiting option negotiation.
if (length $stream->{unsent_opts}) {
&_flush_opts($self, $stream);
}

## Add field and record separators.
$data = join($stream->{ofs}, @_) . $stream->{ors};

## If requested, log the output.
if ($stream->{outputlog}) {
local $\ = '';
$fh = $stream->{outputlog};
$fh->print($data);
}

## Escape TELNET IAC and carriage-return chars.
if ("\n" ne "\015") {  # not running on a Mac
if ($stream->{telnet_mode}) {
#   $data =~ s(\377)(\377\377)g;
$data =~ s(\015)(\015\000)g;
}

if (!$stream->{bin_mode}) {
$data =~ s(\n)(\015\012)g;<===Here
is where you attempt to escape the CR
chomp $data;
<-- This is the code I had to add to achieve correct
 results
}
}
else {  # probably running on a Mac





 The automation wrapper I'm working on is a module I am hoping
to release to the CPAN within six months to a year. But aside from my "selfish"
desires, it would seem that more people as time goes by are going to run into
this issue.  ( I may not even be the first! )

 Without doing that chomp on the end of the line, when attempting to use
fhopen
to automate a login sequence, the CR\LF prevents an opportunity to pause the
password
prompt and interprets that CR\LF as a null password - of course causing failure.
 By
chomping off the CR, the proper and "expected" behavior is achieved.

 This solution may indeed be "too simple". There may be other issues that
must
be considered in arriving at a "final solution". Anyway - I needed to bring it
to your
attention. If you are no longer maintaining this module, could you tell me who
is?

 Again, thanks for making my computing life A LOT EASIER!!!

Sincerely,
Brian Kelly

Work Phone:   212-286-3931
Pager:   810-450-9766



"Empire Blue Cross Blue Shield" made the following
 annotations on 02/26/02 19:33:28
--

[INFO] -- Access Manager:
Attention!  This electronic message contains information that may be legally 
confidential and/or privileged.  The information is intended solely for the individual 
or entity named above and access by anyone else is unauthorized.  If you are not the 
intended recipient, any disclosure, copying, distribution, or use of the contents of 
this information is prohibited and may be unlawful.  If you have received this 
electronic transmission in error, please reply immediately to the sender that you have 
received the message in error, and delete it.




--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/




Re: Net::Telnet needs line of code added for fhopen to work withcygwin-perl and IO::Pty module in MSWin

2002-03-14 Thread Jay Rogers

[EMAIL PROTECTED] wrote:
> First I'd like to say THANKS

Brian, you're most welcome.

>  Recently, the IO:Pty module was successfully ported to
> work with cygwin-perl. Now it is truly possible to use your
> fhopen method (per Lincoln Stein's chapter on your Telnet
> module) to do Expect-like control of interactive programs using
> your Telnet module on MSWin OS's.

I haven't seen Lincoln's book.  Is that chapter available online?

>  There's just one slihhht  problem ...
> 
>  It seems that on a MSWin OS there is no way to truly escape the infamous
> CR\LF.
> 
>  In your "print" subroutine, you "attempt" to do this with the following
> line of code:

The TELNET protocol specifies CR LF as an end-of-line.  The
Net::Telnet::print() code you mention converts the OS native EOL
to the TELNET EOL.

If you're using Net::Telnet with a pseudo terminal then yes you
do want the EOL to be just CR.

Probably the best way to do this is just:

$telnet->output_record_separator("\r");

Modifying Net::Telnet to convert CR LF to just CR is definitely
not the right way to do this.

Here's some code that changes a password on SunOS 5.8.  Give it a
try on cygwin.

#!/usr/bin/perl

my $oldpw = "";
my $newpw = "";

use Net::Telnet;
$pwd = new Net::Telnet (Timeout => 2,
Errmode => "return",
Dump_log => "/tmp/dump.log");

## Start passwd program.
&spawn($pwd, "passwd")
or die $pwd->errmsg;

## Send existing passwd.
$pwd->waitfor('/password: ?$/i')
or die "no old password prompt: ", $pwd->lastline;
$pwd->print($oldpw);

## Send new passwd.
$pwd->waitfor('/new password: ?$/i')
or die "bad old password: ", $pwd->lastline;
$pwd->print($newpw);

## Send new passwd verification.
$pwd->waitfor('/new password: ?$/i')
or die "bad new password: ", $pwd->lastline;
$pwd->print($newpw);

## Display success or failure.
$pwd->waitfor('/changed/')
or die "bad new password: ", $pwd->lastline;
print $pwd->lastline;

$pwd->close;
exit;


sub spawn {
my($spawn) = shift @_;
my($pty,
   $tty,
   $tty_fd);

use IO::Pty ();
use POSIX ();

$pty = new IO::Pty
or die $!;

unless ($pid = fork) {  # child process
die "problem spawning program: $!\n" unless defined $pid;

use POSIX ();
POSIX::setsid
or die "setsid failed: $!";

$tty = $pty->slave;
$tty_fd = $tty->fileno;
 
open STDIN, "<&$tty_fd" or die $!;
open STDOUT, ">&$tty_fd" or die $!;
open STDERR, ">&STDOUT" or die $!;
close $pty;
close $tty;

exec @_ == 1 ? $_[0] : @_
or die "problem executing $_[0]: $!\n";
}

$spawn->fhopen($pty)
or return;
$spawn->telnetmode(0);
$spawn->binmode(1);
$spawn->output_record_separator("\r");
$spawn->cmd_remove_mode(1);

1;
} # end sub spawn

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/




Re: Net::Telnet needs line of code added for fhopen to work withcygwin-perl and IO::Pty module in MSWin

2002-03-15 Thread RGiersig

> >  It seems that on a MSWin OS there is no way to truly escape 
> > the infamous CR\LF.
> 
> The TELNET protocol specifies CR LF as an end-of-line.  The
> Net::Telnet::print() code you mention converts the OS native EOL
> to the TELNET EOL.
> 
> If you're using Net::Telnet with a pseudo terminal then yes you
> do want the EOL to be just CR.

Alternatively you might want to set the pty to raw mode, which disables 
character translation and gives a more pipe-like semantics.  The latest 
IO-Tty v1.00 has a set_raw() method...

> Probably the best way to do this is just:
> 
>$telnet->output_record_separator("\r");
> 
> Modifying Net::Telnet to convert CR LF to just CR is definitely
> not the right way to do this.

I'll second that. :-)

> Here's some code that changes a password on SunOS 5.8.  Give it a
> try on cygwin.

Ugh, I just released a much-improved version of IO-Tty (v1.00), please 
take a look how it's done nowadays (in the 'try' and 'test.pl' 
scripts).  Alternatively try using Expect, which gives you similar 
functionality to waitfor() and also lets you use an already opened 
filehandle.

Hope this helps,

Roland
--
[EMAIL PROTECTED]



--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/