Program 5:

a) Write a Perl program to display various Server Information like Server Name, Server Software, Server protocol, CGI Revision etc.

b) Write a Perl program to accept UNIX command from a HTML form and to display the output of the command executed.


Tags used:

 <form> - The <form> tag is used to create a HTML form for user input.
 <input> - The <input> tag specifies an input field where the user can enter data. 
 <th> - The <th> tag defines a header cell in a HTML table.
 print<<here; (known as here-document) - used for interpolating large chunks of text and for displaying multiple lines easily. We use it for printing multiple lines on to the browser.
 use CGI - imports cgi module.
 Content-type:text/html - to pass output of script to the browser.

Code:

5a.html

 <html>
 <!-- path to perl file -->
    <form action="http://localhost/cgi-bin/5a.pl">
        <center>
        <input type=submit value=Show_Server_Info />
        </center>
    </form>
 </html>

5a.pl

 #!/usr/bin/perl
 #this is a here-document
 print<<here;
 Content-type:text/html\n\n
 <html>
<center>
    <table border=1>
    <tr>
        <th>ENV_VARIABLES</th><th>Value</th>
    </tr>
 here
 #end of here document
 #display values in a table
 foreach $i(sort keys %ENV)
 {
print "<tr><td>$i</td><td>$ENV{$i}</td></tr>";
 }
 print<<here
           </table>
      </center>
 </html>";

Output:

Steps for checking output-

  • Save the .html file in the folder /var/www/html
  • Save the .pl files in the folder /var/www/cgi-bin
  • Change the file permission of the perl file by running the command sudo chmod 777 5a.pl
  • Open a browser and in the address bar type localhost/html/5a.html
  • By clicking the Show_Server_Info button, the output will be displayed on the browser.

Screenshots:

input

output

Code:

5b.html

 <html>
    <!-- path to perl file -->
    <form action="http://localhost/cgi-bin/5b.pl">
        <!-- input command name -->
        Command:<input type=text name=com>
            <input type=submit value=submit />
    </form>
 </html>

5b.pl

 #!/usr/bin/perl
 use CGI':standard';
 #the following line is used for displaying the output of the script in the browser
 print "Content-type:text/html\n\n";
 #take the input command from the browser and store in the variable
 $c=param('com');
 #process the command
 system($c);
 exit(0);

Output:

Steps for checking output-

  • Save the .html file in the folder /var/www/html
  • Save the .pl files in the folder /var/www/cgi-bin
  • Change the file permission of the perl file by running the command sudo chmod 777 5b.pl
  • Open a browser and in the address bar type localhost/html/5b.html
  • Enter a UNIX command as input to view the output of the command on the browser.

Screenshots:

input_1

input_2

output