JSch / SSHJ-单击按钮即可连接到SSH服务器 [英] JSch/SSHJ - Connecting to SSH server on button click

查看:273
本文介绍了JSch / SSHJ-单击按钮即可连接到SSH服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮(使用actionPerformed()方法编写的代码)时,我尝试连接到SSH Unix服务器。我正在使用JSch连接到SSH服务器。该代码使用SwingWorker类编写,因为它是网络操作。

I'm trying to connect to SSH Unix server on button click (code written in actionPerformed() method). I'm using JSch for connecting to SSH server. The code is written in SwingWorker class as it is a network operation.

private void testConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     

        SwingWorker<Boolean, Void> sw = new SwingWorker<Boolean, Void>(){

            @Override
            protected Boolean doInBackground() throws Exception {
                JSch jsch = new JSch();

                String host = "ServerHost";
                String username = "username";
                String password = "password";

                Session session = jsch.getSession(username, host);
                session.setPassword(password);

                session.setTimeout(20000);
                System.out.println("Connecting to server...");
                session.connect();

                return true;
            }

            @Override
            public void done(){
                try {
                    System.out.println(get().toString());
                } catch (Exception ex) {
                    System.out.err(ex);
                } 
            }
        };

        sw.execute();

    }  

但是在使用正确的主机,用户名和密码后运行,我一直都遇到以下错误:

But after running the with correct host, username and password details, I get the below error all the time:

com.jcraft.jsch.JSchException: timeout: socket is not established
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at javax.swing.SwingWorker.get(SwingWorker.java:583)

但是无论何时我在独立程序中运行相同的代码,我的意思是代替编写actionPerformed()方法,如果我用常规方法编写它并从main()方法调用。它将起作用。当我将相同的代码与Button Click的actionPerformed()方法集成在一起时,它将给我上述异常。

But whenever I run the same code in standalone program, I mean instead for writing actionPerformed() method, If I write it in normal method and calling from main() method. It will work. when I integrate the same code with Button Click's actionPerformed() method, it will give me above exception.

有人可以在这里建议我做错了什么,还是应该做任何修改?

Can anyone suggest what I'm doing wrong here or any modification should be made to the code.

我尝试使用 SSHJ实现连接到SSH服务器,但出现以下错误:

I tried to connect to SSH Server using "SSHJ" implementation, but I get the below error:

java.net.SocketException: Connection reset
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at javax.swing.SwingWorker.get(SwingWorker.java:583)

有人可以帮助我-如何前进吗?

Can someone help me - how to move forward?

推荐答案

我获取了您的代码,将其包装在一些GUI代码中(并将其转换为非泛型,以便能够使用与其余JSch示例相同的设置进行编译)。这个对我有用。尝试此操作,并报告遇到的异常(它记录了更多的异常日志)。

I took your code, wrapped it in some GUI code (and converted it to non-generics to be able to compile it with the same settings as the rest of the JSch examples). It works for me. Try this, and report what exception you get (it has a bit more exception logging).

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

import com.jcraft.jsch.*;



class SwingWorkerExample {

    JTextField hostField;
    JTextField userNameField;
    JTextField passwordField;
    JPanel panel;


    public SwingWorkerExample() {
        JPanel p = panel = new JPanel(new GridLayout(0,2));
        hostField = new JTextField(20);
        userNameField = new JTextField(20);
        passwordField = new JPasswordField(20);
        JButton testButton = new JButton("connect!");
        testButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ev) {
                    testConnectionButtonActionPerformed(ev);
                }
            });
        p.add(new JLabel("host:"));
        p.add(hostField);
        p.add(new JLabel("user:"));
        p.add(userNameField);
        p.add(new JLabel("password:"));
        p.add(passwordField);
        p.add(testButton);
    }

    public JPanel getPanel() {
        return panel;
    }

    private void testConnectionButtonActionPerformed(ActionEvent evt) {

        SwingWorker sw = new SwingWorker(){

                protected Object doInBackground() throws Exception {
                    try {
                        JSch jsch = new JSch();

                        String host = hostField.getText();
                        String username = userNameField.getText();
                        String password = passwordField.getText();

                        Session session = jsch.getSession(username, host);
                        session.setPassword(password);
                        session.setConfig("StrictHostKeyChecking", "no");

                        session.setTimeout(20000);
                        System.out.println("Connecting to server...");
                        session.connect();

                        return session;
                    }
                    catch(Exception ex) {
                        ex.printStackTrace();
                        throw ex;
                    }
                }

                public void done(){
                    try {
                        System.out.println(get());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            };

        sw.execute();

    }


    public static void main(String[] egal) {
        EventQueue.invokeLater(new Runnable(){public void run() {
            SwingWorkerExample ex = new SwingWorkerExample();
            JFrame f = new JFrame("bla");
            f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            f.setContentPane(ex.getPanel());
            f.pack();
            f.setVisible(true);
        }});
    }
}

这篇关于JSch / SSHJ-单击按钮即可连接到SSH服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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