jsch-->;无法使用java从unix跳转服务器连接到其他unix服务器

jsch-->;无法使用java从unix跳转服务器连接到其他unix服务器,java,unix,jsch,Java,Unix,Jsch,我无法将一台unix服务器连接到另一台unix服务器。通过ssh连接putty,我可以轻松连接,但当从jsch连接时,我发现以下错误 成功的putty步骤->在putty中连接服务器1->成功的->使用“ssh”连接服务器2user@ip" jsch步骤--> 已连接会话1-->已连接一台服务器//注释-->在服务器1上运行命令以连接其他服务器 错误:- 不会分配伪终端,因为stdin不是终端。 权限被拒绝,请重试。 权限被拒绝,请重试。 权限被拒绝(公钥、密码、键盘交互)。 ksh:找不到更改

我无法将一台unix服务器连接到另一台unix服务器。通过ssh连接putty,我可以轻松连接,但当从jsch连接时,我发现以下错误

成功的putty步骤->在putty中连接服务器1->成功的->使用“ssh”连接服务器2user@ip"

jsch步骤-->

已连接会话1-->已连接一台服务器//注释-->在服务器1上运行命令以连接其他服务器

错误:- 不会分配伪终端,因为stdin不是终端。 权限被拒绝,请重试。 权限被拒绝,请重试。 权限被拒绝(公钥、密码、键盘交互)。 ksh:找不到更改名称 退出状态:127

Jsch示例程序-->

java.util.Properties config=new java.util.Properties()

config.put(“StrictHostKeyChecking”、“no”);
JSch JSch=新的JSch();
jsch.setKnownHosts(“C://known_hosts.txt”);
session=jsch.getSession(user1、server1、22);
session.setPassword(password1);
session.setConfig(config);
session.connect();
System.out.println(“连接会话1”);
String command=“ssh”+“”+”user@ip;“+”密码;
channel=session.openChannel(“exec”);
((ChannelExec)channel).setCommand(command);
channel.setInputStream(空);
InputStream in=channel.getInputStream();
((ChannelExec)channel.setErrStream(System.err);
channel.connect();
字节[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);

如果(i我正在添加答案的重要部分:-

      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-JSch=new-JSch();
setKnownHosts(“C:/My Program Files/eclipse/workspace/StatusTracker/known_hosts.txt”);
//jsch.setKnownHosts(knownHostLoc);
//跳转服务器连接会话已启动
jumpServerSession=jsch.getSession(用户标识,跳转服务器ip/主机名,22);
jumpServerSession.setPassword(跳转服务器密码);
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
jumpServerSession.setConfig(配置);
jumpServerSession.connect();
System.out.println(“会话已建立到“+跳转服务器userid+”@“+跳转服务器名称”);
int assinged_port=jumpServerSession.setPortForwardingL(0,其他服务器ip,22);
System.out.println(“端口转发:”+
“本地主机:“+assinged_端口+”->“+其他服务器ip+”:“+22);
//主服务器连接会话已启动
targetServerSession=jsch.getSession(fileBO.getServerUserId(),“127.0.0.1”,assinged_端口);
targetServerSession.setHostKeyAlias(其他服务器ip);
targetServerSession.setPassword(其他服务器密码);
java.util.Properties config1=新建java.util.Properties();
配置1.输入(“密钥检查”、“否”);
targetServerSession.setConfig(配置1);
targetServerSession.connect();
channel=targetServerSession.openChannel(“sftp”);
channel=targetServerSession.openChannel(“exec”);
//要在dest服务器上执行的命令
((ChannelExec)channel.setCommand(“pwd”);
channel.setInputStream(空);
in11=channel.getInputStream();
((ChannelExec)channel.setErrStream(System.err);
channel.connect();
字节[]tmp1=新字节[1024];
while(true){
while(in11.available()>0){
inti1=in11.read(tmp1,0,1024);

如果(i1不进行端口转发,您可以在连接之前简单地使用channel.setPty(true)。之后,您可以使用outPutStream提供密码。

这是否有效?我也在尝试实现类似的事情。我有两个疑问:
1.int assinged\u port=jumpServerSession.setPortForwardingL(0,其他服务器ip,22)
:-这里我们为什么要将本地端口指定为0?不应该改为22。因为,这个本地端口是跳转服务器的端口
2.targetServerSession=jsch.getSession(fileBO.getServerUserId(),“127.0.0.1”,assinged_Port);
:-这里,在创建与第三台服务器的主会话时,我们将IP作为本地主机传递。为什么???。请纠正我的理解不正确。我是ssh连接的新手。什么是
fileBO
。。您的回答中有6个未声明的变量,这是如何接受的?
      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());
    }