標籤:
最近項目中遇到一個典型事件,當RHEL 的SELINUX設為DISABLE時
使用JAVA的Jsch 庫調用SSH命令時將隨機返回Null 字元串,我使用的版本是0.1.49,最新版本0.1.51未測試。
關於Jsch: http://www.jcraft.com/jsch/
為此,我特意寫了一個程式測試:
package com.ibm.leo;import com.ibm.gts.cms.common.guestssh.api.GuestSshServiceFactory;import com.ibm.gts.cms.common.guestssh.api.IGuestSshProperties;import com.ibm.gts.cms.common.guestssh.api.IGuestSshService;import com.ibm.gts.cms.common.guestssh.api.IScriptResponse;public class GuestSSH { /** * This code snippet will validate that the guestssh service remove execute will return null randomly if the selinux was disabled. * */public static void main(String[] args) {try{int sshRC=-1;if(args.length<3){System.out.println("Usage: java -jar testssh.jar <Host IP> <command> <count>");System.exit(1);}int count=Integer.parseInt(args[2]);if(count==0) count=1;int nullCount=0;System.out.println("start test...");// Run the command via SSHIGuestSshService sshService = GuestSshServiceFactory.GetService();IGuestSshProperties props = sshService.makeGuestSshProperties();props.setConnectTimeout(60000); // 60 seconds to establish connection with the guestprops.setCommandTimeout(60 * 60 * 1000); // 1 hour to wait for command to complete (after connection)//props.setScriptInputStream(null); // stdin may be null, which is OK and means no stdin datafor(int i=1;i<=count;i++){IScriptResponse response = sshService.invoke("root", args[0], 22, null, args[1], null, props);sshRC = response.getReturnCode();String[] stdoutLines = response.getStandardOutputLines();if(stdoutLines[0].trim().equals("")) nullCount++;System.out.println("Exceute count:"+i+" returnCode: "+sshRC +" return Lines:"+stdoutLines.length);for (String line : stdoutLines) {System.out.println("Command return: "+line);}}System.out.println("End test, the total execute count is "+count+", and first line null return count is: " + nullCount);}catch(Exception e){System.out.println(e.getMessage());}}}
測試結果如下:
D:\tmp>java -jar testssh.jar 192.168.1.244 hostname 5start test...Exceute count:1 returnCode: 0 return Lines:1Command return: GMTDevExceute count:2 returnCode: 0 return Lines:1Command return:Exceute count:3 returnCode: 0 return Lines:1Command return:Exceute count:4 returnCode: 0 return Lines:1Command return:Exceute count:5 returnCode: 0 return Lines:1Command return: GMTDevEnd test, the total execute count is 5, and first line null return count is: 3
從結果中可以看出,共取了5次主機名稱,只有兩得到,3次雖然命令成功執行,但返回空值,這種情況只有當SELINUX=disabled時出現,而Enforcing和permissive傳回值都正常。
或許是guestssh的一個BUG? 記錄一下備查。
SELINUX設為Disable 影響java SSH工具包Jsch 0.1.49.jar的一個案例