2017-12-21 1 views
0

JSch 라이브러리를 사용하여 프로그램을 작성 중이므로 shell 채널을 열고 String에 저장되는 몇 가지 명령을 실행해야합니다.
콘솔 대신 String 변수에서 입력 명령을 입력해야합니다.
난 후 pwd 같은 명령에 대해, 그 작업 벌금 whoami 등 주어진 코드에서 Jsch : Command Output unavailable
건너왔다,하지만 난이 sudo -u hiveuser -i을하려고 할 때 그는 중단가는.콘솔 대신 문자열 입력을 사용하여 쉘 채널에서 Jsch 여러 명령을 실행하는 방법

public static void main(String[] args) throws Exception 
    { 
     JSch jsch = new JSch(); 
     String host = "my.host.server"; 
     String user = "myLoginId"; 
     String pswd = "myPASSword"; 

     Session session=jsch.getSession(user,host, 22); 
     session.setPassword(pswd); 
     session.setConfig("StrictHostKeyChecking", "no"); 
     session.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password"); 
     session.connect(); 
     System.out.println("Connected"); 

     Channel channel = session.openChannel("shell"); 
     OutputStream ops = channel.getOutputStream(); 
     PrintStream ps = new PrintStream(ops); 
     channel.connect(); 
     ps.println("sudo -iu hiveuser"); 
     ps.println(pswd); 
     ps.println("hive"); 
     ps.println("desc table myHiveTable;"); 
     ps.flush(); 
     ps.close(); 

     InputStream in = channel.getInputStream(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     System.out.println("Opening..."); 

     String jarOutput; 
     while ((jarOutput = reader.readLine()) != null) 
      System.out.println(jarOutput); 
     reader.close(); 
     channel.disconnect(); 
     session.disconnect(); 

    } 

답변

0

당신은 암호 반향을 시도 할 수 :
여기에 코드입니다

echo password | sudo -iu hiveuser --stdin 
관련 문제