如何於 CLP 中以 Host name 取得主機的 IP 或以 IP 取得主機的 Host name?(Command GETHOSTGet Host by Name(gethostbyname) & Get Host by Address(gethostbyaddr))
File : QRPGLESRC
Member: GETHOST
Type : RPGLE
Usage : CRTBNDRPG PGM(GETHOST)
**
** To compile:
** CRTBNDRPG PGM(xxxx) SRCFILE(xxxx/xxxx) DFTACTGRP(*NO) +
** ACTGRP(*CALLER)
** (actually, activation group can be whatever you prefer)
**
H DEBUG OPTION(*SRCSTMT:*NODEBUGIO) DFTACTGRP(*NO) ACTGRP(*CALLER)
** -------------------------------------------------------------------
D* The "internet" address family.
** -------------------------------------------------------------------
D AF_INET C CONST(2)
** -------------------------------------------------------------------
D INet_Addr PR 10U 0 ExtProc('inet_addr')
D char_addr 16A
** -------------------------------------------------------------------
D inet_ntoa PR * ExtProc('inet_ntoa')
D ulong_addr 10U 0 VALUE
** -------------------------------------------------------------------
D* any address availabl
D INADDR_ANY C CONST(0)
D* broadcast
D INADDR_BRO C CONST(4294967295)
D* loopback/localhost
D INADDR_LOO C CONST(2130706433)
D* no address exists
D INADDR_NON C CONST(4294967295)
** -------------------------------------------------------------------
D GetHostNam PR * extProc('gethostbyname')
D HostName 256A
** -------------------------------------------------------------------
** gethostbyaddr()--Get Host Information for IP Address
** -------------------------------------------------------------------
D GetHostAdr PR * ExtProc('gethostbyaddr')
D IP_Address 10U 0
D Addr_Len 10I 0 VALUE
D Addr_Fam 10I 0 VALUE
** -------------------------------------------------------------------
** Host Database Entry (for DNS lookups, etc)
** -------------------------------------------------------------------
D p_hostent S *
D hostent DS Based(p_hostent)
D h_name *
D h_aliases *
D h_addrtype 5I 0
D h_length 5I 0
D h_addrlist *
D p_h_addr S * Based(h_addrlist)
D h_addr S 10U 0 Based(p_h_addr)
D*** internal "work" variables. (not part of /COPY file)
D wkInput S 256A
D wkIP S 10U 0
D wkLen S 10I 0
D p_Name S * INZ(*NULL)
D wkName S 256A BASED(p_name)
C****************************************************************
C* Parameters:
C*
C* RetType: May be *NAME or *ADDR. If *NAME is given,
C* we'll return a domain name. If *ADDR we'll return an
C* IP Address.
C*
C* Input: Host or IP address to lookup. IP addresses should
C* be given in x.x.x.x format.
C*
C* Output: Resulting IP address, host name or error code.
C* Error codes are: *TYPE = invalid "RetType" parameter.
C* *BLANK = Input cant be blank
C* *FAIL = Lookup failed for this host.
C****************************************************************
C *entry plist
c parm RetType 5
c parm Input 256
c parm Output 256
C* If we werent given enough parms, just end this program now...
C* (we'll seton LR, even) We can't return an error since we
c* don't have an output parm to return it in (ack!)
c if %parms < 3
c eval *inlr = *on
c return
c endif
C* Did we have a valid return type?
c if RetType <> '*NAME'
c and RetType <> '*ADDR'
c eval Output = '*TYPE'
c Return
c endif
C* Was some input given?
C if Input = *blanks
c eval Output = '*BLANK'
c Return
c endif
C* were we given an IP address or a name?
c eval wkInput = %trim(Input) + x'00'
c eval wkIP = inet_addr(wkInput)
C* An address was requested... and the input was already
C* an address... return the input directly.
c if RetType = '*ADDR'
c and wkIP <> INADDR_NON
c eval Output = %trim(Input)
c Return
c endif
C* Call the OS/400 resolver routines to get the information that
C* we require. (It will check the hosts table first, then try DNS)
c if wkIP = INADDR_NON
c eval p_hostent = gethostnam(wkInput)
c else
c eval p_hostent = gethostadr(wkIP:4:AF_INET)
c endif
c if p_hostent = *NULL
c eval Output = '*FAIL'
c return
c endif
C* if we're returning an address, we'll need to use inet_ntoa
C* to convert it back to dotted-decimal x.x.x.x format.
C*
c if RetType = '*ADDR'
c eval p_name = inet_ntoa(h_addr)
c if p_name = *NULL
c eval Output = '*FAIL'
c else
c x'00' scan wkName wkLen
c eval Output = %subst(wkName:1:wkLen-1)
c endif
c return
c endif
C* the hostent structure contains a pointer to the requested
C* domain name... we'll need to base a variable on that pointer,
C* and then convert it from the "C" format for strings to a
C* fixed-length RPG string
c if h_name = *NULL
c eval Output = '*FAIL'
c return
c endif
c eval p_name = h_name
c x'00' scan wkName wkLen
c eval Output = %subst(wkName:1:wkLen-1)
c return
File : QCMDSRC
Member: GETHOST
Type : CMD
Usage : CRTCMD CMD(GETHOST) PGM(GETHOST) ALLOW(*IPGM *BPGM)
/* COMMAND GETHOST */
/* TO COMPILE : */
/* CRTCMD CMD(XXX/GETHOST) PGM(XXX/GETHOST ) + */
/* SRCFILE(XXX/QCMDSRC) */
/*================================================================*/
/* USAGE SAMPLE in CLP: */
/* GETHOST RETTYPE(*ADDR) INPUT(&HOSTNAME) OUTPUT(&HOSTIP) */
/* GETHOST RETTYPE(*NAME) INPUT(&HOSTIP) OUTPUT(&HOSTNAME) */
/* */
/* OUTPUT Resulting IP address, host name or error code. */
/* Error codes are: *TYPE = invalid "RetType" parameter */
/* *BLANK = Input cant be blank */
/* *FAIL = Lookup failed for this host */
/*================================================================*/
GETHOST: CMD PROMPT('Get Host by Name & by Address')
PARM KWD(RETTYPE) TYPE(*CHAR) LEN(5) RSTD(*YES) +
VALUES(*ADDR *NAME) MIN(1) PROMPT('Return +
type')
PARM KWD(INPUT) TYPE(*CHAR) LEN(256) MIN(0) +
EXPR(*YES) PROMPT('Input Host name or +
address')
PARM KWD(OUTPUT) TYPE(*CHAR) LEN(256) +
RTNVAL(*YES) PROMPT('Output Host name or +
address')
File : QCLSRC
Member: GETHOSTC
Type : CLP
Usage : CRTCLPGM GETHOSTC
執行 CALL RTVSQLINFC 後,會產生 QPPGMDMP 報表,檢視 QPPGMDMP 報表,
PGM
DCL &INPUT *CHAR 256
DCL &IPOUTPUT *CHAR 256
DCL &NMOUTPUT *CHAR 256
/* 以主機名稱取 IP 位址 */
GETHOST RETTYPE(*ADDR) INPUT('tw.yahoo.com') +
OUTPUT(&IPOUTPUT)
/* 以 IP 位址取主機名稱 */
GETHOST RETTYPE(*NAME) INPUT(&IPOUTPUT) +
OUTPUT(&NMOUTPUT)
DMPCLPGM
ENDPGM
部分報表輸出範例:
Display Spooled File
File . . . . . : QPPGMDMP Page/Line 1/24
Control . . . . . Columns 1 - 78
Find . . . . . .
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+...
Variable Type Length Value
*...+....1....+....2....+
&IPOUTPUT *CHAR 256 '202.43.195.52 '
+26 ' '
+51 ' '
+76 ' '
+101 ' '
+126 ' '
+151 ' '
+176 ' '
+201 ' '
+226 ' '
+251 ' '
&NMOUTPUT *CHAR 256 'vip1.tw.tpe.yahoo.com '
+26 ' '
+51 ' '
A blog about IBM i (AS/400), MQ and other things developers or Admins need to know.
星期三, 11月 08, 2023
2008-07-01 如何於 CLP 中以 Host name 取得主機的 IP 或以 IP 取得主機的 Host name?(Command GETHOSTGet Host by Name(gethostbyname) & Get Host by Address(gethostbyaddr))
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言