2014-09-25 31 views
1

我無法將安全服務器的一個unix服務器連接到另一個unix服務器。通過ssh的putty我能夠輕鬆連接,但我是當jsch的connet出現錯誤時出現錯誤。 >jsch - >無法使用java從unix跳轉服務器連接到另一個unix服務器

連接會話1 - - >一臺服務器

成功膩子步驟 - >在putty-連接服務器1>上successful->使用 「SSH用戶@ IP」

jsch步驟連接服務器2連接//註釋 - >在服務器1上運行命令以連接其他服務器

錯誤: - 僞終端將不被分配,因爲標準輸入不是終端。 權限被拒絕,請重試。 權限被拒絕,請重試。 權限被拒絕(公鑰,密碼,鍵盤交互)。 KSH:changeme:未發現 退出狀態:127

Jsch示例程序 - >

java.util.Properties配置=新java.util.Properties();

 config.put("StrictHostKeyChecking", "no"); 
     JSch jsch = new JSch(); 
     jsch.setKnownHosts("C://known_hosts.txt"); 
     session=jsch.getSession(user1, server1, 22); 
     session.setPassword(password1); 
     session.setConfig(config); 
     session.connect(); 
     System.out.println("Connected session1"); 

     String command ="ssh"+" "+"[email protected];"+"password" ; 

     channel=session.openChannel("exec"); 
     ((ChannelExec)channel).setCommand(command); 
     channel.setInputStream(null); 
     InputStream in=channel.getInputStream(); 
     ((ChannelExec)channel).setErrStream(System.err); 
     channel.connect(); 
     byte[] tmp=new byte[1024]; 
     while(true){ 
      while(in.available()>0){ 
       int i=in.read(tmp, 0, 1024); 
       if(i<0)break; 
       System.out.print("server 1"+new String(tmp, 0, i)); 
      } 
      if(channel.isClosed()){ 
       System.out.println("exit-status: "+channel.getExitStatus()); 
       break; 
      } 
      try{Thread.sleep(1000);}catch(Exception ee){} 
     channel.disconnect(); 
     session.disconnect(); 

回答

2

我加入的答案重要的部分: -

 JSch jsch = new JSch(); 
     jsch.setKnownHosts("C:/My Program Files/eclipse/workspace/StatusTracker/known_hosts.txt"); 
    // jsch.setKnownHosts(knownHostLoc); 
     //Jump server connection session started 
     jumpServerSession = jsch.getSession(userid, jump server ip/hostname, 22); 
     jumpServerSession.setPassword(jump server password); 
     java.util.Properties config = new java.util.Properties(); 
     config.put("StrictHostKeyChecking", "no"); 
     jumpServerSession.setConfig(config); 
     jumpServerSession.connect(); 
    System.out.println("The session has been established to "+jump server userid+"@"+jump server name); 

     int assinged_port = jumpServerSession.setPortForwardingL(0, other server ip, 22); 
     System.out.println("portforwarding: "+ 
          "localhost:"+assinged_port+" -> "+other server ip+":"+22); 
     //Main server connection session started 
     targetServerSession = jsch.getSession(fileBO.getServerUserId(), "127.0.0.1", assinged_port); 
     targetServerSession.setHostKeyAlias(other server ip); 
     targetServerSession.setPassword(other server password); 
     java.util.Properties config1 = new java.util.Properties(); 
     config1.put("StrictHostKeyChecking", "no"); 
     targetServerSession.setConfig(config1); 
     targetServerSession.connect(); 


     channel = targetServerSession.openChannel("sftp"); 
     channel = targetServerSession.openChannel("exec"); 

//command want to execute on dest server 
     ((ChannelExec)channel).setCommand("pwd"); 

      channel.setInputStream(null); 
    InputStream in11=channel.getInputStream(); 
    ((ChannelExec)channel).setErrStream(System.err); 
    channel.connect(); 
    byte[] tmp1=new byte[1024]; 
    while(true){ 
     while(in11.available()>0){ 
      int i1=in11.read(tmp1, 0, 1024); 
      if(i1<0)break; 
      System.out.print(new String(tmp1, 0, i1)); 
     } 
     if(channel.isClosed()){ 
      System.out.println("exit-status: "+channel.getExitStatus()); 
      break; 
     } 
     try{Thread.sleep(1000);}catch(Exception ee){} 
    } 

    } catch (final JSchException e) { 
     LOGGER.error(e.getMessage()); 
    } 
+0

難道這項工作?我也試圖實施類似的事情。我有兩個疑問:'1。 int assinged_port = jumpServerSession.setPortForwardingL(0,other server ip,22);': - 這裏爲什麼我們把本地端口設置爲0?它不應該是22而是。因爲這個本地端口是Jump-Server'2的端口。 targetServerSession = jsch.getSession(fileBO.getServerUserId(),「127.0.0.1」,assinged_port);': - 在這裏,在使用第三臺服務器創建主會話時,我們將IP作爲LocalHost傳遞。爲什麼? 。請糾正我的理解是不正確的。我對ssh連接很陌生。 – AnirbanDebnath 2016-07-28 15:40:09

相關問題