Display the IP address of a host using a RPGLE program

The following program can display the IP address of local or remote hosts as well as web urls.

     h dftactgrp(*no) actgrp(*new)

     d GETIPADDR       pr                  ExtPgm('GETIPADDR')
     d                               32a

     d GETIPADDR       pi
     d   host                        32a

     d inet_addr       pr            10U 0 ExtProc('inet_addr')
     d  address_str                    *   value options(*string)

     d INADDR_NONE     c                   CONST(4294967295)

     d inet_ntoa       pr              *   ExtProc('inet_ntoa')
     d  internet_addr                10u 0 value

     d p_hostent       s               *
     d hostent         ds                  Based(p_hostent)
     d   h_name                        *
     d   h_aliases                     *
     d   h_addrtype                  10i 0
     d   h_length                    10i 0
     d   h_addr_list                   *
     d p_h_addr        s               *   Based(h_addr_list)
     d h_addr          s             10u 0 Based(p_h_addr)

     d gethostbyname   pr              *   extproc('gethostbyname')
     d   host_name                     *   value options(*string)

     d IPaddress       s             10u 0
     d Message         s             50a

      /free

         IPaddress = inet_addr(%trim(host));

         If IPaddress = INADDR_NONE;
            p_hostent = gethostbyname(%trim(host));
            If  p_hostent <> *NULL;
                IPaddress = h_addr;
            EndIf;
         EndIf;

         If IPaddress = INADDR_NONE;
            Message = 'Host not found|';
         Else;
            Message = 'IP Address= ' + %str(inet_ntoa(IPaddress));
         EndIf;

         dsply Message;

         *inlr = *on;
      /end-free

Download Source

Posted in RPG, tips.