如何以编程方式 运行 android 中的 SSH 命令

How to run SSH command in android programmatically

我想 运行 一个简单的 SSH 命令,如 ssh -R 80:localhost:1337 serveo.net,我知道有库 jsch 可以执行此操作,但没有用户名和密码将无法使用。在我的例子中,不需要身份验证。我该怎么做?

更新 当我 运行 命令 ssh -R 80:localhost:1337 serveo.net -v 时,我得到以下输出

debug1: Server host key: ssh-rsa SHA256:07jcXlJ4SkBnyTmaVnmTpXuBiRx2+Q2adxbttO9gt0M
The authenticity of host 'serveo.net (159.89.214.31)' can't be established.
RSA key fingerprint is SHA256:07jcXlJ4SkBnyTmaVnmTpXuBiRx2+Q2adxbttO9gt0M.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'serveo.net,159.89.214.31' (RSA) to the list of known hosts.
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/paranoid/.ssh/id_rsa 
debug1: Will attempt key: /home/paranoid/.ssh/id_dsa 
debug1: Will attempt key: /home/paranoid/.ssh/id_ecdsa 
debug1: Will attempt key: /home/paranoid/.ssh/id_ecdsa_sk 
debug1: Will attempt key: /home/paranoid/.ssh/id_ed25519 
debug1: Will attempt key: /home/paranoid/.ssh/id_ed25519_sk 
debug1: Will attempt key: /home/paranoid/.ssh/id_xmss 
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /home/paranoid/.ssh/id_rsa
debug1: Trying private key: /home/paranoid/.ssh/id_dsa
debug1: Trying private key: /home/paranoid/.ssh/id_ecdsa
debug1: Trying private key: /home/paranoid/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/paranoid/.ssh/id_ed25519
debug1: Trying private key: /home/paranoid/.ssh/id_ed25519_sk
debug1: Trying private key: /home/paranoid/.ssh/id_xmss
debug1: Next authentication method: keyboard-interactive
debug1: Authentication succeeded (keyboard-interactive).
Authenticated to serveo.net ([159.89.214.31]:22).
debug1: Remote connections from LOCALHOST:80 forwarded to local address localhost:1337
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LC_ADDRESS = en_US.UTF-8
debug1: Sending env LC_NAME = en_US.UTF-8
debug1: Sending env LC_MONETARY = en_US.UTF-8
debug1: Sending env LC_PAPER = en_US.UTF-8
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_IDENTIFICATION = en_US.UTF-8
debug1: Sending env LC_TELEPHONE = en_US.UTF-8
debug1: Sending env LC_MEASUREMENT = en_US.UTF-8
debug1: Sending env LC_TIME = en_US.UTF-8
debug1: Sending env LC_NUMERIC = en_US.UTF-8
debug1: remote forward success for: listen 80, connect localhost:1337
debug1: All remote forwarding requests processed
Forwarding HTTP traffic from https://vacuus.serveousercontent.com

它还会在 .ssh 文件夹中创建 known_hosts 文件。 known_hosts id 的内容如下

|1|2K3SBFWPCPqI3poBW2X99LiuP8c=|cqCa7m1e23x1P9UpcUuPac+KKI8= ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDxYGqSKVwJpQD1F0YIhz+bd5lpl7YesKjtrn1QD1RjQcSj724lJdCwlv4J8PcLuFFtlAA8AbGQju7qWdMN9ihdHvRcWf0tSjZ+bzwYkxaCydq4JnCrbvLJPwLFaqV1NdcOzY2NVLuX5CfY8VTHrps49LnO0QpGaavqrbk+wTWDD9MHklNfJ1zSFpQAkSQnSNSYi/M2J3hX7P0G2R7dsUvNov+UgNKpc4n9+Lq5Vmcqjqo2KhFyHP0NseDLpgjaqGJq2Kvit3QowhqZkK4K77AA65CxZjdDfpjwZSuX075F9vNi0IFpFkGJW9KlrXzI4lIzSAjPZBURhUb8nZSiPuzj
|1|F1SIE4/IIEjZPJfHBIx90xnSjSU=|NKCdGqv3SFcGcAqPLvVfuRXI4Ok= ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDxYGqSKVwJpQD1F0YIhz+bd5lpl7YesKjtrn1QD1RjQcSj724lJdCwlv4J8PcLuFFtlAA8AbGQju7qWdMN9ihdHvRcWf0tSjZ+bzwYkxaCydq4JnCrbvLJPwLFaqV1NdcOzY2NVLuX5CfY8VTHrps49LnO0QpGaavqrbk+wTWDD9MHklNfJ1zSFpQAkSQnSNSYi/M2J3hX7P0G2R7dsUvNov+UgNKpc4n9+Lq5Vmcqjqo2KhFyHP0NseDLpgjaqGJq2Kvit3QowhqZkK4K77AA65CxZjdDfpjwZSuX075F9vNi0IFpFkGJW9KlrXzI4lIzSAjPZBURhUb8nZSiPuzj

