簡體   English   中英

jsch - >無法使用java從unix跳轉服務器連接到另一個unix服務器

[英]jsch-->Not able to connect from unix jump server to another unix servers using java

我無法將安全服務器的一個unix服務器連接到另一個unix服務器。通過ssh從putty我可以輕松連接但是當我從jsch connet時我正在下面的錯誤。

成功的putty步驟 - >在putty中連接server1->在成功 - >使用“ssh user @ ip”連接server2

jsch步驟 - >

連接session1 - >一台服務器連接//注釋 - >在服務器1上運行命令以連接其他服務器

錯誤: -由於stdin不是終端,因此不會分配偽終端。 許可被拒絕,請再試一次。 許可被拒絕,請再試一次。 權限被拒絕(公鑰,密碼,鍵盤交互)。 ksh:changeme:未找到退出狀態:127

Jsch示例程序 - >

java.util.Properties config = new 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"+"  "+"user@ip;"+"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();

我正在補充說明的重要部分: -

      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());
    }

您可以在連接之前使用channel.setPty(true),而不是進行端口轉發。 在此之后,您可以使用outPutStream提供密碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM