Perl Scripts for NT & LDAP

Home Page

You can use these scripts in any way you want and welcome to send questions/suggestions. I am in no way responsible for any problems you come across using these scripts. The module object usage may need some path modifications to work under the present builds of Perl.   Please understand that these scripts were written to work under older builds of Perl.

I put some working Perl scripts for NT administration on this page. These scripts need some minimum modifications to suit the individual environments. I put some explanation notes for easy understanding of the scripts. I my self used all these scripts in a production environment. Particularly the remote registry update script saved lot of man-hours in a production environment where the proxy server setting had to be changed on around 700 machines for Netscape browser v3.x. I advice not to use these scripts in a production environment if you do not understand the scripts completely. I feel that these scripts certainly show some stuff of Perl programming for NT administration.

LDAP compatible ADS search

The following script can bind to an LDAP compatible directory service like Windows Active Directory Service and browse for the data under a container.

# Perl Script to browse LDAP compatible directory
# Author Sai Prasad Kesavamatham, 2002
# Tested under Windows 2000 ADS

use Net::LDAP;

# $mesg->code && die $mesg->error;
# foreach $entry ($mesg->all_entries) { $entry->dump; }
# $ldap->unbind; # take down session

$ldap = Net::LDAP->new('10.10.10.5');
$mesg = $ldap->bind( "dn=mydomain, cn=saiprasadk", password => "mypassword");
# $mesg = $ldap->bind;
$mesg = $ldap->search ( # perform a search
base => " o=santaclara",
filter => "(cn=sai*)"
);
$mesg->code && die $mesg->error;
foreach $entry ($mesg->all_entries) { $entry->dump; }
$ldap->unbind; # take down session

Remote Registry Update

Situation: The proxy server for the site has been moved to a new host with a different name and the auto-proxy setting under Netscape 3.x needs modification to point to the new server. This basically sits in the Registry under the key \\HKEY_USERS\SID\Software\Netscape\Netscape Navigator\Proxy Information for each individual user. This at any point of time represents the setting for that logged_in user only. The script has to be run more than once to change for all the existing users. Also note that the workstations are not in a domain and you need the common workstation admin login id and password.

# Perl Script to update registry remotely
# Author Sai Prasad Kesavamatham, 1998
# Tested under Windows NT 4.0

# Program to change the Registry keys for Netscape-3 Proxy server config information 

# and Proxy Type On remote machines
# Events are logged into Registry.log

use Win32;
require 'NT.ph';
$key="";
$proxy_url="http://www-xyz.domain.com/new.proxy"; # New proxy setting
$proxy_type="2"; # Represents auto-proxy
open (machines, "machines.txt");   # List of machines of IP Addresses
open (out, ">registry.log"); # Output file for error and result logs
chop($line=<machines>);
while ($line ne ""){
@array=split(/\s+/,$line);
$machine=@array[1];
print $machine;
$disconnect="net use \\\\$machine /d";
$connection="net use \\\\$machine";
system($connection); # Connect to the next available machine in the list
# Try to connect to the HKEY_USERS of the remote machine and fail if not able to connect

unless (Win32::RegConnectRegistry($machine,&HKEY_USERS, $key_LM)) {
print out "$machine: ERROR, unable to connect to "."remote registry. Check user-id\n";
goto next_machine;
};
# Try to open HKEY_USERS and fail if not able to open
unless (Win32::RegOpenKeyEx($key_LM, $key, &NULL, &KEY_ALL_ACCESS, $key_Root)){
print out ("Not able to connect to HKEY_USERS\n");
goto next_machine;
}
# First key is always default; I skipp that key in this script
$i=1; # ***Skipping .DEFAULT key

# *** GO through SIDs if more than 1 available
while (Win32::RegEnumKey($key_Root,$i++,$key_Name)){
$SID=$key_Name;
print out "$machine\t SID=$SID\n";
# Set the key to the exact location and try to open

$key1="$SID\\Software\\Netscape\\Netscape Navigator\\Proxy Information";
# ***Opening the exact Key ***
 if (Win32::RegOpenKeyEx($key_LM, $key1, &NULL, &KEY_ALL_ACCESS, $key_Proxy))
 {
Win32::RegSetValueEx($key_Proxy, "Auto Config url", &NULL, ®_SZ, $proxy_url);
Win32::RegSetValueEx($key_Proxy, "Proxy Type", &NULL, ®_DWORD, $proxy_type);
 }
 else {print out ("No key found for $SID\n"); goto bottom;};
# Remove the hashes from the next two lines if you want to just find the existing key values for auto-proxy 

# Win32::RegQueryValueEx($key_Proxy,"Auto config url",&NULL,®_SZ,$data1);

#  Win32::RegQueryValueEx($key_Proxy,"Proxy Type",&NULL,®_SZ,$data2);
 print out "Auto config url=$data1\tProxy Type=$data2\n";
bottom: next;
}
Win32::RegCloseKey($key_Root);
Win32::RegCloseKey($key_LM);
next_machine: chop($line=<machines>);
}
close (out);
close (machines);

