how to get ip address from hostname in CL (IBM i)

Today, I was preparing another program for automation. I need a function to retrieve an IP address of a remote system from the DNS having the system hostname. In the interactive session, you’d simple use ping or nslookup commands to resolve the hostname. But none of these commands can store the result into a variable. I did a short research, and there is very cool command Start DIG Query (DIG). This command can be used in a program, and it’s very easy get an IP address. Here is the example of a simple CL:

DIG        HOSTNAME(‘hostname1’) +
DMNSCHLIST(‘domain.com’) +
IPVSN(*IPV4ONLY) PROTOCOL(*TCP) +
SHORT(*YES) PRTCLASS(*NO) PRTTTL(*NO) +
PRTALL(*NO) CMD(*NO) COMMENTS(*NO) +
STATS(*NO) QUESTION(*NO) ANSWER(*YES) +
AUTHORITY(*NO) ADDITIONAL(*NO) +
TOSTMF(‘/tmp/ipaddress.txt’)

CRTPF      FILE(QTEMP/IPADD) RCDLEN(15)

QSYS/CPYFRMSTMF FROMSTMF(‘/tmp/ipaddress.txt’) +
TOMBR(‘/QSYS.LIB/QTEMP.LIB/ipadd.FILE/ipadd+
.MBR’) MBROPT(*REPLACE)

RMVLNK     OBJLNK(‘/tmp/ipaddress.txt’)

One thought on “how to get ip address from hostname in CL (IBM i)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s