dns commands


DNS Commands

The program below (which is the DNS.CBL sample program) demonstrates how to use the GETHOSTBYNAME command. You can run this program from your web browser by typing in the URL http://127.0.0.1/cgi-bin/cobolscript.exe?dns.cbl if you are running CobolScript and a web server on your local machine.

Both GETHOSTNAME and GETHOSTBYNAME require two special group level data items – TCPIP-HOSTENT and TCPIP-RETURN-CODES. These data structures are placeholders for return values that are populated when these commands are executed. The structures must be in your program in order for it to run properly when you use these commands.

GETHOSTNAME gets the TCP/IP hostname from your local machine and place the name in a CobolScript variable. The GETHOSTBYNAME is a much more advanced command. It contacts your DNS (Domain Name Server) and retrieves detailed information about a specified host name. It retrieves information such as aliases and host addresses associated with a particular domain name. Try running this example with some domain names like lycos.com or yahoo.com.

Here are the variable definitions for DNS.CBL. Note the two standardized TCP/IP structures that we mentioned earlier. These would normally just be placed in a copybook by themselves, such as tcpip.cpy, but we include them here to show their detail:

**************************************

* TCP/IP *

* DATA STRUCTURES *

**************************************

* GETHOSTBYNAME REQUIRES THE DATA *

* STRUCTURE BELOW. DO NOT CHANGE IT.*

**************************************

01 TCPIP-HOSTENT.

05 TCPIP-HOSTENT-HOSTNAME PIC X(255).

05 TCPIP-HOSTENT-NUM-ALIASES PIC X.

05 TCPIP-HOSTENT-ALIASES OCCURS 8 TIMES.

10 TCPIP-HOSTENT-ALIAS PIC X(255).

05 TCPIP-HOSTENT-ADDRESS-TYPE PIC 9(7).

05 TCPIP-HOSTENT-ADDRESS-LENGTH PIC 9(7).

05 TCPIP-HOSTENT-NUM-ADDRESSES PIC X.

05 TCPIP-HOSTENT-ADDRESSES OCCURS 8 TIMES.

10 TCPIP-HOSTENT-ADDRESS PIC X(255).

**************************************

* TCP/IP RETURN CODES DATA STRUCTURE *

* DO NOT CHANGE. *

**************************************

01 TCPIP-RETURN-CODES.

05 TCPIP-RETURN-CODE PIC 9(7).

05 TCPIP-RETURN-MESSAGE PIC X(255).

* Program-specific variables

**************************************

1 content_length PIC 9(05).

1 web_header_html.

5 `Content-type: text/html`.

5 ` `.

5 `<HTML><BODY>`.

5 `<BR>`.

5 `<B>Sample CobolScript DNS Application</B>`.

5 `<BR><BR>`.

5 `Enter a Fully Qualified Domain Name or an IP address and then`

5 ` click on the Resolve button.`.

5 `<FORM ACTION="/cgi-bin/cobolscript.exe?dns.cbl" METHOD="POST">`.

5 `<INPUT TYPE="TEXT" NAME="host_name" SIZE=60 VALUE="`.

5 host_name PIC X(80) VALUE `www.cornell.edu`.

5 `">`.

5 `<INPUT TYPE="SUBMIT" VALUE="Resolve">`.

5 `</FORM>`.

5 `<HR>`.

1 web_footer_html.

5 `</BODY></HTML>`.

Here’s our main paragraph of code for DNS.CBL. Since we’re running this program from a browser, we first use the GETENV statement to determine whether we have input or not (see Chapter 5) and the output that we display is HTML:

MAIN.

GETENV USING `CONTENT_LENGTH` content_length.

IF content_length > 0

ACCEPT DATA FROM WEBPAGE

END-IF.

IF host_name = SPACES

MOVE `www.cornell.edu` TO host_name

END-IF.

* Populate TCP/IP structure that is defined in included copybook.

GETHOSTBYNAME USING host_name.

DISPLAYLF web_header_html.

PERFORM DISPLAY-TCPIP-INFO.

DISPLAYLF web_footer_html.

GOBACK.

The code module below displays each of the TCP/IP variables that are populated by the call to GETHOSTBYNAME, in an HTML table format. We’re excluding most of this module’s code from here because of its repetitive nature, but the entire code is in the DNS.CBL sample program:

DISPLAY-TCPIP-INFO.

1 counter PIC Z9.

DISPLAY `<TABLE BORDER=1 BGCOLOR="CCCCCC">`.

DISPLAY `<TR BGCOLOR="lightgreen">`.

DISPLAY `<TD><B>host_name:</B></TD>`.

DISPLAY `<TD><B>` & host_name & `</B></TD>`.

DISPLAY `</TR>`.

.

.

.

DISPLAY `</TABLE>`.