Accessing Novell Services from Perl on NetWare
Articles and Tips: article
Software Consultant
Novell Applications Group
sguruprasad@novell.com
01 Oct 2000
Perl is a popular scripting language used to generate Web pages and automate various aspects of interfacing with Web servers. This AppNote shows how to access Novell network services, specifically Novell Directory Services (NDS), from within a Perl script using either a Universal Component System Extension or Java Beans.
Introduction
Perl is the acronym for Practical Extraction and Report Language. Perl is a very powerful language for text processing, pattern matching, and report generation. In addition, it can be used for Web page generation and automation activities.
Perl was created by Larry Wall and is maintained by Perl porters. Perl 5.6.0 is the latest shipping version. Perl on NetWare is based on the ActiveState Windows 32-bit version of Perl 5.003_07.
Perl on NetWare offers all of the above features as well as the ability to access Novell services such as Directory, File I/O, FTP, Server Management, Volume administration, and so on. This AppNote focuses on using Perl in Novell environments and explains how to access Novell services, specifically Novell Directory Services (NDS). The same concepts can also be applied to other Novell services.
Universal Component System
The technology that makes it possible to access Novell services from Perl is the Universal Component System (UCS). It is an infrastructure that enables a scripting language to consume Universal Component Extension (UCX) components, or Java Beans and Classes, or Remote ActiveX controls. Novell offers a collection of UCX components, Java Beans, and ActiveX controls that encapsulate various Novell services like Directory, Database, Server Management, and File I/O. All of these can be used from Perl.
The Perl-UCS Extension
The Perl-UCS extension implements the standard Fetch and Store semantics. This extension takes care of routing the data between script and the component using UCS APIs. It also does the necessary data type conversions between UCS and Perl, and it invokes the methods of the components and sets/gets the property values.
Common Tasks
Before we jump into a case study, let's take a look at the way some of the most common tasks are done in Perl.
Instantiating an Object. This is done by calling the new method of the UCSExt package. For example:
$Dir = UCSExt->new("UCX:NWDIR")
Note the use of the Arrow operator (a hyphen and a greater-than symbol, typed in succession), which is also used to call methods and to get/set properties.
Calling Methods. This is done by invoking the method with the required parameters. For example:
$Dir = UCSExt->new("UCX:NWDIR") $Ret= $Dir->Login("username", "password")
Here the Login method is called on the instantiated directory object.
Get/Set Properties. The "get" and "set" property functions are accessed the same way. For example:
$Filter = $Dir->{"filter"} $Filter->{"derefAlias"} = true
The first code snippet shows how to get the Filter object, which is a property in the directory object. The second one shows how to set a property.
Advantages
Using Perl along with UCS helps you create applications that can leverage the power of NDS as well as other Novell services. Perl can be combined with the Novell services to create very powerful applications. There is no need to learn the UCS--the extension itself takes care of all the details required to work with it.
Perl on NetWare Examples
Once you understand how to call methods and properties, it is important to understand the components. The latest documentation and binaries for the Perl components are available in the Novell Developer Kit (NDK) at http://developer.novell.com\ndk. Specific items to look at are the following:
http://developer.novell.com\ndk\perl5.htm
http://developer.novell.com\ndk\ucs.htm
http://developer.novell.com\ndk\nscript.htm
http://developer.novell.com\ndk\bns.htm
In the following sections, we will look at two usage examples: one accessing NDS using the NWDIR UCX component, and another also accessing NDS but using the Directory bean.
Accessing NDS Using the UCX Component
The following code snippet allows you to access the NWDIR UCX Component from a Perl script to enumerate the contents of the directory at a specified context.
Note: Comments are embedded within the scripts so that they can be easily understood and can be copied to a server and executed with minimal modification. If you do paste these scripts to some other file, make sure you replace any extended ASCII character that shows up between the object variable and method/property name with the Arrow operator.
use UCSExt; #This will pull in the definitions and declarations from the module #file. Also this loads the necessary module into memory if not yet #loaded. #In this case it imports definitions and declarations from UCSExt.pm #present in sys:\perl\lib directory and also loads UCSExt.nlp which is #an NLM from sys:\perl\lib\auto\ucsext directory. #Also this loads UCSCore.nlm, which is in sys:\system directory. $Dir = UCSExt->new("UCX:NWDIR") or die "Unable to create Directory object \n"; #This instantiates the directory object, and stores it as a Perl object #for later use. #This loads UCS2UCX.nlm and UCXMgr.nlm from sys:\system directory. #Also it loads _nwdir.nlm which is the directory UCX component from #sys:\ucs\ucx directory. $Dir->Login("username", "password") or die "Failed to login. Please check the username and password.\n"; #This will call the login method of the directory object and logs into #the tree with the specified user name and password. #If the user is in a different context, set the 'FullName' property #with the correct context before calling the login method. #$Dir->{"FullName"} = "nds:\\\\<treename>\\<context>"; #Also if you want to enumerate the contents of a different context, set #the 'FullName' property before starting the enumeration $Entries = $Dir->{"Entries"}; #This gets the entries object, which is a property in the directory #object $Entries->Reset(); #This calls the 'reset' method of the entries object, which initializes #for a search of all entry objects. #The following loop, gets all the entry objects, by calling the next #method of the entries object. Also this prints the 'shortname' which #is a property on the entry object. while($Entries->HasMoreElements()) { $Entry = $Entries->Next(); $ShortName = $Entry->{"ShortName"}; print "$ShortName \n"; } #The following calls the 'logout' method of the directory object, which #logs out the user out of the directory service. $Dir->Logout();
Accessing NDS from a Java Bean
The following code snippet allows you to access the NWDIR Java Bean from a Perl script to enumerate the contents of the directory at a specified context.
When using these scripts, make sure that the jar files used are set in the classpath list. Otherwise, instantiation fails because the necessary jar files cannot be found. An example classpath setting is shown below:
envset classpath=$classpath;sys:\java\beans\nwdir.jar
This setting can be put in an NCF file, saved under the SYS:\SYSTEM directory, and executed once. Also make sure that your classpath includes the following two entries apart from any others:
sys:\java\lib\ucs.jarsys:\java\lib\swingall.jar
use UCSExt; #This will pull in the definitions and declarations from the module #file. Also this loads the necessary module into memory if not yet #loaded. #In this case it imports definitions and declarations from UCSExt.pm #present in sys:\perl\lib directory and also loads UCSExt.nlp which is #an NLM from sys:\perl\lib\auto\ucsext directory. #Also this loads UCSCore.nlm, which is in sys:\system directory. $Dir = UCSExt->new("NWDir(NWDir)") or die " Unable to instantiate the Directory bean \n"; #This instantiates the Directory bean (nwdir.jar) from sys:\java\beans, #and stores it as a Perl object for later use. This loads UCS2Java.nlm #from sys:\system directory. #Get the user to enter the fullname from where the objects needs to #enumerated print "Enter this server's NDS FullName(NDS:\\\\Tree Name\\Organization):\n"; $FullName = <STDIN>; chop $FullName; #Sets the 'fullname' property of Directory bean to the input fullname $Dir->{"FullName"} = $FullName; #Calls the 'getEntry' method of the Directory bean to get the entry #object $RootEntry = $Dir->getEntry(); #Gets all the entries $Entries = $RootEntry->{"Entries"} or die "Unable to get entries object\n"; #The following loop, gets all the entry objects, by calling the 'next' #method. Also this prints the 'shortname' while ($Entries->HasMoreElements()) { $Entry = $Entries->Next(); $ShortName = $Entry->{"ShortName"}; print "$ShortName \n"; }
Conclusion
The examples presented above show how easy it is to access Novell services from a scripting language like Perl. Even though the samples use NDS, the same concepts and techniques can be used with various other Novell services to create powerful custom Web applications.
* Originally published in Novell AppNotes
Disclaimer
The origin of this information may be internal or external to Novell. While Novell makes all reasonable efforts to verify this information, Novell does not make explicit or implied claims to its validity.