java 控制台上的 Telnet 连接

发布于 2024-12-04 00:59:28 字数 923 浏览 1 评论 0原文

我正在Linux操作系统中的eclipse上工作,这就是我想要做的(仅在java控制台中)-

  1. 通过(当前)telnet客户端连接到另一台远程计算机,
  2. 在该远程系统中执行一个简单的命令(类似于ls)

是那可能吗?我确信 Runtime.getRuntime.exec() 不会工作。所以使用了commons.net jar 文件。这是我的代码片段

public static void testMount() throws Exception {
    String osName = "";
    Scanner sc = new Scanner(System.in);
    TelnetClient telnet = new TelnetClient();
    System.out.println("Operating System: ");
    osName = sc.next();
    System.out.println(osName);
    String volumeToMount = "";
    String mountPoint = "";
    String ipAddress = "";
    int port = 23;

    if (osName.equalsIgnoreCase("Linux")) {
        // Linux
        ipAddress = "1.2.3.4"; //
        telnet.connect(ipAddress, port);
        volumeToMount = "/dev/hda1";
        mountPoint = "/data/Temp";
    } 
    mountFileSystem(volumeToMount, mountPoint);
}

如果您有现有的示例或者可以修改我的代码,如果您在这里分享它,我将不胜感激!

I am working on eclipse in a Linux OS and this is what i want to do (in the java console only)-

  1. Connect to another remote machine via (currently) telnet client
  2. execute a simple command in that remote system(something like ls)

Is that possible? I'm sure Runtime.getRuntime.exec() wont work. So used the commons.net jar file. Here is my code snippet

public static void testMount() throws Exception {
    String osName = "";
    Scanner sc = new Scanner(System.in);
    TelnetClient telnet = new TelnetClient();
    System.out.println("Operating System: ");
    osName = sc.next();
    System.out.println(osName);
    String volumeToMount = "";
    String mountPoint = "";
    String ipAddress = "";
    int port = 23;

    if (osName.equalsIgnoreCase("Linux")) {
        // Linux
        ipAddress = "1.2.3.4"; //
        telnet.connect(ipAddress, port);
        volumeToMount = "/dev/hda1";
        mountPoint = "/data/Temp";
    } 
    mountFileSystem(volumeToMount, mountPoint);
}

If you have an existing example or if could modify my code, I'd be thankful to you if you share it over here!

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

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

发布评论

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

评论(2

情深缘浅 2024-12-11 00:59:28

对于 Apache Commons Net TelnetClient,您必须使用对象返回的 InputStreamOutputStream(请参阅 getInputStream()getOutputStream() 方法)读取数据和发送数据(命令)。 此处提供了一个很好的示例(请参阅Telnet 和 Commons/NET)。

With Apache Commons Net TelnetClient you must use the InputStream and OutputStream returned by the object (see getInputStream() and getOutputStream() method) to read data and send data (commands). A nice example is available here (see section Telnet and Commons/NET).

浅浅 2024-12-11 00:59:28

您为什么要自己实施 Telnet?有几个库在这方面做得很好。看一下 Jakarta Net 包:http://commons.apache.org/net/

Why are you implementing Telnet yourself? There are several libraries that do it very well. Take a look on Jakarta Net package: http://commons.apache.org/net/

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