chiark / gitweb /
xset b off
[ian-dotfiles.git] / from-cvs / files / personal_scripts_bin_remoteshell
1 #!/usr/bin/perl --
2 # Perl wrapper around rsh/remsh to export DISPLAY and run an xterm
3 # Usage:
4 #  remoteshell hostname [-l remoteuser]
5
6 $rsh= 'ssh';
7 $usesh= '';
8
9 $rsh= shift if $ARGV[0] =~ s/^-r//;
10 $usesh= shift if $ARGV[0] =~ s/^-s//;
11
12 ($remotehost= shift) || die "need hostname\n";
13 $remotehost= (gethostbyname($remotehost))[0] || $remotehost;
14
15 if ($ARGV[0] eq '-l') {
16     shift;
17     ($remoteuser= shift) || die "need username after -l\n";
18     $remoteuser= "-l $remoteuser";
19 } elsif (open(H,"$ENV{'HOME'}/.rhosts")) {
20     while (<H>) {
21         m/^(\S+)\s+(\S+)\s*$/ || next;
22         if (&canon($1) eq &canon($remotehost)) {
23             $remoteuser= "-l $2";
24             last;
25         }
26     }
27 }
28
29 $_= $ENV{'DISPLAY'};
30 m/:/ || die "\$DISPLAY variable format `$_' bad";
31 $ENV{'DISPLAY'}= &canon($`).':'.$';
32
33 sub canon {
34     local ($h) = @_;
35     local ($v,@h,@v);
36     chomp($h= `hostname`) unless length($h);
37     @h= gethostbyname($h);
38     return $h unless @h;
39     @v= gethostbyaddr($h[4],$h[2]);
40     $v= $v[0];
41 #    unless ($v =~ m/\./) {
42 #        $v .= '.';
43 #        $v .= `domainname`;
44 #        $v =~ s/\n$//;
45 #    }
46     return $v;
47 }
48
49 open(U,"|$rsh $remotehost $remoteuser perl")
50     || die "failed to run $rsh: $!";
51
52 print U <<'END' ;
53     $_= <DATA>; chop; @ar= split(/\0/,$_) if length($_);
54     $_= <DATA>; chop; $ENV{'DISPLAY'}= $_;
55     $_= <DATA>; chop; $usesh= $_;
56     $host= <DATA>; chop($host);
57     $pcmd= '. .configs/setenvir; echo ok $PATH';
58     $pout= `$pcmd`;
59     $pout =~ m/^ok (\S+)\n$/ || die "pcmd gave `$pout'";
60     $ENV{'PATH'}= $1;
61     open(X,"|xauth nmerge -");
62     while (<DATA>) { print X; }
63     close(X);
64     $? && die "xauth gave code $?";
65     defined($c= fork) || die "fork: $!";
66     exit 0 if $c;
67     close(STDIN); close(STDOUT);
68     open(STDIN,"</dev/null") || die "/dev/null for stdin: $!";
69     open(STDOUT,">>.remoteshell-errors") || die "append .remoteshell-errors: $!";
70     chmod(0600,".remoteshell-errors");
71     close(STDERR); open(STDERR,">&STDOUT");
72     if ($usesh eq '') {
73         exec('close3onwards','xterm','-T',$host,@ar,'-e','.configs/rxprofile');
74     } else {
75         exec('close3onwards',$usesh,'-xc','. .configs/setenvir; exec "$@"','-',@ar);
76     }
77     print STDERR "close3onwards: $!\n";
78     exit 1;
79 __END__
80 END
81
82 print U join("\0",@ARGV),"\n";
83 print U $ENV{'DISPLAY'},"\n";
84 print U $usesh,"\n";
85 print U $remotehost,"\n";
86 print U `xauth nextract - $ENV{'DISPLAY'}`;
87
88 close(U);
89 $? && die "remoteshell $rsh gave code $?";
90
91 exit 0;