Following up on a post a few days ago (node 67578) I am working on a 'telnet passthrough'. This is a process that connects to a server via telnet ( net::telnet) and also offers a standard socket ( io::socket ) for other users to connect to. Key presses are then passed from the socket to the telnet session and responses copied back. I developed the following on unix and it seems to work. I am surprised at that fact..... $switch is a telnet object (see switchConnect()) and I do
print $switch $byte # prints one byte to the telnet connection. + Confirmed as written by checking DUMP file sysread($switch, $byte2, 1) # reads in one byte from the telnet inp +ut buffer.
Can someone explaing why this works as $switch is not a filehandle. Now for the big problem. I have to get this running on a windows system running activeperl v5.6.1. Copied it over and everything has stopped working. boo hoo. the line from above has to be swapped to something like
-print $switch $byte +$switch->put($byte); ( telnet method )
and the whole child/parent thing seems to stop working or deadlock. If I replace
-while (sysread($switch, $byte2, 1)) -{ - print $client $byte2; -} +sleep(50)
in the parent section things seem to start going again but obviously nothing it now coming back from the telnet console. What is going on ?? here's the program
#!/opt/DKBperl/bin/perl $|=1; use IO::Socket; use Net::Telnet; #pass4.pl <switchname> <port> my($switch)=switchConnect($ARGV[0]); my($server) = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $ARGV[1], Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; while ($client = $server->accept()) { if (!defined($child_pid = fork())) { die "cannot fork: $!"; } if($child_pid) { print "I'm the parent $child_pid\n"; # REPLY # accept input from the telnet connection (switch) and copi +es it to heroix (io::socket) while (sysread($switch, $byte2, 1)) { print $client $byte2; } kill("TERM" => $kidpid); # send SIGTERM to child exit 0; } else { print "I'm the child $child_pid\n"; # accept input from heroix (io::socket) and copies it to +the telnet connection (switch) $client->autoflush(1); while (sysread($client, $byte, 1)) { $num=ord($byte); if($num==127) {$num=8}; print $switch chr($num); } print "child exiting\n"; } } sub switchConnect { my($switchName)=@_; $t = new Net::Telnet()->new( input_log => "tmp/in", dump_log => "tmp/dump", Timeout => 10, output_log => "tmp/out", errmode => 'return'); print("Attempting to open switch $switchName.."); $t->open(host=>$switchName); if(!$t->waitfor('/login:/')) { print "$hsgname seems engaged- exiting code 10\n"; exit(10); } $t->print("admin"); $t->waitfor('/Password:/'); $t->print("santest1"); if(!$t->waitfor('/\>/')) { print "Timeout on the password. Is the password correct for $ +hsgname ? - exiting code 11\n"; exit(11); } print "..ok\n"; return $t; }

update (broquaint): added a ending </b> tag and put a <readmore> tag before the main section of code


In reply to telnet passthrough 2 by wertert

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.