2016-10-26 71 views
0

我想從我的UAT服務器中的路徑讀取簡單的文件。從我的本地測試 - 我不斷收到JschException:Auth失敗。我使用的代碼如下:jsch身份驗證失敗時,連接到unix框

java.util.Properties config = new java.util.Properties(); 
     config.put("StrictHostKeyChecking", "no"); 

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

     String passwd = "----"; 
     session.setPassword(passwd); 
     Channel channel = session.openChannel("exec"); 

我試圖添加密鑰到我的本地驅動器的known_hosts,但它不會改變任何東西。我得到的錯誤如下:

com.jcraft.jsch.JSchException: Auth fail 
at com.jcraft.jsch.Session.connect(Session.java:473) 
at com.jcraft.jsch.Session.connect(Session.java:145) 
at gundam.uatchecker.impl.Shell.main(Shell.java:91) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

一些幫助將不勝感激。 此外,我試圖添加已知主機象下面這樣:

ssh-keyscan remote_server > ~/.ssh/known_hosts 

而且其他職位 - 似乎沒有在這裏工作。 任何想法?

+0

也許你必須指定口令連接之前? – zack6849

+0

試圖指定,以及 - 不幸沒有用。 – Amby

回答

0

下面的代碼工作: -

JSch jsch = new JSch(); 

    String path = "PATH TO PRIVATE KEY"; 


    jsch.addIdentity(path); 

    jsch.setConfig("StrictHostKeyChecking", "no"); 

    Session session = jsch.getSession(userName, ipToConnect, portToConnect); 
    session.connect(); 

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

    ((ChannelExec)channel).setCommand("COMMAND TO FIRE"); 

    channel.setInputStream(null); 

    ((ChannelExec)channel).setErrStream(System.err); 

    InputStream in = channel.getInputStream(); 

    channel.connect(); 

    //Read Response Here 

    channel.disconnect(); 
    session.disconnect();