Retrieve the IP address of the local iSeries using a CL program

The following program uses the PING command to generate a message with the IP address. The IP address is then extracted from the message text and displayed on screen.

PGM
DCL VAR(&SYSNAME) TYPE(*CHAR) LEN(8)
DCL VAR(&MSGDTA) TYPE(*CHAR) LEN(100)
DCL VAR(&IPADDR) TYPE(*CHAR) LEN(16)

DCL VAR(&COMP) TYPE(*CHAR) LEN(12) +
VALUE('0123456789. ')
DCL VAR(&SPOS) TYPE(*UINT) LEN(2)
DCL VAR(&EPOS) TYPE(*UINT) LEN(2)
DCL VAR(&LEN) TYPE(*UINT) LEN(2)

/* Get the system name */
RTVNETA SYSNAME(&SYSNAME)

/* PING with the system name to get message with IP address */
PING RMTSYS(&SYSNAME)
RCVMSG MSGTYPE(*INFO) MSG(&MSGDTA)

/* Extracts IP address from message... */
/* ...find trailing full stop in message */
CHGVAR VAR(&EPOS) VALUE(%CHECKR('. ' &MSGDTA))
/* Get the system name */
RTVNETA SYSNAME(&SYSNAME)

/* PING with the system name to get message with IP address */
PING RMTSYS(&SYSNAME)
RCVMSG MSGTYPE(*INFO) MSG(&MSGDTA)

/* Extracts IP address from message... */
/* ...find trailing full stop in message */
CHGVAR VAR(&EPOS) VALUE(%CHECKR('. ' &MSGDTA))

/* ...find last piece of message text before ip address */
CHGVAR VAR(&SPOS) VALUE(%CHECKR(&COMP &MSGDTA))

/* ...calc start of ip address */
CHGVAR VAR(&SPOS) VALUE(&SPOS + 2)

/* ...IP address */
CHGVAR VAR(&LEN) VALUE(&EPOS - &SPOS + 1)
CHGVAR VAR(&IPADDR) VALUE(%SST(&MSGDTA &SPOS &LEN))

SNDUSRMSG MSG(&IPADDR)

ENDPGM

Download source
Posted in tips.