[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-unattended/var/se3/unattended/install/bin/ -> auconfig.pl (source)

   1  # Script to configure Automatic Updates from a command prompt.
   2  # Part of <http://unattended.sourceforge.net/>.
   3  # 2004-02-15 Nils Østbjerg <no@business.aau.dk>
   4  
   5  use warnings;
   6  use strict;
   7  use Getopt::Long;
   8  use Pod::Usage;
   9  
  10  my %opts;
  11  GetOptions (\%opts, 'help|h|?', 'day=i', 'time=i', 'remote=s', 'wait=i',
  12              'sus=s', 'status=s', 'noautoreboot', 'policy')
  13    or pod2usage (2);
  14  
  15  (exists $opts{'help'})
  16    and pod2usage ('-exitstatus' => 0, '-verbose' => 2);
  17  
  18  # Ensure exactly one argument after options.
  19  scalar @ARGV == 1
  20    or pod2usage (2);
  21  
  22  my ($au_options) = @ARGV;
  23  
  24  
  25  ## Initialize the hashes 
  26  
  27  my %au_values = ('ScheduledInstallDay' => 255, 
  28           'ScheduledInstallTime' => 255,
  29                   'RescheduleWaitTime' => 255,
  30                   'AUOptions' => 255,
  31           'NoAutoRebootWithLoggedOnUsers' => 255,
  32           'NoAutoUpdate' => 255,
  33           'UseWUServer' => 255
  34          );
  35  
  36  my %wu_values = ('WUServer' => '',
  37                   'WUStatusServer' => '',
  38                  );
  39  
  40  my %old_values = ('AUOptions' => 254, 
  41            'AUState' => 254,
  42            'ScheduledInstallDay' => 255,
  43            'ScheduledInstallTime' => 255
  44           );
  45  
  46  ## Validate all options.
  47  
  48    
  49  if ($opts{'policy'}) { 
  50  # Use policy settings.
  51    if ($au_options eq "off" || $au_options eq "1") {
  52        (exists $opts{'day'} || exists $opts{'time'}
  53         || exists $opts{'wait'} || exists $opts{'sus'}
  54         || exists $opts{'status'} || exists $opts{'noautoreboot'})
  55            and die ("only the --remote option is allowed for this mode\n");
  56      
  57      $au_values{'NoAutoUpdate'} = 1;
  58      
  59    } elsif ($au_options eq "notify" || $au_options eq "2") {
  60      (exists $opts{'day'} || exists $opts{'time'} || exists $opts{'wait'})
  61        and die ("--day, --time and --wait options is not allowed for this mode\n");
  62      
  63      $au_values{'AUOptions'} = 2;
  64      
  65    } elsif ($au_options eq "download"|| $au_options eq "3" ) {
  66      (exists $opts{'day'} || exists $opts{'time'} || exists $opts{'wait'}) 
  67        and die ("--day, --time and --wait options is not allowed for this mode\n");
  68      
  69      $au_values{'AUOptions'} = 3;
  70      
  71    } elsif ($au_options eq "install"|| $au_options eq "4" ) {
  72      (exists $opts{'day'} && exists $opts{'time'} && exists $opts{'wait'})
  73        or die "--day, --time and --wait options required for this mode\n";
  74      
  75      if ($opts{'day'} =~ /^[01234567]$/) {
  76        $au_values{'ScheduledInstallDay'} = $opts{'day'};
  77      } else {
  78        die "--day must be between 0 and 7\n";
  79      }
  80      
  81      if ($opts{'time'} =~ /^\d+$/ && $opts{'time'} >= 0 && $opts{'time'} <= 23) {
  82        $au_values{'ScheduledInstallTime'} = $opts{'time'};
  83      } else {
  84        die "--time must be between 0 and 23\n";
  85      }
  86      
  87      if ($opts{'wait'} =~ /^\d+$/ && $opts{'wait'} >= 1 && $opts{'wait'} <= 60) {
  88        $au_values{'RescheduleWaitTime'} = $opts{'wait'};
  89      } else {
  90        die "--wait must be between 1 and 60\n";
  91      }
  92      
  93      $au_values{'AUOptions'} = 4;
  94      
  95    } else {
  96      die "Argument must be 1, off, 2, notify, 3, download, 4, or install.\n";
  97    }
  98  
  99    if (exists $opts{'noautoreboot'}) {
 100      $au_values{'NoAutoRebootWithLoggedOnUsers'} = 1;
 101    }
 102    
 103    if (exists $opts{'sus'}) {
 104      if ($opts{'sus'} =~ /^http\:\/\/.+$/ ) {
 105        $wu_values{'WUServer'} = $opts{'sus'}; 
 106        $au_values{'UseWUServer'} = 1; 
 107      } else {
 108        die ("syntax is : --sus http://susserver.example.com");
 109      }
 110    }
 111    
 112    if (exists $opts{'status'}) {
 113      if ($opts{'status'} =~ /^http\:\/\/.+$/ ) {
 114        $wu_values{'WUStatusServer'} = $opts{'status'}; 
 115      } else {
 116        die ("syntax is : --status http://susserver.example.com");
 117      }
 118    }
 119  } else {
 120  # Use old settings, and allow admins to configure it.
 121  
 122    if ($au_options eq "off" || $au_options eq "1") {
 123      (exists $opts{'day'} || exists $opts{'time'} )
 124        and die ("only the --remote option is allowed for this mode\n");
 125      
 126      $old_values{'AUOptions'} = 1;
 127      $old_values{'AUState'} = 7;
 128          
 129    } elsif ($au_options eq "notify" || $au_options eq "2") {
 130      (exists $opts{'day'} || exists $opts{'time'})
 131        and die ("--day and --time options is not allowed for this mode\n");
 132      
 133      $old_values{'AUOptions'} = 2;
 134      $old_values{'AUState'} = 2;
 135      
 136    } elsif ($au_options eq "download"|| $au_options eq "3" ) {
 137      (exists $opts{'day'} || exists $opts{'time'})
 138        and die ("--day and --time options is not allowed for this mode\n");
 139      
 140      $old_values{'AUOptions'} = 3;
 141      $old_values{'AUState'} = 2;
 142      
 143    } elsif ($au_options eq "install"|| $au_options eq "4" ) {
 144      (exists $opts{'day'} && exists $opts{'time'})
 145        or die "--day and --time options required for this mode\n";
 146      
 147      if ($opts{'day'} =~ /^[01234567]$/) {
 148        $old_values{'ScheduledInstallDay'} = $opts{'day'};
 149      } else {
 150        die "--day must be between 0 and 7\n";
 151      }
 152      
 153      if ($opts{'time'} =~ /^\d+$/ && $opts{'time'} >= 0 && $opts{'time'} <= 23) {
 154        $old_values{'ScheduledInstallTime'} = $opts{'time'};
 155      } else {
 156        die "--time must be between 0 and 23\n";
 157      }
 158  
 159      $old_values{'AUOptions'} = 4;
 160      $old_values{'AUState'} = 2;
 161    } else {
 162      die "Argument must be 1, off, 2, notify, 3, download, 4, or install.\n";
 163    }
 164  
 165    if (exists $opts{'noautoreboot'} || exists $opts{'sus'} || exists $opts{'status'}) {
 166      die ("The --policy option is required for the use of this option.\n");
 167    }
 168    
 169  }
 170  
 171  
 172  ## Change the registry settings.
 173  
 174  my %reg;
 175  use Win32::TieRegistry (Delimiter => '/', TiedHash => \%reg);
 176  
 177  my $key_name = ('HKEY_LOCAL_MACHINE/SOFTWARE/Policies/Microsoft/Windows/');
 178  my $wu_key_name = ('WindowsUpdate/');
 179  my $au_key_name = ('AU/');
 180  
 181  ($opts{'remote'})
 182    and $key_name = "//$opts{'remote'}/$key_name";
 183  
 184  my $key = $reg{$key_name};
 185  
 186  defined $key 
 187    or die "Unable to open $key_name";
 188  
 189  foreach my $name (sort keys %wu_values) {
 190    my $val = $wu_values{$name};
 191    if ($val eq '') {   
 192      if (exists $key->{"$wu_key_name$name"}) {
 193        (delete $key->{"$wu_key_name$name"})
 194      or die "Unable to delete $key_name$wu_key_name$name: $^E";
 195      }
 196    } else {
 197      $key->{"$wu_key_name"}= { "/$name" => $val}
 198        or die "Unable to set $key_name$wu_key_name$name to $val: $^E";
 199    }
 200  }
 201  
 202  foreach my $name (sort keys %au_values) {
 203    my $val = $au_values{$name};
 204    if ($val == 255 ) {
 205      if (exists $key->{"$wu_key_name$au_key_name$name"}) {
 206        (delete $key->{"$wu_key_name$au_key_name$name"})
 207      or die "Unable to delete $key_name$wu_key_name$au_key_name$name: $^E";
 208      }
 209    } else {
 210      $key->{"$wu_key_name"}= { "$au_key_name" => 
 211                    {
 212                     "/$name" =>  [ pack("L", $val), "REG_DWORD" ] 
 213                    }
 214                  }
 215        or die "Unable to set $key_name$au_key_name$name to $val: $^E";
 216    }
 217  }
 218  
 219  my $old_key_name = ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/'
 220              . 'CurrentVersion/WindowsUpdate/Auto Update/');
 221  
 222  ($opts{'remote'})
 223    and $old_key_name = "//$opts{'remote'}/$old_key_name";
 224  
 225  my $old_key = $reg{$old_key_name};
 226  
 227  defined $old_key 
 228    or die "Unable to open $old_key_name";
 229  
 230  foreach my $name (sort keys %old_values) {
 231    my $val = $old_values{$name};
 232    if ($val == 255 ) {
 233      if (exists $old_key->{"$name"}) {
 234        delete $old_key->{"$name"} 
 235      or die "Unable to delete $old_key_name$name: $^E";
 236      }
 237    } elsif ($val != 254) {
 238      $old_key->{"/$name"} =  [ pack("L", $val), "REG_DWORD" ]
 239        or die "Unable to set $old_key_name$name to $val: $^E";
 240    }
 241  }
 242  
 243  __END__
 244  
 245  =head1 NAME
 246  
 247  auconfig.pl - Configure Windows AutoUpdate
 248  
 249  =head1 SYNOPSIS
 250  
 251  auconfig.pl [ options ] <command>
 252  
 253  Commands:
 254  
 255   1 | off         Disable automatic updates
 256   2 | notify      Notify user before download
 257   3 | download    Download automatically; notify user before install
 258   4 | install     Download and install automatically 
 259  
 260  Options:
 261  
 262   --help           Display verbose help and exit (RECOMMENDED)
 263   --policy         Use system policy settings
 264   --remote <host>  Operate on <host> instead of local machine     
 265   --day <day>      Day of week for autoupdate (1 == Sunday; 0 == every day)
 266   --time <hour>    Time for auto update (0 == midnight, 23 == 11 P.M.)
 267   --wait <min>     How long after a restart a rescheduled install starts  
 268   --sus <url>      Specifies the URL address of the SUS server to be used
 269   --status <url>   Specifies the URL address of the SUS stastistics server
 270   --noautoreboot   Turns off automatic reboots while users are logged on
 271  
 272  =head1 DESCRIPTION
 273  
 274  auconfig.pl configures the Windows 2000 and XP "Auto Updates"
 275  facility. 
 276  
 277  The off command allows no options, except --policy and --remote.
 278  
 279  The notify and download commands requires no options, but allows all 
 280  excepts the --day, --time and --wait options.   
 281  
 282  All options are allowed with the install command, and the --day,
 283  and --time are required. If the --policy option is used the --wait
 284  option is required as well.
 285  
 286  The --policy option is required for all options except --remote, 
 287  --day and --time. When the --policy option is use the settings is 
 288  a system policy, which cant be changed from the GUI.
 289  
 290  The --day option specifies which weekday the automatic installation 
 291  runs. The value must be between 0 and 7.  1 means Sunday, 2 
 292  means Monday, and 7 means Saturday.  0 means every day.
 293  
 294  The --time option specifies at which time the automatic installation
 295  runs. The value must be between 0 and 23.  0 mean midnight, 1 means 
 296  1 A.M., 12 means noon, and 23 means 11 P.M.
 297  
 298  With the --wait you specify the amount of minutes a computer waits
 299  before preforming installations which was scheduled to take place while
 300  the computer was off. The range is from 1 to 60 minutes.
 301  
 302  The --sus option enables Automatic Updates to use the Software Update 
 303  Services server specified. 
 304  The format of the url is http://susserver.example.com
 305  
 306  The --status allows you to specify the SUS statistics server. The format of
 307  the url is the same as with --sus.
 308  
 309  The --noautoreboot option makes Automatic Update present the user 
 310  with a request for a system reboot if one is required, instead of the
 311  default behavior where the system will reboot if required, something
 312  that may result in loss of data.
 313  
 314  
 315  =head1 SEE ALSO
 316  
 317  C<http://windows2000faq.com/articles/?articleid=26221>
 318  C<http://support.microsoft.com/?id=328010>
 319  C<http://www.microsoft.com/resources/documentation/WindowsServ/2003/all/deployguide/en-us/dmebh_sus_wviy.asp>


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1