简体   繁体   English

在Android应用中实现JSch以运行ssh命令

[英]Implementing JSch to run ssh command in android app

I'm currently trying to code up an android app that does a simple "ls" command over ssh to a remote machine. 我目前正在尝试编写一个Android应用程序,该应用程序通过ssh向远程计算机执行简单的“ ls”命令。 I've tried to use JSch and this page , but it just doesn't seem to work. 我尝试过使用JSch和此页面 ,但是似乎不起作用。 I've looked around and it seems that I have to implement some Async task to run the code. 我环顾四周,看来我必须实现一些异步任务才能运行代码。 Here is the entire code for the main activity right now: 这是当前主要活动的完整代码:

package com.sshtest.sshtest;

import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.*;

import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.lang.String;




public class MainActivity extends Activity {

    String string;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


private class sshconnection extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... params) {

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession("name", "ipaddress", 22);
            session.setPassword("password");

            // Avoid asking for key confirmation
            Properties prop = new Properties();
            prop.put("StrictHostKeyChecking", "no");
            session.setConfig(prop);

            session.connect();

            ChannelExec channel = (ChannelExec)session.openChannel("exec");
            channel.setCommand("ls");
            channel.connect();

            InputStream input = channel.getInputStream();


            channel.disconnect();






        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;


    }

    @Override
    protected void onPostExecute(String result) {
        TextView txt = (TextView) findViewById(R.id.txtipinfo);
        txt.setText("Executed");

    }


}
  • adding uses-permission android:name="android.permission.INTERNET" to AndroidManifest.xml 向AndroidManifest.xml添加uses-permission android:name =“ android.permission.INTERNET”

This is my first attempt at any serious coding, if anyone can help it would be greatly appreciated. 这是我第一次尝试进行任何认真的编码,如果有人可以帮助的话,将不胜感激。 Thanks! 谢谢!

If you're just want to test. 如果您只是想测试。 Try not to using AsyncTask, but just simply a thread... like this 尽量不要使用AsyncTask,而只是使用一个线程...就像这样

        new Thread(new Runnable() {
        @Override
        public void run() {

            Session session;
            JSch jsch;
            try {
                jsch = new JSch();
                jsch.addIdentity(privateKey, "yourprivatekey");
                session = jsch.getSession("git", "github.com", 22);
                session.setPassword("yourpass");

                // Avoid asking for key confirmation
                Properties prop = new Properties();
                prop.put("StrictHostKeyChecking", "no");
                session.setConfig(prop);

                session.connect();

                if(session.isConnected()){
                    System.out.println(this.getClass().getSimpleName() + " CONNECTED");
                    System.out.println(this.getClass().getSimpleName() + " YOO " + jsch.getIdentityRepository().getName()+" "+session.getClientVersion() + " " + session.isConnected());
                }else{
                    System.out.println(this.getClass().getSimpleName() + " NOT CONNECTED");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }).start();

Call it in the onCreate() method on your Main Activity. 在您的主要活动的onCreate()方法中调用它。 Hope it helped ! 希望能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM