如何使用我的Java / Android应用程序进行SSH?

时间:2013-11-30 16:20:14

标签: java android ssh raspberry-pi

我正在尝试使用Ganymed库建立SSH连接。但是,在使用该库时,似乎在创建一个Connection对象到我要连接的主机后,它不会最终连接到它,而是直接进入我的代码的catch块。此应用程序将连接到我的Raspberry Pi以执行一些命令。

有人可以向我解释为什么会发生这种情况吗?我在网上和这个网站上到处都是,我似乎无法找到任何解决方案。我是否需要一些额外的文件来进行SSH操作?如果我能得到一些帮助,我将非常感激。以下是尝试建立SSH连接的类的代码。

package com.sys.videostreamer; 
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

public class Options extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);
        Button startssh = (Button) findViewById(R.id.camera_start);
        System.out.println("pass");
        startssh.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String hostname = "10.X.X.XX";
                String username = "username";
                String password = "password";

                try {
                    // create a connection instance and connect to it
                    Connection ssh = new Connection(hostname);

                    ssh.connect();
                    boolean authorized = ssh.authenticateWithPassword(username,
                            password);
                    if (authorized == false)
                        throw new IOException(
                                "Could not authenticate connection, please try again.");

                    // if authorized, create the session
                    Session session = ssh.openSession();
                    session.execCommand("mkdir test");

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.options, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

它可能不起作用,因为您在主线程上执行了联网任务。在android中,所有联网请求都必须在这样的后台线程中完成:

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

当然,您必须在清单中添加互联网许可

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