如何由已知的 IP address 取得該 IP 的 Host Name ?
可藉由 C Library 的 inet_addr 及gethostbyaddr 取得。
File : QRPGLESRC
Member: GETHOSTNAM
Type : RPGLE
Usage : CRTBNDRPG GETHOSTNAM
hdebug dftactgrp( *no ) bnddir( 'QC2LE' )
Dinet_addr PR 10U 0 ExtProc('inet_addr')
D IPAddr * Value
DGetHostByAddr PR * ExtProc('gethostbyaddr')
D HostEnt * Value
D AddrLen 5I 0 Value
D AddrType 5I 0 Value
DSysInfDS SDS
D CurDevice 244 253
DRcvVar DS
D Protocol 859 859
D IPAddress 878 892
DRcvLen S 9B 0
DFormat S 8
DDevice S 10
DIPAddr S *
DHostEnt S *
DHostAddr S *
DAddrLen S 5I 0
DAddrType S 5I 0
DHostEntS DS Based(HostEntP)
D HostNPtr *
DHostName S 64 Based(HostNameP)
DHostAddrS DS Based(HostAddrP)
D IP32Addr 10U 0
DAF_INET S 5I 0 Inz(2)
DSpaces S 64 Inz(*Blanks)
DAPIError DS
D NbrBytes 1 4B 0 Inz(15)
D CPFID 9 15
*****************************************************************
C *Entry PList
C Parm P#IPAddress 15
C Parm P#IPName 64
* Get IP-name
c Eval IPAddress = P#IPAddress
C Eval HostEntP = %Addr(HostEnt)
C Eval HostAddrP = %Addr(HostAddr)
C Eval IP32Addr = inet_addr(%Addr(IPAddress))
C Eval AddrLen = %Size(HostAddrS)
C Eval AddrType = AF_INET
C Eval HostEntP = GetHostByAddr(HostAddrP:
C AddrLen:
C AddrType)
C HostEntP IfEq *NULL
C Eval P#IPName = Spaces
C Else
C Eval HostNameP = HostNPtr
C Eval P#IPName = HostName
C X'00':X'40' XLate P#IPName P#IPName
C EndIf
* Return parameters
C Eval P#IPAddress = IPAddress
C SetOn Lr
C Return
File : QCLSRC
Member: GETHOSTNMC
Type : CLP
Usage : CRTCLPGM GETHOSTNMC
CALL PGM(GETHOSTNMC) PARM('xxx.xxx.xxx.xxx')
xxx.xxx.xxx.xxx -> IP address
PGM PARM(&IP)
DCL VAR(&IP) TYPE(*CHAR) LEN(15)
DCL VAR(&HOST) TYPE(*CHAR) LEN(64)
CALL PGM(GETHOSTNAM) PARM(&IP &HOST)
SNDPGMMSG MSG('The host name for TCP/IP address ' || +
&IP *BCAT 'is ' || &HOST)
ENDPGM
A blog about IBM i (AS/400), MQ and other things developers or Admins need to know.
星期三, 11月 01, 2023
2002-06-04 如何由已知的 IP address 取得該 IP 的 Host Name ?
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言