Simple SSH connect with JSch

后端 未结 3 2019
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 01:38

I am trying to make something from this simple example :

SSH, execute remote commands with Android

I just want to see if I can connect from my android phone

相关标签:
3条回答
  • 2020-11-30 01:50

    A Kotlin solution:

    import android.os.AsyncTask
    import android.os.Bundle
    import android.support.v7.app.AppCompatActivity
    import com.jcraft.jsch.ChannelExec
    import com.jcraft.jsch.JSch
    import java.io.ByteArrayOutputStream
    import java.util.*
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            SshTask().execute()
        }
    
        class SshTask : AsyncTask<Void, Void, String>() {
            override fun doInBackground(vararg p0: Void?): String {
                val output = executeRemoteCommand("demo", "password", "test.rebex.net")
                print(output)
                return output
            }
        }
    }
    
    fun executeRemoteCommand(username: String,
                             password: String,
                             hostname: String,
                             port: Int = 22): String {
        val jsch = JSch()
        val session = jsch.getSession(username, hostname, port)
        session.setPassword(password)
    
        // Avoid asking for key confirmation.
        val properties = Properties()
        properties.put("StrictHostKeyChecking", "no")
        session.setConfig(properties)
    
        session.connect()
    
        // Create SSH Channel.
        val sshChannel = session.openChannel("exec") as ChannelExec
        val outputStream = ByteArrayOutputStream()
        sshChannel.outputStream = outputStream
    
        // Execute command.
        sshChannel.setCommand("ls")
        sshChannel.connect()
    
        // Sleep needed in order to wait long enough to get result back.
        Thread.sleep(1_000)
        sshChannel.disconnect()
    
        session.disconnect()
    
        return outputStream.toString()
    }
    

    In build.gradle add:

    dependencies {
        ...
        compile group: 'com.jcraft', name: 'jsch', version: '0.1.54'
    }
    
    0 讨论(0)
  • 2020-11-30 01:55

    chubbsondubs's solution is perfect. I just want to share the code that I made for this problem too for people who want a quick solution:

    public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new AsyncTask<Integer, Void, Void>(){
           @Override
           protected Void doInBackground(Integer... params) {
               try {
                     executeRemoteCommand("root", "myPW","192.168.0.26", 22);
               } catch (Exception e) {
                     e.printStackTrace();
               }
               return null;
           }
        }.execute(1);
    }
    
    public static String executeRemoteCommand(String username,String password,String hostname,int port)
            throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, hostname, port);
        session.setPassword(password);
    
        // Avoid asking for key confirmation
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);
    
        session.connect();
    
        // SSH Channel
        ChannelExec channelssh = (ChannelExec)
                session.openChannel("exec");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        channelssh.setOutputStream(baos);
    
        // Execute command
        channelssh.setCommand("lsusb > /home/pi/test.txt");
        channelssh.connect();
        channelssh.disconnect();
    
        return baos.toString();
    }
    
    0 讨论(0)
  • 2020-11-30 01:57

    You have to execute that code in another thread so you don't hang the UI thread is what that exception means. If the UI thread is executing a network call it can't repaint the UI so your users sees a frozen UI that doesn't respond to them while the app is waiting on the network call to finish. Android wants to avoid bad user experiences like this so it prevents you from doing things like this by throwing this exception.

    Your onCreate() method should invoke another thread (I'd suggest using an AsyncTask over a raw thread) to perform the SSH connection. Then when it's done it can post the results back to the UI thread and safely update your application's UI from the UI thread.

    http://developer.android.com/reference/android/os/AsyncTask.html

    0 讨论(0)
提交回复
热议问题