JSch / SSHJ - 单击按钮连接到SSH服务器

时间:2011-07-25 16:21:20

标签: button ssh click jsch

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

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

    }  

但是在运行了正确的主机,用户名和密码详细信息后,我始终得到以下错误:

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()方法集成相同的代码时,它会给我上面的异常。

任何人都可以建议我在这里做错了,或者对代码进行任何修改。

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

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)

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

1 个答案:

答案 0 :(得分:0)

我接受了你的代码,将它包装在一些GUI代码中(并将其转换为非泛型代码,以便能够使用与其他JSch示例相同的设置进行编译)。这个对我有用。试试这个,并报告你得到的异常(它有一些异常的日志记录)。

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