首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用JSCH Java

使用JSCH Java
EN

Stack Overflow用户
提问于 2015-05-18 14:52:32
回答 1查看 850关注 0票数 0

我试图使用JsCH连接到远程服务器,然后操作PostgreSQL。

如果我使用腻子和JSCH,一切都很好。

我唯一的问题是,即使设置了这些属性,JsCH也只接受来自Eclipse的用户名和密码。

代码语言:javascript
运行
复制
 public void connect() {

    // 
    int assigned_port;   
    final int local_port=5432;

    // Remote host and port
    final int remote_port=5432;
    final String remote_host="myserver011";

    try {
        JSch jsch = new JSch(); 

        // Create SSH session.  Port 22 is your SSH port which
        // is open in your firewall setup.
        Session session = jsch.getSession("companyusername", remote_host, 22);

        session.setPassword("password");

        // Additional SSH options.  See your ssh_config manual for
        // more options.  Set options according to your requirements.
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        config.put("Compression", "yes");
        config.put("ConnectionAttempts","2");

        session.setConfig(config);

        // Connect
        session.connect();            

        // Create the tunnel through port forwarding.  
        // This is basically instructing jsch session to send 
        // data received from local_port in the local machine to 
        // remote_port of the remote_host
        // assigned_port is the port assigned by jsch for use,
        // it may not always be the same as
        // local_port.

        assigned_port = session.setPortForwardingL(local_port, 
                remote_host, remote_port);
        System.out.println("SSH Connection succes!");


    } catch (JSchException e) {  
        e.printStackTrace();
        LOGGER.log(Level.SEVERE, e.getMessage()); return;
    }

    if (assigned_port == 0) {
        LOGGER.log(Level.SEVERE, "Port forwarding failed !"); 
        return;
    }

}

但是控制台仍然需要用户名和密码。

代码语言:javascript
运行
复制
Kerberos username [myusername]: companyusername  <---- here I set again the username
Kerberos password for companyusername : password <---- and the password
SSH Connection succes!
Opened database successfully

然后一切都正常。但我不想再设置这些,我找不到这个“错误”的来源?!我不知道,可能是服务器配置参数迫使用户手动输入?

任何建议或暗示都会很好。

EN

回答 1

Stack Overflow用户

发布于 2015-05-18 18:47:14

代码语言:javascript
运行
复制
    //this works pretty well for me.

    Session session;
    ssh = new JSch();
            session = ssh.getSession(username,hostname,port);
            session.setConfig(config);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking","no");
            session.connect();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30306437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档