How do I retrieve the IP address of a machine via a CL program?
I checked RTVJOBA but it's not there, so I checked RTVNETA and it's not there either.
I checked RTVJOBA but it's not there, so I checked RTVNETA and it's not there either.
*==========================================================
* To Compile:
*
* CRTBNDRPG PGM(XXX/RTVIPADR1)
*
* Function: Retrieve IP address of PC
*
*==========================================================
H DFTACTGRP(*NO) BNDDIR('QC2LE')
D RtvIpAdr PR 20a
D Device 10a CONST
*
D I_Net_Adr S 16a
*
D SDS
D Device 244 253
*---------------------------------------------------------
* Retrieve IP Address
C Eval I_Net_Adr = RtvIpAdr(Device)
C Eval I_Net_Adr = %trim(I_Net_Adr)
C I_Net_Adr Dsply
C Eval *Inlr = *On
*---------------------------------------------------------
* RtvIpAdr - Subprocedure To Retrieve PC's IP Address
*---------------------------------------------------------
P RtvIpAdr B Export
D RtvIpAdr PI 20A
D Inp_Device 10A Const
D Apierr DS
D Bytprv 1 4B 0 Inz(216)
D Bytavl 5 8B 0 Inz
D Errid 9 15A Inz
D Rsvd 16 16A Inz
D Errdta 17 216A Inz
D Net_Address S 20A INZ
D Format S 8A Inz('DEVD0600')
D Rcvar S 5000A Inz
D Varlen S 4B 0 Inz(5000)
C Eval Device = Inp_Device
C Call 'QDCRDEVD'
C Parm Rcvar
C Parm Varlen
C Parm Format
C Parm Device
C Parm Apierr
C If BytAvl = 0
C Eval Net_Address = %Subst(Rcvar:877:16)
C Endif
C Return Net_Address
P RtvIpAdr E
pgm
dcl &sysname *char 8
RTVNETA SYSNAME(&SYSNAME)
endpgm
SELECT * FROM QATOCHOST ORDER BY HOSTNME1, INTERNET
The calling program
** Program . . : CBX109T
** Description : Get file field value by key - Test
**
** Program directions
** ------------------
**
** This test program retrieves field values from the TCP/IP host table
** which is stored in a physical file named QATOCHOST in QUSRSYS.
**
** Go CFGTCP option 10 allow you to examine the current entries in this
** table. If you want, or if required, please change the key value from
** '127.0.0.1' to another existing entry in the table. Please also
** check that you have sufficient authority to the table prior to
** running this test program.
**
** Another option would be to replace the file name, field name and key
** value specified in the example below to values of your own choice.
**
** This test program presents the retrieved field values using the DSPLY
** facility. You could also simply start a debug session against this
** program and step through the code lines, to watch the process as it
** unfolds.
**
** When the debug session is positioned on a GetFldVal() procedure
** statement you can use F22 to step into the subprocedure and examine
** the statements executed there. The F10 step instruction also applies
** while in the subprocedure.
**
**
** Compile options required:
** CrtRpgMod CBX109T +
** DbgView( *LIST )
**
** CrtPgm CBX109T +
** Module( CBX109T ) +
** BndSrvPgm( CBX109S ) +
** ActGrp( QILE )
**
**
**-- Header specifications: --------------------------------------------**
H Option( *SrcStmt )
**-- Global definitions: -----------------------------------------------**
D FldVal s 1024a Varying
D DspVal s 42a
**
D SngRqs c 0
D InzRqs c 1
D RunRqs c 2
D TrmRqs c 3
**-- Get field value: --------------------------------------------------**
D GetFldVal Pr 1024a Varying
D PxRqsTyp 10i 0 Const
D PxFilNam 10a Const Options( *NoPass )
D PxFldNam 10a Const Options( *NoPass )
D PxKey 256a Const Varying Options( *NoPass )
**
**-- Mainline: ---------------------------------------------------------**
**
C Eval FldVal = GetFldVal( SngRqs
C : 'QATOCHOST'
C : 'HOSTNME1'
C : '127.0.0.1'
C )
**
C Eval DspVal = FldVal
C 'HOSTNME1 =' Dsply DspVal
**
C Eval FldVal = GetFldVal( InzRqs
C : 'QATOCHOST'
C )
**
C Eval FldVal = GetFldVal( RunRqs
C : 'QATOCHOST'
C : 'HOSTNME1'
C : '127.0.0.1'
C )
**
C Eval DspVal = FldVal
C 'HOSTNME1 =' Dsply DspVal
**
C Eval FldVal = GetFldVal( RunRqs
C : 'QATOCHOST'
C : 'IPINTGER'
C : '127.0.0.1'
C )
**
C Eval DspVal = FldVal
C 'IPINTGER =' Dsply DspVal
**
C Eval FldVal = GetFldVal( RunRqs
C : 'QATOCHOST'
C : 'TXTDESC'
C : '127.0.0.1'
C )
**
C Eval DspVal = FldVal
C 'TXTDESC =' Dsply DspVal
**
C Eval FldVal = GetFldVal( TrmRqs )
**
C Return
**
Thanks to Carsten Flensburg
//
// Program Info
//
H BNDDIR('QC2LE') DFTACTGRP(*NO) ACTGRP('QILE')
D gethostbyname pr * ExtProc('gethostbyname')
D host_name * value
D inet_ntoa pr * ExtProc('inet_ntoa')
D addr 10u 0 value
* host
D host_ptr s * Inz(*null)
D host_ds ds Align Qualified Based(host_ptr)
D name_p *
D aliases_pp *
D addr_type 10i 0
D addr_len 10i 0
D addr_list_pp *
D addr_list_p s * Based(host_ds.addr_list_pp)
D addr_arr s 10u 0 Dim(1) Based(addr_list_p)
D inetaddr s 10u 0 Inz(0)
D c_host s 256a
D c_ipaddr_p s *
D c_ipaddr s 17a based(c_ipaddr_p)
D ipaddr s 16a varying
/free
ipaddr='';
%str(%addr(c_host) : %size(c_host)) = 'systemname.domain.com';
host_ptr = gethostbyname(%addr(c_host));
if host_ptr <> *null and host_ds.name_p <> *null;
inetaddr = addr_arr(1);
c_ipaddr_p = inet_ntoa(inetaddr);
ipaddr = %str(c_ipaddr_p);
endif;
*INLR = *on;
return;
/End-Free

C Eval Net_Address= %Subst(rcvar:877:16)***
C Eval Net_Address = %Subst(Rcvar:958:15)
Eval Net_Address = %Subst(Rcvar:878:15)
// QDCD060000 defines the fixed portion of the DEVD0600 format.
D/COPY QSYSINC/QRPGLESRC,QDCRDEVD
GetIPAddr(QDCD060000: 972: 'DEVD0600': DevName: stdError);
if stdError.QUSEC.QUSBAVL > 0;
if stdError.QUSEC.QUSEI = 'CPF2702';
// unknown device
eval RtnIndPr = 'N1';
else;
// other errors
eval RtnIndPr = 'N2';
endif;
else;
eval RtnIndPr = 'OK';
select;
// IP device address
when QDCNP = PROTO_IP;
IPAddrDev = QDCIPADF;
IPAddr400 = QDCSNIPA;
// SNA device address
// --- not catered for but fill anyway as we might
// --- need it later
when QDCNP = PROTO_IPX;
IPAddrDev = QDCIPADF;
IPAddr400 = QDCSNIPA;
other;
eval RtnIndPr = 'N3';
endsl;
endif;
*inlr = *ON;

Comment