Automated User creation

I used this script to automatically create users, their corresponding home directories, to copy any minumum required files and to set the ACLs on the home directories. The idea is to first create all the users using ADDUSERS command from the Resource Kit and then to create the home directories and set the ACLs. The users home directory is assumed to be under E:\USERS on SERVER1. The minimum base files for all new users are available under E:\MODEL\BASE.The initial profile files are available under E:\MODEL\PROFILES.Also note that the user's profile directory has an extension .pds. This is to accommodate the NT4.0 users to login to 3.51 also. The user's profile folder is under E:\PROFILE\USERID.PDS.The user's home directory has been shared and hidden. The acls set using CACLS command. I leave the remaining description for user's understanding. This script is a fairly simple one and can fit any environment with some minor modifications.

# Perl Script to Create Users automatically
# Author Sai Prasad Kesavamatham, 1998
# Tested under Windows NT 4.0
# To Create Users from the file USERS.TXT using ADDUSERS command from Resource Kit
# Edit the USERS.TXT file using the right syntax.
# Requires ADDUSERS.EXE, USERS.TXT and THIS program in the same directory 
# This program has to be run from the main file server on which the user
# folders exist with Administrator account
# Errors are redirected to ERRORS.LOG file
# *******User Creation**********
        $argone="addusers \\\\SERVER1 /c users.txt >errors.log";
        system ($argone);
# *******End of User Creation******
# To copy required base files to users home directories and profile directories
if (open (users, "users.txt")){
  $line=<users>;
  $endmarker="[Global]";
  $line=<users>;
open (logfile, ">>errors.log");
do {  
   @userarray=split(/,/,$line);
# ********** Create the home directory on SERVER1 at E:\USERS***********
   if (mkdir ("e:/users/@userarray[0] ",777)) 
        {
        # ***************Copy home directory files ****************
   $base=" e:\\model\\base\\. e:\\users\\@userarray[0]\\. /s /e";       
   system ("xcopy", $base);
        }
        else 
        {
        print logfile ("Could not create Home directory for ", @userarray[0],"\n");
        }
# ********** Create the profile directory on MEFP01********
   if (mkdir ("e:/profile/@userarray[0].pds ",777)) 
        {
        # ***************Copy profile directory files *********
        $profile=" e:\\model\\profiles\\. e:\\profile\\@userarray[0].pds\\. /s /e";
        system ("xcopy", $profile);
        }
        else 
        {
        print logfile ("Could not create Profile directory for ", @userarray[0],"\n");
        }
chop($line=<users>);
}until($line eq $endmarker);
}
close (logfile);
# To share home directories 
if (open (users, "users.txt")){
  $line=;
  $endmarker="[Global]";
  $line=;
  chop($line);
do {  
   @userarray=split(/,/,$line);
$share="net share @userarray[0]\$=e:\\users\\@userarray[0]";
system ($share,"\n");
chop($line=<users>);
}until ($line eq $endmarker);
}
close (users);
# To set ACL settings on the Home and Profile folders using CACLS
if (open (users, "users.txt")){
  $line=<users>;
  $endmarker="[Global]";
  $line=;
  chop($line);
do {  
   @userarray=split(/,/,$line);
$sharehome1="cacls e:\\users\\@userarray[0] /t /e /g @userarray[0]:c ";
$sharehome2='"domain admins":f "creator owner":f system:f /r everyone';
$shareprofile1="cacls e:\\profile\\@userarray[0].pds /t /e /g @userarray[0]:c ";
$shareprofile2='"domain admins":f "creator owner":f system:f /r everyone';
system ($sharehome1,$sharehome2);
system ($shareprofile1,$shareprofile2,"\n");
        $line=<users>;
        chop($line);
}until ($line eq $endmarker);
}
close (users);