我不认为你的 ssh 命令没有使用任何身份验证。请使用 verbose out with ssh -v ... 验证它。我猜它使用 publickey 和来自 ~/.ssh/id_rsa 的默认密钥 id_rsa 并且服务器只接受任何密钥。

如果是这种情况,您可以使用 jsch 执行相同的操作。

final JSch jSch = new JSch();
jSch.addIdentity(idRsaFile);
final Session newSession = jSch.getSession(null, "serveo.net", 22);
newSession.setConfig("PreferredAuthentications", "publickey");
newSession.setConfig("StrictHostKeyChecking", "no");

session.setUserInfo(new UserInfo() {
        @Override
        public String getPassphrase() {
            return null;
        }

        @Override
        public String getPassword() {
            return null;
        }

        @Override
        public boolean promptPassword(String message) {
            return false;
        }

        @Override
        public boolean promptPassphrase(String message) {
            return false;
        }

        @Override
        public boolean promptYesNo(String message) {
            return false;
        }

        @Override
        public void showMessage(String message) {

        }
    });
newSession.connect(connectTimeout);

然后

newSession.setPortForwardingR(...);

这里是一个通过Jsch使用serveo的例子。这里唯一真正的“技巧”似乎是 serveo 特有的。在会话打开“shell”或“exec”通道之前,Serveo 不会接受隧道请求。 Serveo 使用 shell/exec 通道向客户端发送状态消息。所以需要先开启一个shell或者exec通道,然后请求远程转发

SSH 协议要求客户端提供一个用户名来登录。如果您不提供,ssh 命令行实用程序默认使用本地用户名。 Jsch 同样默认使用 system property "user.name" 的值。 Serveo 有点不寻常,因为它会接受来自任何人的 SSH 连接,因此您可以使用任何您喜欢的名称。您可以让 jsch 使用它的默认值——假设它适用于 android——或者您可以使用硬编码名称,如“x”或“foo”。

import com.jcraft.jsch.*;

public class App {
    public static void main(String[] arg) {
        JSch.setLogger(new MyLogger());
        JSch jsch = new JSch();
        try {
            final Session session = jsch.getSession("serveo.net");
            session.setHostKeyRepository(new NoSecurityRepo());
            session.connect();

            // Establish a shell channel to receive messages from serveo
            ChannelShell cc = (ChannelShell)session.openChannel("shell");
            cc.setPty(false);
            cc.setInputStream(System.in);
            cc.setOutputStream(System.out);
            cc.setExtOutputStream(System.err);
            cc.connect();

            // Now open a remote forward
            session.setPortForwardingR(null, 80, "localhost", 3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class MyLogger implements Logger {
        @Override
        public boolean isEnabled(int level) { return true; }
        @Override
        public void log(int level, String message) {
            System.err.printf("%s\t%s%n", level, message);
        }
    }

    /*
     * This implementation of HostKeyRepository is just for demonstration.
     * Every time you use it in production, a kitten dies. Please think of
     * the kittens.
     */
    private static class NoSecurityRepo implements HostKeyRepository {
        @Override
        public int check(String host, byte[] key) { return HostKeyRepository.OK; }
        @Override
        public void add(HostKey hostkey, UserInfo ui) { }
        @Override
        public void remove(String host, String type) { }
        @Override
        public void remove(String host, String type, byte[] key) { }
        @Override
        public String getKnownHostsRepositoryID() { return "NoSecurityRepo"; }
        @Override
        public HostKey[] getHostKey() { return new HostKey[0]; }
        @Override
        public HostKey[] getHostKey(String host, String type) { return new HostKey[0]; }
    }
}