gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:16:34 28 4
gpt4 key购买 nike

我想运行一个简单的 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

它还创建 known_hosts .ssh 内的文件文件夹。 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

最佳答案

下面是一个通过 Jsch 使用 serveo 的例子。这里唯一真正的“技巧”似乎是特定于serveo的。在 session 打开“shell”或“exec” channel 之前,Serveo 不会接受隧道请求。 Serveo 使用 shell/exec channel 向客户端发送状态消息。所以需要先打开一个shell或者exec channel ,然后再请求远程转发。
SSH 协议(protocol)期望客户端提供一个用户名来登录。 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]; }
}
}

关于android - 如何以编程方式在android中运行SSH命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62663771/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com