USERS.TXT file creation for the above script

This script is used to create the USERS.TXT file which is needed for running the above script.

# Perl script to create USERS.TXT file
# Author Sai Prasad Kesavamatham, 1998
# prompts for USERID and FULLNAME until NO is typed in & Creates USERS.TXT
# Overwrites the existing USERS.TXT file

system ("cls");
print ("Welcome to the User-ID entry program Developed by Sai Prasad!\n\n");
print ("Type no when prompted for User-ID, if you wish to come out\n\n");
$userid="dumb";
$line="[User]";
$endmarker="[Global]";
$group="[Local]";

if (open (users, ">users.txt"))
{
print users ($line,"\n");
nextline:
$original="test1,Test1,changeme,Created through Addusers,H:,,\\\\SERVER1\\e\$\\profile\\%USERNAME%.USR,logon.bat";
@userarray=split(/,/,$original);
print ("Enter the User-ID ...");
chop($userid=);

if (/L$userid/E ne "no")
{
print ("Enter User's Full Name ...");
chop($fullname=);
@userarray[0]=$userid;
@userarray[1]=$fullname;
@userarray[5]="\\\\SERVER1\\@userarray[0]\$";
@userarray[6]="\\\\SERVER1\\profile\$\\@userarray[0].USR";
@userarray=@userarray[0].",".@userarray[1].",".@userarray[2].",".@userarray[3].",".@userarray[4].",

# The following line is the continuation of the above line

".@userarray[5].",".@userarray[6].",".@userarray[7];
print users (@userarray,"\n");
system ("cls");
print ("Enter the next name...\n");
print ("Type no when prompted for User-ID, if you wish to come out\n\n");
goto nextline;
}
else
{
print users ($endmarker,"\n");
print users (" ","\n");
print users ($group,"\n");
print users (" ","\n");
system ("cls");
print ("Thankyou for using this program!\n");
die ("Program Developed by Sai Prasad!\n\n");;
}
}
close (users);

Automated Event Viewer [Published in August,1998 Windows NT Magazine]

This script has been published in the August Issue of Windows NT magazine under Readers to Readers.

Automated Event Viewer & Roaming Profiles online

# Perl script to view the Warning and STOP messages from the event log from the previous day
# Author Sai Prasad Kesavamatham, 1998
# Perl Script for viewing warnings and errors of the day from event viewer

$node=$ENV{"COMPUTERNAME"};;
system ("net time \\\\$node>tmp");
open (tmp, "tmp");
$line=<tmp>;
@userarray=split(/ /,$line);
$date=@userarray[5];
close tmp;
system ("cls");

@machine=("MACHINE01","MACHINE02"); # Add the machines to this array
foreach $machine (@machine){

@source=("system","security","application");

foreach $source(@source)
{
system ("d:\\perl\\bin\\dumpel -f d:\\perl\\data\\event\\tmp -l $source -s $machine -c");

open (logfile, "tmp");
open (outfile, ">$source.log");
print outfile ("Event log for $machine $source\n");
while ($line=<logfile>)
{
@userarray=split(/,/,$line);
if ((@userarray[0] eq $date) && ((@userarray[2] eq '1') || (@userarray[2] eq '2')))
{
print outfile ("$line\n");
}

};
close (logfile);
close (outfile);
system ("del d:\\perl\\data\\event\\tmp");
}

foreach $source(@source)
{
open (logfile, "$source.log");
chop($line=<logfile>);
if ($line eq "")
{
print ("No events for $source on $date ! Deleting the tmp log files..\n");
close (logfile);
system ("del $source.log");
}
else
{
system ("notepad $source.log");
close (logfile);

Home Page