Sample Perl Scripts

Document Sample
Sample Perl Scripts Powered By Docstoc
					Chapter 5
 Sample Perl Scripts                                                                                                      •
                                                                                                                          •
                                                                                                                          •
                                                                                                                          •
                     Juniper Networks provides sample scripts that illustrate how to use Perl to automate access to       •
                     a JUNOScript server and parse its output. To download the samples, access the Juniper                •
                     Networks Technical Documentation web page (www.juniper.net/techpubs) and click on the                •
                     appropriate choice (“Canada and U.S.” or “Worldwide”) under the                                      •
                     “JUNOS Internet Software Download” header.                                                           •
                                                                                                                          •
                     The samples distribution uses the standard directory structure for Perl modules used by the          •
                     Comprehensive Perl Archive Network (www.cpan.org). There is an examples directory for the            •
                     sample Perl scripts and a lib directory for supporting files. After downloading the samples,          •
                     issue the following commands:                                                                        •
                                                                                                                          •
                         $   perl Makefile.PL                                                                              •
                         $   make
                                                                                                                          •
                         $   make test
                         $   make install
                                                                                                                          •
                                                                                                                          •
                     Juniper Networks has tested the scripts on platforms running FreeBSD 3.4 and FreeBSD 4.2,            •
                     but they are provided as unsupported examples only.                                                  •
                                                                                                                          •
                                                                                                                          •
                                                                                                                          •
  Supporting Files                                                                                                        •
                                                                                                                          •
                     The supporting Perl methods files in the lib directory include the following:
                                                                                                                          •
                                                                                                                          •
                         Access.pm—Implements a physical connection between a client application and a
                                                                                                                          •
                         JUNOScript server, using ssh or telnet.
                                                                                                                          •
                                                                                                                          •
                         Device.pm—Implements an object that client applications can use to represent and
                                                                                                                          •
                         communicate with a JUNOScript server.
                                                                                                                          •
                                                                                                                          •
                         Methods.pm—Implements the set of JUNOScript functions that client applications can
                                                                                                                          •
                         call on a JUNOScript server.
                                                                                                                          •
                                                                                                                          •
                         Trace.pm—Implements a utility library that client applications can use to control tracing
                                                                                                                          •
                         within a JUNOScript session.
                                                                                                                          •
                                                                                                                          •
                         Transform.pm—Implements a simple routine for translating the set of tags in a
                                                                                                                          •
                         JUNOScript server’s response into a Perl hash structure.
                                                                                                                          •
                                                                                                                          •
                                                                                                                          •
                                                                                                                          •
                                                                                                                          •
                                                                                                                          •
                                                                                                                          •

                                                                                                    Sample Perl Scripts   51
Sample P erl Scripts




•                                The supporting files in the examples directory include the following nonexecutable files:
•
•                                    base-jsrc.pl—Supports the change-tz.pl, hw-env.pl, rollback.pl, and test.pl scripts.
•
•                                    base-telnet.pl—Supports the standalone.pl script.
•
•                                    devices.xml—Lists the routers to which to direct requests, and the login names (user
•                                    IDs) to use when issuing the requests. Supports the change-tz.pl, hw-env.pl, rollback.pl,
•                                    and test.pl scripts.
•
•
•     Sample Perl Scripts
•
•                                The following scripts in the examples directory use the supporting functions defined in the
•                                Perl library modules and other files, which are listed in “Supporting Files” on page 51.
•
•                                    change-tz.pl—Illustrates how to change the time zone setting on the router.
•
•                                    get_config.pl—Illustrates how to perform the following functions:
•
•                                         Accept input that specifies the access method and the names of any number of
•                                         routers
•
•                                         Connect to the JUNOScript server on each specified router using the Device.pm
•                                         module
•
•                                         Request the current candidate configuration and record it in a file
•
•                                    get_config_ospf.pl—Illustrates how to perform the following functions:
•
•                                         Accept input that specifies the access method and router
•
•                                         Connect to the JUNOScript server by using the Device.pm module
•
•                                         Request the [edit protocols ospf] level of the current candidate configuration and
•                                         feed the result to a parser that implements the XML Document Object Module
•                                         (DOM)
•
•                                    hw-env.pl—Illustrates how to generate a simple hardware inventory.
•
•                                    hw_inventory.pl—Illustrates how to perform the following functions:
•
•                                         Accept input that specifies the access method and the names of any number of
•                                         routers
•
•                                         Connect to the JUNOScript server on each specified router using the Device.pm
•                                         module
•
•                                         Send a JUNOScript request tag, in this case <get-chassis-inventory>, which
•                                         corresponds to the show chassis hardware command
•
•                                         Parse each router’s response and generate formatted ASCII output that duplicates
•                                         the standard JUNOS CLI output, by invoking the XML DOM methods in the
•                                         crackers.pl script
•
•
•
•

