【问题标题】:How to connect SSH tunnel tcp/udp in android如何在 android 中连接 SSH 隧道 tcp/udp
【发布时间】:2021-12-27 06:30:33
【问题描述】:

如何使用java语言在android中连接SSH Tunnel TCP/UDP,我尝试使用JSCH但它不起作用,有没有其他方法可以连接

【问题讨论】:

  • 您的问题完全不清楚,缺少所有相关信息

标签: java android ssh tcp udp


【解决方案1】:

正如@jeb 所说,您的问题尚不清楚,但是如果您尝试使用 java 在 android 中建立 ssh 连接,您可以使用它:

首先,您必须在清单中添加互联网权限

<uses-permission android:name="android.permission.INTERNET" />

然后在 Thread/Executor 或 Async 类中使用以下代码(在这种情况下我使用 Thread):

final Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                // create a connection instance and connect to it
                Connection ssh = new Connection(hostname);

                ssh.connect();
                final boolean authorized = ssh.authenticateWithPassword(username,
                        password);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!authorized)
                            Toast.makeText(MainActivity.this, "could not establish connection", Toast.LENGTH_SHORT).show();
                        else {
                            Toast.makeText(MainActivity.this, "wohoo", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


                // if authorized, create the session
                Session session = ssh.openSession();

                // terminate the session
                session.close();

                // terminate the connection
                ssh.close();
            } catch (IOException e) {
                e.printStackTrace(System.err);
                System.out.println(e.getMessage());
                //System.exit(2);
            }
        }
    });

【讨论】:

  • 非常感谢,我在哪里可以获得连接库?
  • 不客气,我在这个例子中使用 ch.ethz.ssh2.Connection, ch.ethz.ssh2.Session
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 2021-02-15
  • 2017-08-24
  • 2018-10-06
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多