在Java中发送TAB控制字符 [英] Sending a TAB control character in Java

查看:529
本文介绍了在Java中发送TAB控制字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

编辑:解决方案必须是通用的,不仅可以在bash中使用,还可以在其他程序中,如octave(使用命令行的开源matlab实现,只是无法应用bash命令的示例)。

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).

推荐答案

正如Joachim Sauer所说,这可能是因为你的会话没有连接终端,所以像Bash这样的程序会表现得好像是非交互式的。

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.

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

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.

代码示例:

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();
            }
        }
    };
}

这篇关于在Java中发送TAB控制字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