星期四, 11月 09, 2023

2015-09-17 Retrieve AS400 Network Interfaces with java( List Network Interfaces (QtocLstNetIfc) API format NIFC0100)


Retrieve AS400 Network Interfaces with java( List Network Interfaces (QtocLstNetIfc) API format NIFC0100)
(RtvNetIfc.java)



File  : RtvNetIfc.java



/* ==================================================================*/
/*                                                                   */
/*  Program . . : RtvNetIfc.java                                     */
/*  Description : List Network Interfaces                            */
/*  Author  . . : Vengoal Chang                                      */
/*  Published . : AS400ePaper                                        */
/*  Date  . . . : September  17, 2015                                */
/*                                                                   */
/* ==================================================================*/

package com.free400.vengoal.as400api;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Bin4;
import com.ibm.as400.access.AS400Exception;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.ProgramParameter;
import com.ibm.as400.access.ServiceProgramCall;
import com.ibm.as400.access.SystemStatus;
import com.ibm.as400.access.UserSpace;

public class RtvNetIfc {

	public static void main(String[] args)  {
		 final AS400Bin4 intConverter_ = new AS400Bin4();		

		// Change following as400ip, as400user, as400password as your system and user profile setting
		AS400 as400 = new AS400("as400ip", "as400user", "as400password");
		try {
			// The first parm is 20 characters, 10 chars of user space followed by 10 chars of library. 
			// Create a converter for 20 chars. AS400Text char20 = new AS400Text(20, system); 
			// The second parm is the format name (8 chars). Create a converter. 
			AS400Text char20 = new AS400Text(20, as400);
			AS400Text char8 = new AS400Text(8, as400);			

			// Create program parameters
			ProgramParameter[] parms = new ProgramParameter[3];
			UserSpace usrSpc = new UserSpace(as400, "/QSYS.LIB/QTEMP.LIB/MYSPACE.USRSPC");
			usrSpc.setMustUseProgramCall(true);
			usrSpc.create(10240, // The initial size is 10KB
					true, // Replace if the user space already exists
					" ", // No extended attribute
					(byte) 0x00, // The initial value is a null
					"Created by a Java program", // The description of the user
													// space
					"*USE");

			// First parm is qualified user space CHAR(20)
			// CHAR(0-9) is the user space name
			// CHAR(10-19) is the library name
			String qSpace = "MYSPACE   QTEMP     ";

			// First parm is the library qualified UserSpace
			parms[0] = new ProgramParameter(char20.toBytes(qSpace));
			parms[0].setParameterType(ProgramParameter.PASS_BY_REFERENCE);
			// Second parm is the format
			parms[1] = new ProgramParameter(char8.toBytes("NIFC0100"));
			parms[1].setParameterType(ProgramParameter.PASS_BY_REFERENCE);

			// Last parm is the error code. We pass an array of 0x00s so
			// messages are returned.
			byte[] bytes = new byte[32];
			parms[2] = new ProgramParameter(bytes, 32);
			parms[2].setParameterType(ProgramParameter.PASS_BY_REFERENCE);
			System.out.println("Retrieving network interface information for system "
					+ new SystemStatus(as400).getSystemName() + " ...");
			ServiceProgramCall sPGMCall = new ServiceProgramCall(as400, "/QSYS.LIB/QTOCNETSTS.SRVPGM", "QtocLstNetIfc",
					ServiceProgramCall.NO_RETURN_VALUE, parms);
			if (sPGMCall.run() != true) {
				throw new AS400Exception(sPGMCall.getMessageList());
			} else {
				byte[] header = new byte[140];
				usrSpc.read(header, 0);
				int list_Offset = intConverter_.toInt(header, 124);
				int list_Size = intConverter_.toInt(header, 128);
				int entry_count = intConverter_.toInt(header, 132);
				int entry_size = intConverter_.toInt(header, 136);

				int strPos = list_Offset;
				System.out.println("IP Address     " + " " + "NetWork Address" + " " + "Line Desc " + " " + "Status");
				System.out.println("===============" + " " + "===============" + " " + "==========" + " " + "======");
				for (int i = 0; i < entry_count; i++) {
					String ipAdr = usrSpc.read(strPos, 15);
					String netAdr = usrSpc.read(strPos + 20, 15);
					String netWork = usrSpc.read(strPos + 40, 10);
					String lineDesc = usrSpc.read(strPos + 50, 10);
					String ifc = usrSpc.read(strPos + 60, 10);
					byte[] ifcStatusBytes = new byte[4];
					usrSpc.read(ifcStatusBytes, strPos + 72);
					int ifcStatus = intConverter_.toInt(ifcStatusBytes);
					System.out.println(ipAdr + " " + netAdr + " " + lineDesc + " " + ifcStatus);
					strPos += entry_size;
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


						
						


參照: List Network Interfaces (QtocLstNetIfc) API




沒有留言: