salva has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: golf challenge: one-liner netcat clone
by ikegami (Patriarch) on Dec 10, 2011 at 22:24 UTC | |
by salva (Canon) on Dec 11, 2011 at 00:23 UTC | |
by ikegami (Patriarch) on Dec 11, 2011 at 00:36 UTC | |
by salva (Canon) on Dec 11, 2011 at 08:57 UTC | |
by ikegami (Patriarch) on Dec 11, 2011 at 09:32 UTC | |
by ikegami (Patriarch) on Dec 11, 2011 at 00:25 UTC | |
|
Re: golf challenge: one-liner netcat clone
by cavac (Prior) on Dec 10, 2011 at 22:08 UTC |