Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.2 KiB

  1. #! /usr/bin/perl
  2. # $RCSfile: gsh,v $$Revision: 4.1 $$Date: 92/08/07 17:20:20 $
  3. # Do rsh globally--see man page
  4. $SIG{'QUIT'} = 'quit'; # install signal handler for SIGQUIT
  5. sub getswitches {
  6. while ($ARGV[0] =~ /^-/) { # parse switches
  7. $ARGV[0] =~ /^-h/ && ($showhost++,$silent++,shift(@ARGV),next);
  8. $ARGV[0] =~ /^-s/ && ($silent++,shift(@ARGV),next);
  9. $ARGV[0] =~ /^-d/ && ($dodist++,shift(@ARGV),next);
  10. $ARGV[0] =~ /^-n/ && ($n=' -n',shift(@ARGV),next);
  11. $ARGV[0] =~ /^-l/ && ($l=' -l ' . $ARGV[1],shift(@ARGV),shift(@ARGV),
  12. next);
  13. last;
  14. }
  15. }
  16. do getswitches(); # get any switches before class
  17. $systype = shift; # get name representing set of hosts
  18. do getswitches(); # same switches allowed after class
  19. if ($dodist) { # distribute input over all rshes?
  20. `cat >/tmp/gsh$$`; # get input into a handy place
  21. $dist = " </tmp/gsh$$"; # each rsh takes input from there
  22. }
  23. $cmd = join(' ',@ARGV); # remaining args constitute the command
  24. $cmd =~ s/'/'"'"'/g; # quote any embedded single quotes
  25. $one_of_these = ":$systype:"; # prepare to expand "macros"
  26. $one_of_these =~ s/\+/:/g; # we hope to end up with list of
  27. $one_of_these =~ s/-/:-/g; # colon separated attributes
  28. @ARGV = ();
  29. push(@ARGV,'.grem') if -f '.grem';
  30. push(@ARGV,'.ghosts') if -f '.ghosts';
  31. push(@ARGV,'/etc/ghosts');
  32. $remainder = '';
  33. line: while (<>) { # for each line of ghosts
  34. s/[ \t]*\n//; # trim trailing whitespace
  35. if (!$_ || /^#/) { # skip blank line or comment
  36. next line;
  37. }
  38. if (/^(\w+)=(.+)/) { # a macro line?
  39. $name = $1; $repl = $2;
  40. $repl =~ s/\+/:/g;
  41. $repl =~ s/-/:-/g;
  42. $one_of_these =~ s/:$name:/:$repl:/; # do expansion in "wanted" list
  43. $repl =~ s/:/:-/g;
  44. $one_of_these =~ s/:-$name:/:-$repl:/;
  45. next line;
  46. }
  47. # we have a normal line
  48. @attr = split(' '); # a list of attributes to match against
  49. # which we put into an array
  50. $host = $attr[0]; # the first attribute is the host name
  51. if ($showhost) {
  52. $showhost = "$host:\t";
  53. }
  54. $wanted = 0;
  55. foreach $attr (@attr) { # iterate over attribute array
  56. $wanted++ if index($one_of_these,":$attr:") >= 0;
  57. $wanted = -9999 if index($one_of_these,":-$attr:") >= 0;
  58. }
  59. if ($wanted > 0) {
  60. print "rsh $host$l$n '$cmd'\n" unless $silent;
  61. $SIG{'INT'} = 'DEFAULT';
  62. if (open(PIPE,"rsh $host$l$n '$cmd'$dist 2>&1|")) { # start an rsh
  63. $SIG{'INT'} = 'cont';
  64. for ($iter=0; <PIPE>; $iter++) {
  65. unless ($iter) {
  66. $remainder .= "$host+"
  67. if /Connection timed out|Permission denied/;
  68. }
  69. print $showhost,$_;
  70. }
  71. close(PIPE);
  72. } else {
  73. print "(Can't execute rsh: $!)\n";
  74. $SIG{'INT'} = 'cont';
  75. }
  76. }
  77. }
  78. unlink "/tmp/gsh$$" if $dodist;
  79. if ($remainder) {
  80. chop($remainder);
  81. open(grem,">.grem") || (printf stderr "Can't make a .grem file: $!\n");
  82. print grem 'rem=', $remainder, "\n";
  83. close(grem);
  84. print 'rem=', $remainder, "\n";
  85. }
  86. # here are a couple of subroutines that serve as signal handlers
  87. sub cont {
  88. print "\rContinuing...\n";
  89. $remainder .= "$host+";
  90. }
  91. sub quit {
  92. $| = 1;
  93. print "\r";
  94. $SIG{'INT'} = '';
  95. kill 2, $$;
  96. }