jsch->无法使用Java从UNIX跳转服务器连接到另一个UNIX服务器 [英] jsch-->Not able to connect from unix jump server to another unix servers using java

查看:151
本文介绍了jsch->无法使用Java从UNIX跳转服务器连接到另一个UNIX服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将受保护的服务器从一台unix服务器连接到另一台unix服务器.通过ssh,我可以轻松地从腻子连接,但是当从jsch进行connet时,我遇到了错误.

成功的腻子步骤->在腻子中连接server1->成功->使用"ssh user @ ip"连接server2

jsch步骤->

已连接的会话1->一台服务器已连接//注释->在服务器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());
    }

I am not able to connect secured server one unix server to another unix server.From putty by ssh i am able to connect easily but i am geeting below error when connet from jsch.

successful putty Steps->connect server1 in putty->on successful->Connect server2 using "ssh user@ip"

jsch steps-->

Connected session1-->one server connected //comment-->running command on server 1 to connect other server

Error:- Pseudo-terminal will not be allocated because stdin is not a terminal. Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,password,keyboard-interactive). ksh: changeme: not found exit-status: 127

Jsch sample program-->

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

解决方案

I am adding important part of answer:-

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

这篇关于jsch->无法使用Java从UNIX跳转服务器连接到另一个UNIX服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