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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.