在Java中发送TAB控制字符

发布于 2024-11-01 11:08:46 字数 361 浏览 6 评论 0原文

我正在使用 Ganymede ssh 库 (http://www.ganymed.ethz.ch/ssh2/) 连接到 ssh 服务器(显然;))。

通常,在使用 bash 时,当用户按键盘上的 TAB 键时,会出现可能的命令完成列表。我想实现相同的行为,但手动发送制表符。不幸的是我不知道该怎么做。发送“\t”不起作用。

编辑:解决方案必须是一个通用的,它不仅可以在bash中工作,而且可以在其他程序中工作,例如octave(使用命令行的开源matlab实现,只是不能应用bash命令的示例) 。

I am using the Ganymede ssh library (http://www.ganymed.ethz.ch/ssh2/)
to connect to a ssh server (obviously ;)).

Normally, when using the bash, when a user presses the TAB key on the keyboard a list of possible command completions appears. I want to achieve the same behaviour, but send the tab character manually. Unfortunately I am not aware how to do this. Sending "\t" does not work.

Edit: The solution must be a generic which does not only work in bash, but also in other programs, like octave (open source matlab implementation with command line, just an example where bash commands cannot be applied).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛刀软妹 2024-11-08 11:08:46

正如 Joachim Sauer 所说,这可能是因为您的会话没有附加终端,因此 Bash 等程序的行为就像它们被非交互式调用一样。

Ganymede ssh 库的文档暗示调用 session.requestPTY(...) 将要求 SSH 服务器将伪 TTY 附加到会话。这就像将“-t”标志传递给 ssh 命令。

代码示例:

import java.io.IOException;
import java.io.InputStream;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;


public class Main {
    public static void main(String[] args) throws Exception {
        new Main().test();
    }

    private Session sess;

    public void test() throws IOException, InterruptedException {
        Connection c  = new Connection("myserver", 22);
        c.connect();
        boolean OK = c.authenticateWithPassword("user", "pass");
        if (!OK) throw new IOException("Bad password");
        sess = c.openSession();
        sess.requestPTY("vt220");
        new Thread(stdoutLogger).start();
        sess.execCommand("/bin/bash");
        Thread.sleep(2000);
        sess.getStdin().write("echo Hello\n".getBytes());
        sess.getStdin().write("ls -l /tm\t".getBytes());
        Thread.sleep(4000);
        sess.close();
        c.close();
    }

    Runnable stdoutLogger = new Runnable() {
        public void run() {
            InputStream is = sess.getStdout();
            int b;
            try {
                while ( (b = is.read()) != -1) {
                    System.out.println("Read " + b + " - " + (char)b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
}

As Joachim Sauer said, this is probably because your session does not have a terminal attached, so programs such as Bash will behave as though they have been called non-interactively.

The documentation for Ganymede ssh library implies that calling session.requestPTY(...) will ask the SSH server to attach a pseudo TTY to the session. It is like passing the "-t" flag to the ssh command.

Code sample:

import java.io.IOException;
import java.io.InputStream;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;


public class Main {
    public static void main(String[] args) throws Exception {
        new Main().test();
    }

    private Session sess;

    public void test() throws IOException, InterruptedException {
        Connection c  = new Connection("myserver", 22);
        c.connect();
        boolean OK = c.authenticateWithPassword("user", "pass");
        if (!OK) throw new IOException("Bad password");
        sess = c.openSession();
        sess.requestPTY("vt220");
        new Thread(stdoutLogger).start();
        sess.execCommand("/bin/bash");
        Thread.sleep(2000);
        sess.getStdin().write("echo Hello\n".getBytes());
        sess.getStdin().write("ls -l /tm\t".getBytes());
        Thread.sleep(4000);
        sess.close();
        c.close();
    }

    Runnable stdoutLogger = new Runnable() {
        public void run() {
            InputStream is = sess.getStdout();
            int b;
            try {
                while ( (b = is.read()) != -1) {
                    System.out.println("Read " + b + " - " + (char)b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
}
撕心裂肺的伤痛 2024-11-08 11:08:46

您可以使用此命令:

compgen -c

它列出 shell 中所有可能的命令。您可以添加

grep <something> 

过滤输出

You can use this command :

compgen -c

It list all possible commands in the shell. You may add

grep <something> 

to filter the output

一张白纸 2024-11-08 11:08:46

你说的不是依赖于命令shell吗?我认为 '\t' 不起作用,因为这是制表符,而不是制表符按键,它被 shell 解释为列出可能的命令完成的命令。

因为这是您要询问的库,所以在我看来您想要什么,您必须自己实现,因为我认为 shell 通常会处理这个问题。

Isn't what you're talking about dependent on the command shell? I don't think '\t' works because that's the tab character, not the tab keypress which gets interpreted by your shell as a command that lists the possible command completions.

As this is a library you're asking about, it seems to me what you want, you'd have to implement yourself as the shell normally handles that, I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文