chiark / gitweb /
remoteshell as found on jura
authorian <ian>
Thu, 19 Jul 2001 20:00:22 +0000 (20:00 +0000)
committerian <ian>
Thu, 19 Jul 2001 20:00:22 +0000 (20:00 +0000)
files/personal_scripts_bin_remoteshell [new file with mode: 0755]

diff --git a/files/personal_scripts_bin_remoteshell b/files/personal_scripts_bin_remoteshell
new file mode 100755 (executable)
index 0000000..1be2331
--- /dev/null
@@ -0,0 +1,91 @@
+#!/usr/bin/perl --
+# Perl wrapper around rsh/remsh to export DISPLAY and run an xterm
+# Usage:
+#  remoteshell hostname [-l remoteuser]
+
+$rsh= 'ssh';
+$usesh= '';
+
+$rsh= shift if $ARGV[0] =~ s/^-r//;
+$usesh= shift if $ARGV[0] =~ s/^-s//;
+
+($remotehost= shift) || die "need hostname\n";
+$remotehost= (gethostbyname($remotehost))[0] || $remotehost;
+
+if ($ARGV[0] eq '-l') {
+    shift;
+    ($remoteuser= shift) || die "need username after -l\n";
+    $remoteuser= "-l $remoteuser";
+} elsif (open(H,"$ENV{'HOME'}/.rhosts")) {
+    while (<H>) {
+        m/^(\S+)\s+(\S+)\s*$/ || next;
+        if (&canon($1) eq &canon($remotehost)) {
+            $remoteuser= "-l $2";
+            last;
+        }
+    }
+}
+
+$_= $ENV{'DISPLAY'};
+m/:/ || die "\$DISPLAY variable format `$_' bad";
+$ENV{'DISPLAY'}= &canon($`).':'.$';
+
+sub canon {
+    local ($h) = @_;
+    local ($v,@h,@v);
+    chomp($h= `hostname`) unless length($h);
+    @h= gethostbyname($h);
+    return $h unless @h;
+    @v= gethostbyaddr($h[4],$h[2]);
+    $v= $v[0];
+#    unless ($v =~ m/\./) {
+#        $v .= '.';
+#        $v .= `domainname`;
+#        $v =~ s/\n$//;
+#    }
+    return $v;
+}
+
+open(U,"|$rsh $remotehost $remoteuser perl")
+    || die "failed to run $rsh: $!";
+
+print U <<'END' ;
+    $_= <DATA>; chop; @ar= split(/\0/,$_) if length($_);
+    $_= <DATA>; chop; $ENV{'DISPLAY'}= $_;
+    $_= <DATA>; chop; $usesh= $_;
+    $host= <DATA>; chop($host);
+    $pcmd= '. .configs/setenvir; echo ok $PATH';
+    $pout= `$pcmd`;
+    $pout =~ m/^ok (\S+)\n$/ || die "pcmd gave `$pout'";
+    $ENV{'PATH'}= $1;
+    open(X,"|xauth nmerge -");
+    while (<DATA>) { print X; }
+    close(X);
+    $? && die "xauth gave code $?";
+    defined($c= fork) || die "fork: $!";
+    exit 0 if $c;
+    close(STDIN); close(STDOUT);
+    open(STDIN,"</dev/null") || die "/dev/null for stdin: $!";
+    open(STDOUT,">>.remoteshell-errors") || die "append .remoteshell-errors: $!";
+    chmod(0600,".remoteshell-errors");
+    close(STDERR); open(STDERR,">&STDOUT");
+    if ($usesh eq '') {
+        exec('close3onwards','xterm','-T',$host,@ar,'-e','.configs/rxprofile');
+    } else {
+       exec('close3onwards',$usesh,'-xc','. .configs/setenvir; exec "$@"','-',@ar);
+    }
+    print STDERR "close3onwards: $!\n";
+    exit 1;
+__END__
+END
+
+print U join("\0",@ARGV),"\n";
+print U $ENV{'DISPLAY'},"\n";
+print U $usesh,"\n";
+print U $remotehost,"\n";
+print U `xauth nextract - $ENV{'DISPLAY'}`;
+
+close(U);
+$? && die "remoteshell $rsh gave code $?";
+
+exit 0;