Android implements SSH Client

This article implements how to use JSCH to implement a simple version of ssh client on Android to remotely execute ssh commands.

1. Start the ssh service. This article uses mac as an example.

Open Settings --> Sharing --> Select Remote Login

2. Check whether the ssh server is started successfully.

     open shell

    ssh dongxuli@ip

    Enter the password of the dongxuli account, and if no error is reported, the ssh server starts successfully.

3. Download jsch.jar: jsch-0.1.55.jar package_jsch-0.1.55.jar-Linux Documentation Resources-CSDN Download

 4. Create a new Android project and import jsch-0.1.55.jar

5. The Android source code realizes the execution of the ssh command:


class SSHTest {
    fun test(){
        Thread {
            val jsch = JSch()
            try {
                //ssh登录连接的用户名和ip
                val session = jsch.getSession("dongxuli", "192.168.31.123");
                //ssh登录连接的密码
                session.setPassword("123456");
                val config = java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
                session.connect()
                val channelExec = session.openChannel("exec") as ChannelExec
                channelExec.setCommand("ls -l /Users/xxx \n")
                channelExec.connect()
               val  inputStreamReader = BufferedReader(InputStreamReader(channelExec.inputStream))
                val errInputStreamReader = BufferedReader(InputStreamReader(channelExec.errStream))
                val runLog = StringBuilder("")
                val errLog = StringBuilder("")
                var line: String? = null
                while (inputStreamReader.readLine().also { line = it } != null) {
                    runLog.append(line).append("\n")
                }
                var errLine: String? = null
                while (errInputStreamReader.readLine().also { errLine = it } != null) {
                    errLog.append(errLine).append("\n")
                }

                Log.d("gggl" , runLog.toString())
                Log.d("gggl" , errLog.toString())
            } catch ( e : Exception) {
                e.printStackTrace();
            }
        }.start()
    }
}

After executing the test, the file list in the /User/xxx directory will be output:

 

In this example, ChannelExec is used, and only one command can be executed at a time. If you want to execute multiple commands, use ChannelShell.

ChannelExec channelExec = (ChannelExec) session.openChannel("exec");//只能执行一条指令(也可执行符合指令)
ChannelShell channelShell = (ChannelShell) session.openChannel("shell");//可执行多条指令 不过需要输入输出流

1. ChannelExec

  Separate each command with ;. Note: The execution results of each command will not affect the execution of other commands. In other words, individual commands will be executed, but there is no guarantee that each command will execute successfully.
Separate each command with &&. Note: If the previous command is executed successfully, the following command will be executed. This can ensure that after all commands are executed, the execution process is successful.

Each command is separated by ||. Explanation: || means or, the next command will be executed only after the execution of the previous command fails, until a command is executed successfully.

2. ChannelShell

  For ChannelShell, in the form of an input stream, multiple instructions can be executed, which is like using an interactive shell on the local computer (it is usually used for: interactive use). If you want to stop, there are two ways:

  Send an exit command to tell the program that this interaction is over;
  use the available method in the byte stream to get the total size of the data, and then read it in a loop.

Guess you like

Origin blog.csdn.net/mldxs/article/details/128295045