Dear Monks,

I am working on Net::OpenSSH::Gateway, a Net::OpenSSH extension that allows this module to cross through proxies and SSH gateways.

As part of it, I have written a Perl one-liner that is a netcat clone, but this one-liner is bigger than I wish, so I would like to get your help in order to reduce its length.

You can improve on the current version or rewrite it from scratch, I don't mind as long as it is able to run an SSH session on top of it (using ProxyCommand, see ssh_config) and exit when any side of the connection closes. It should only use core modules and be able to run on older Perls.

Here is the current one-liner:

perl -MFcntl=F_SETFL,F_GETFL,O_NONBLOCK -MSocket '-e$0=perl;socket($c, +AF_INET,SOCK_STREAM,0)&&connect($c,sockaddr_in$ARGV[1],inet_aton$ARGV +[0])||die$!;fcntl$_,F_SETFL,O_NONBLOCK|fcntl$_,F_GETFL,0 for@d=(*STDI +N,$c),@e=($c,*STDOUT);L:for(0,1){sysread($d[$_],$f,8**5)||exit and$f[ +$_].=$f if vec$g,$_*($h=fileno$c),1;substr$f[$_],0,syswrite($e[$_],$f +[$_],8**5),"";vec($g,$_*$h,1)=($i=length$f[$_]<8**5);vec($j,$_||$h,1) +=!!$i}select$g,$j,$k,5;goto L' perlmonks.org 80

It is automatically generated from this more readable version:

$0=perl; socket($socket, AF_INET, SOCK_STREAM, 0) && connect($socket, sockaddr_in PORT, inet_aton "SERVER") || die $!; fcntl $_, F_SETFL, O_NONBLOCK|fcntl $_, F_GETFL, 0 for @in = (*STDIN, +$socket), @out = ($socket, *STDOUT); L: for (0, 1) { sysread ($in[$_], $buffer, 8**5) || exit and $buffer[$_] .= $buffe +r if vec $iv, $_ * ($socket_fileno = fileno $socket), 1; substr $buffer[$_], 0, syswrite($out[$_], $buffer[$_], 8**5), ""; vec($iv, $_ * $socket_fileno, 1) = ($l = length $buffer[$_] < 8**5 +); vec($ov, $_ || $socket_fileno, 1) = !!$l; } select $iv, $ov, $u, 5; goto L

In reply to golf challenge: one-liner netcat clone by salva

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.