52    JUNOScript 4.4 API Guide
                                                                                                  Web-enabled Scripts




                      re_info.pl—Illustrates how to perform the following functions:                                    •
                                                                                                                        •
                          Accept input that specifies the access method and router                                       •
                                                                                                                        •
                          Connect to the JUNOScript server by using the Device.pm module                                •
                                                                                                                        •
                          Send a JUNOScript request tag, in this case <get-route-engine-info>, which                    •
                          corresponds to the show chassis routing-engine command                                        •
                                                                                                                        •
                          Use the Transform.pm module to transform the router’s output into a set of                    •
                          key-value pairs in a Perl hash structure, and output the pairs                                •
                                                                                                                        •
                      re_info_dom.pl—Like the re_info.pl script, accepts input that specifies the access                 •
                      method and router, connects to the specified JUNOScript server, and sends the                      •
                      <get-route-engine-info> request tag. It then illustrates how to perform the following             •
                      functions:                                                                                        •
                                                                                                                        •
                          Use the Document Object Model (DOM) to parse the router’s response, extracting                •
                          the values from the tags                                                                      •
                                                                                                                        •
                          Generate formatted ASCII output that duplicates the standard JUNOS CLI output                 •
                                                                                                                        •
                      rollback.pl—Illustrates how to roll back to a previous configuration on the router.                •
                                                                                                                        •
                      simple.pl—Illustrates how to perform the following functions:                                     •
                                                                                                                        •
                          Accept input that specifies the access method, router, and CLI command                         •
                                                                                                                        •
                          Connect to the JUNOScript server by using the Device.pm module                                •
                                                                                                                        •
                          Send the specified command to the JUNOScript server by using the JUNOScript                    •
                          <command> tag                                                                                 •
                                                                                                                        •
                          Output the JUNOScript server’s response as an XML document (unanalyzed stream                 •
                          of tags)                                                                                      •
                                                                                                                        •
                      test.pl—Verifies correct functioning of the Perl methods files.                                     •
                                                                                                                        •
                 The cli-rollback.pl script performs the same function as rollback.pl, but accepts the router           •
                 name and other information from user input rather than extracting them from the                        •
                 devices.xml file.                                                                                       •
                                                                                                                        •
                 The standalone_telnet.pl script illustrates how to contact a JUNOScript server and parse its           •
                 output without using the methods defined in the supporting Perl modules. It invokes                     •
                 supporting functions in the nonexecutable base-telnet.pl script.                                       •
                                                                                                                        •
                                                                                                                        •
Web-enabled Scripts                                                                                                     •
                                                                                                                        •
                 The web subdirectory of the examples directory houses files that perform the same functions             •
                 as scripts in the examples directory but provide a Web-browser interface rather than a                 •
                 command-line interface.                                                                                •
                                                                                                                        •
                 The files with the .cgi extension perform the same function as the files in the example                  •
                 directory that have the .pl extension. They are simple.cgi, re_info.cgi, re_info_dom.cgi, and          •
                 hw_inventory.cgi. In each .cgi file, make the appropriate alteration for your installation in the       •
                 line that begins with the string #!.                                                                   •
                                                                                                                        •

                                                                                                  Sample Perl Scripts   53
Web-enabled Scripts




•                               The files with the .htm extension are HTML templates that control the appearance of the
•                               graphical user interface for each script. They are simple_template.htm,
•                               re_info_template.htm, re_info_dom_template.htm, and hw_inventory_template.htm.
•
•                               The file jcgi_base.pl is a supporting file.
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

54   JUNOScript 4.4 API Guide