2013-09-22 68 views
0

我的新的問題是執行的bash shell腳本,Java的要求sudo的權限root權限bash腳本。我想要做的我使用命令行,這是與NS-的slapd的db2ldif命令導出完成DS-389數據庫轉換爲LDIF格式是什麼。這是我簡單的Java代碼,主要用於執行此操作:執行需要從Java版本(NetBeans)

ProcessBuilder p = new ProcessBuilder("/bin/bash", "example.sh"); 
final Process process = p.start(); 

其中example.sh位於項目目錄,並有一個與訪問它沒有問題。我還添加了腳本以執行chmod 777的權限。 Example.sh只有這個:

#!/bin/bash 
ns-slapd db2ldif -D /etc/dirsrv/slapd-localhost -n userRoot -s "ou=Group,dc=localdomain" -a /tmp/file.ldif 

我怎麼努力,到目前爲止是visudo命令,添加此行:

nobody ALL=(ALL) NOPASSWD: /usr/sbin/ns-slapd 
myUSER ALL=(ALL) NOPASSWD: /usr/sbin/ns-slapd 
root ALL=(ALL) NOPASSWD: /usr/sbin/ns-slapd 
bin ALL=(ALL) NOPASSWD: /usr/sbin/ns-slapd 
myUSER ALL = NOPASSWD: /usr/bin/java 
root ALL= NOPASSWD: /usr/bin/java 
nobody ALL= NOPASSWD: /usr/bin/java 
bin ALL= NOPASSWD: /usr/bin/java 

但沒有結果..而且是這種變化讓我執行例子.sh沒有問我密碼,但在命令行。當我從java中嘗試這樣做時,它不起作用,並且在/ tmp中沒有創建file.ldif。歡迎任何幫助。 感謝您的時間:)

回答

0

嘗試使用sudo -S -p

其他方式我用位於jsch-0.1.38.jarJSch類。

的想法是從控制檯sudo輸入重定向到Java代碼。

SudoExec類

public abstract class SudoExec { 

private String mHost; 
private static String passwd; 
private SSHObserverItf mObserver = null; 
protected boolean isForceStop = false; 
protected boolean isAsIs = false; 
protected Timer mTimer = null; 



//default constructor 
public SudoExec(String hostName,String userName,String password){ 
    setHost(userName+"@"+hostName); 
    setPassword(password); 
} 

public void init(int timeToWait) { 

    mTimer = new Timer(); 


    new Thread(){  
     public void run(){ 
      execCMD(); 
     }   
    }.start(); 

    mTimer.doWait(timeToWait); 

    isForceStop = true; 
} 


private void execCMD(){ 

    isForceStop = false;   

    try{ 
     JSch jsch=new JSch(); 

     String host=getHost(); 


     String user=host.substring(0, host.indexOf('@')); 
     host=host.substring(host.indexOf('@')+1); 

     Session session=jsch.getSession(user, host, 22); 



     // username and password will be given via UserInfo interface. 
     UserInfo ui=new MyUserInfo(); 
     session.setUserInfo(ui); 
     session.connect(); 

     String command=getCmd(); 

     Channel channel=session.openChannel("exec"); 

     ((ChannelExec)channel).setPty(true); 

     if(isAsIs == true){ 
      ((ChannelExec)channel).setCommand(command); 
      } 
     else{ 
      ((ChannelExec)channel).setCommand("sudo -S -p '' " + command); 
     } 

     InputStream in=channel.getInputStream(); 
      OutputStream out=channel.getOutputStream(); 
      ((ChannelExec)channel).setErrStream(System.err); 

      channel.connect(); 

      out.write((passwd+"\n").getBytes()); 
      out.flush(); 

     byte[] tmp=new byte[1024]; 
     while(true && isForceStop == false){     
      while(in.available()>0){ 
       int i=in.read(tmp, 0, 1024); 
       if(i<0)break; 

       mObserver.onResponse((new String(tmp, 0, i))); 

      } 
      if(channel.isClosed()){ 
       mObserver.onResponse("exit-status: "+channel.getExitStatus()); 
       mTimer.doNotify(); 
       break; 
      } 


      try{Thread.sleep(100);}catch(Exception ee){} 
     } 

     mObserver.onResponse("close channel ... ");   
     channel.disconnect(); 
     mObserver.onResponse("close session ... "); 
     session.disconnect(); 
    } 
    catch(Exception e){ 
     System.out.println(e); 
     mObserver.onErrorResponse(e.getMessage()); 
    } 



} 



public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 
    public String getPassword(){ 
     return passwd; 
    } 

    public boolean promptYesNo(String str){ 
     return true; 
    } 



    public String getPassphrase(){ return null; } 
    public boolean promptPassphrase(String message){ return true; } 
    public boolean promptPassword(String message){ 
     return true; 
    } 

    public void showMessage(String message){ 
    } 

    @Override 
    public String[] promptKeyboardInteractive(String arg0, String arg1, 
      String arg2, String[] arg3, boolean[] arg4) { 
     return null; 
    } 
} 

public void setPassword(String password){ 
    passwd=password; 
} 

public void setHost(String hostname){ 
    mHost=hostname; 
} 

public String getPassword(){ 
    return passwd; 
} 


public String getHost(){ 
    return mHost; 
} 

protected abstract String getCmd(); 

public void setObserver(SSHObserverItf observer) { 
    mObserver = observer; 
} 
} 

SSHObserverItf接口

public interface SSHObserverItf { 
public void onResponse(String line); 
public void onErrorResponse(String line); 
} 

SomeTask

public class SomeTask extends SudoExec implements SSHObserverItf{ 

private static String command = ""; 
private static String hostname = ""; 
private static String user = ""; 
private static String pass = ""; 
private static Boolean isError=false; 

private static String wait = "300"; 



static public void main(String args[]) throws IOException, ParseException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { 


    new SomeTask(hostname,user,pass); 

    if (isError == true){ 
     System.out.println("Test failed"); 
    } 
    else{ 
     System.out.println("\nSucceeded to invoke command : " + command); 
    } 

} 



public CopyPeriodMergeToExternal(String hostName, String userName, String password) throws IOException, ParseException { 

    super(hostName, userName, password); 

    SSHObserverItf observer = this; 

    super.setObserver(observer); 

    super.init(Integer.parseInt(wait) * 1000); 

} 




@Override 
protected String getCmd() { 

    isAsIs = true; 


    command="rm -f somescript.sh"; 


    System.out.println("Run followed command : " + command); 

    return command; 
} 

@Override 
public void onResponse(String line) { 
    System.out.println(line);  
} 

@Override 
public void onErrorResponse(String line) { 
    System.out.println(line); 
    System.out.println("Error has occured");   
    isError = true; 
} 
} 

SudoExec類中的主要部分是:

public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 
    public String getPassword(){ //  <--- 
     return passwd; 
    } 

    public boolean promptYesNo(String str){ 
     return true; //  <--- 
    } 

希望這本書能解決你的問題

+0

我嘗試須藤-S -p,但沒有成功。隨着jsch-0.1.38.jar我會稍後嘗試,但我有點迷路。讓我們看看是否有人提供其他解決方案。無論如何謝謝你的幫助。 – user1260255

0

我解決問題,使用visudo命令行註釋

默認requiretty