STAF小试牛刀

1.问题描述: 利用STAF框架提供的API,编写Java Code,来取出远程linux系统里某些文件的信息(例如进度信息,端口号等等) 2.模拟环境: 1) 版本STAF3.3.1,Windows XP + RHEL5 2)联通测试 staf IP process start command ls 出现如下错误 Returned: RC=25, STAFResult=Trust level 5 required for the PROCESS service’s START request. Requester has trust level 3 on machine 解决办法如下 TRUST DEFAULT LEVEL 5 or TRUST LEVEL 5 MACHINE *.company.com or(不太管用 =。=。。) STAF配置文件是staf.cfg,window下默认的路径为:c:\staf\bin\. 安装完staf后,为了实现多台机器通过staf统一测试和管理,需要修改staf的配置文件, 以2台机器为例,进行配置:host1:10.62.162.55 host2:10.62.162.65 2台机器上均已经安装了Staf,且运行正常 通过修改staf安装目录(默认c:\staf\bin)下的staf.cfg这个配置文件实现 首先把每台机器的staf.cfg中增加另一台机器的信任级别: 如: trust machine tcp://10.62.162.65 level 5 保存后,重新启动staf. 确认运行正常 在command中运行:staf local trust list,查看信任的机器,新增加的机器名显示在列表中 3. Java 源码

public class Test
{
public static void main(String argv[])
{
try
{
// Create a STAFHandle
 
STAFHandle handle = new STAFHandle("MyApplication");
System.out.println("My handle is: " + handle.getHandle());
 
try
{
String machine = "192.168.235.128";
String service = "PROCESS";
//String command = "more /etc/services | grep \"vnc\" | awk -F\" \" '{print $2}'";
String command = "more /etc/services | grep \"vnc\" |awk '^{print $2}'";
String request = "START SHELL COMMAND " + STAFUtil.wrapData(command) +
" RETURNSTDOUT STDERRTOSTDOUT WAIT";
 
STAFResult result = handle.submit2(machine, service, request);
 
if (result.rc == STAFResult.Ok)
{
STAFMarshallingContext mc =
STAFMarshallingContext.unmarshall(result.result);
 
Map processCompletionMap = (Map)mc.getRootObject();
 
String processRC = (String)processCompletionMap.get("rc");
 
if (!processRC.equals("0"))
{
System.out.println(
"ERROR:  Process RC is " + processRC);
System.exit(1);
}
 
List returnedFileList = (List)processCompletionMap.get(
"fileList");
Map stdoutMap = (Map)returnedFileList.get(0);
String stdoutRC = (String)stdoutMap.get("rc");
 
if (!stdoutRC.equals("0"))
{
System.out.println(
"ERROR retrieving process Stdout data. RC=" + stdoutRC);
System.exit(1);
}
 
// Get the data from the STDOUT file created by the process
// and print it
 
String stdoutData = (String)stdoutMap.get("data");
 
System.out.println("\nProcess Stdout Contains:\n");
System.out.println(stdoutData);
}
else
{
System.out.println("Error submitting FS " + request +
"\nRC: " + result.rc + "  Result: " + result.result);
}
 
}
finally
{
handle.unRegister();
}
}
catch (STAFException e)
{
System.out.println(
"Error (un)registering with STAF, RC:" + e.rc);
System.exit(1);
}
 
}    // End of main()
 
}  // End of STAFTest

打印出: My handle is: 37 Process Stdout Contains: 2654/tcp 2654/udp

Leave a Comment.