有没有办法编写一个脚本(python或java),它可以连接到IBMi来运行命令并获取输出,或者可以是Linux中的shell脚本(.sh)。例如,为了查看对象的授权值,在绿屏中,我将运行 WRKOBJ [OBJNAME] 和 5 以显示每个库的信息。
我在这里的挑战是我需要获取几百个对象的值,所以我在想是否有一个脚本可以自动化该过程并将值输出到文件。
非常感谢您对任何建议和参考的帮助。谢谢。
sql 提供了一些视图和表函数。这称为 IBM i 服务。 我认为您可以从他们那里获得您想要的大部分信息 查看文档
你能在IBM i上运行PHP吗? 下面是一个执行以下操作的 PHP 脚本:
- 从
querystring
读取 3 个参数。objName
、objLib
、objType
- 调用
qcmdexc
以运行DSPOBJAUT
命令,输出将转到库QTEMP
中的OUTFILE
。 - 使用 SQL
select
语句读取DSPOBJAUT
输出文件的内容 - 运行
json_encode
以将输出文件内容作为json
流回显到 Web 调用方。
<?php
// set out document type to text/javascript instead of text/html
header("Content-type: text/javascript; charset:utf-8;");
$objName = isset($_GET["objName"]) ? $_GET["objName"]: '' ;
$objLib = isset($_GET["objLib"]) ? $_GET["objLib"]: '' ;
$objType = isset($_GET["objType"]) ? $_GET["objType"]: '' ;
// connect to ibm i
$libl = 'qgpl qtemp' ;
$options = array('i5_naming' => DB2_I5_NAMING_ON);
$options['i5_libl'] = $libl ; // library names separated by space.
$conn = db2_connect("*LOCAL","","", $options);
if (!$conn)
{
echo db2_conn_errormsg() ;
}
// build and run dspobjaut command. Will output object authorities to file in qtemp.
$cmds = 'dspobjaut obj(' . $objLib . '/' . $objName . ') objtype(' . $objType . ') output(*outfile) outfile(qtemp/objaut)';
$sql = "call qcmdexc('" . $cmds . "', " . strlen($cmds) . ")" ;
$stmt = db2_prepare( $conn, $sql ) ;
$result = db2_execute( $stmt ) ;
// read contents of dspobjaut outfile. Read into array where each item
// contains object. Each property of object is column from the dspobjaut outfile table.
$sql = 'select a.* from qtemp/objaut a ' ;
$stmt = db2_prepare( $conn, $sql ) ;
$result = db2_execute( $stmt ) ;
$ar1 = db2Stmt_ToManyRowArray($stmt) ;
// return dspobjaut output as json data stream.
$encode_txt = json_encode( $ar1, JSON_UNESCAPED_UNICODE ) ;
echo $encode_txt ;
// --------------------- db2Stmt_GetColNames ----------------
// build and return array of column names from a db2_execute
// executed $stmt.
function db2Stmt_GetColNames( $stmt )
{
$colCx = db2_num_fields($stmt);
$colNames = array( ) ;
for( $ix=0; $ix < $colCx; $ix++ )
{
array_push( $colNames, db2_field_name( $stmt, $ix )) ;
}
return $colNames ;
}
// ------------------- db2Stmt_ToManyRowArray ------------
function db2Stmt_ToManyRowArray( $stmt )
{
$colNames = db2Stmt_GetColNames( $stmt ) ;
$manyArr = array( ) ;
while( $row = db2_fetch_array( $stmt ))
{
// build row array consisting of column name/vlu pairs for
// each column of the result set.
$rowArr = array( ) ;
for( $ix = 0 ; $ix < count($colNames) ; $ix++ )
{
$rowArr[ $colNames[$ix] ] = rtrim($row[$ix]) ;
}
// push the $rowArr onto the array of many rows.
$manyArr[] = $rowArr ;
}
return $manyArr ;
}
?>