发送 telnet 命令并使用 Java 读取响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3763511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:35:16  来源:igfitidea点击:

Sending telnet commands and reading the response with Java

java

提问by BigMac66

I work for a large organization that supports many different sites on a nation wide network. Our vendor supplies diagnostic tools for the hardware we use at each site. The diagnostics and reports are accessed via Telnet.

我为一家大型组织工作,该组织支持全国网络上的许多不同站点。我们的供应商为我们在每个站点使用的硬件提供诊断工具。诊断和报告可通过 Telnet 访问。

The problem is we want to monitor allsites simultaneously and currently we can only check them one at a time (via telnet).

问题是我们想同时监控所有站点,目前我们一次只能检查一个(通过 telnet)。

I have already built something that will use Runtime.exec(String)to send ping commands to each IP address. But now I want to be able to send specific commands automatically using the vendor's diagnostic and reporting tools. Any ideas on the best way to do this? NOTE we have a hybrid system - some of the sites are behind a firewall some are not. A complete solution would be ideal but I will settle for a partial one as well.

我已经构建了一些Runtime.exec(String)用于向每个 IP 地址发送 ping 命令的东西。但现在我希望能够使用供应商的诊断和报告工具自动发送特定命令。关于做到这一点的最佳方法的任何想法?注意我们有一个混合系统 - 有些站点在防火墙后面,有些则没有。一个完整的解决方案将是理想的,但我也会满足于部分解决方案。

Could it be as simple as getting the input and output streams of the Process object returned by the Runtime.exec(String)and send my commands to the input and reading the response on the output? Or should I be connecting to port 23 (the usual telnet port) and then behave as any other client-server system. Or something else completely different?

是否可以像获取由 返回的 Process 对象的输入和输出流Runtime.exec(String)并将我的命令发送到输入并读取输出上的响应一样简单?或者我应该连接到端口 23(通常的 telnet 端口),然后像任何其他客户端 - 服务器系统一样运行。或者其他完全不同的东西?

I am continuing to try things out, just brainstorming at this point...

我正在继续尝试,在这一点上只是头脑风暴......

CODE: (sensitive information removed)

CODE:(删除敏感信息)

void exec(String ip)
{
  Socket sock = null;
  BufferedReader br = null;
  PrintWriter pw = null;

  try
  {
    sock = new Socket(ip, 23);

    br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    pw = new PrintWriter(sock.getOutputStream());

    this.read(br);
    System.out.println("Sending username");
    pw.println("username");
    this.read(br);  // Always blocks here
    System.out.println("Sending password");
    pw.println("password");
    this.read(br);

    pw.close();
    br.close();
    sock.close();
  }
  catch(IOException e)
  {
    e.printStackTrace();
  }
}

void read(BufferedReader br) throws IOException
{
  char[] ca = new char[1024];
  int rc = br.read(ca);
  String s = new String(ca).trim();

  Arrays.fill(ca, (char)0);

  System.out.println("RC=" + rc + ":" + s);

//String s = br.readLine();
//      
//while(s != null)
//{
//  if(s.equalsIgnoreCase("username:"))
//    break;
//          
//  s = br.readLine();
//          
//  System.out.println(s);
//}

采纳答案by Colin Hebert

Why don't you simply use the Socket system ?

为什么不简单地使用 Socket 系统?

Open a socket to the chosen server on port 23 and send your own commands from here.

在端口 23 上打开与所选服务器的套接字,并从此处发送您自己的命令。



Here is a quick example :

这是一个快速示例:

public class EchoClient {
    public static void main(String[] args) throws IOException {

        Socket pingSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            pingSocket = new Socket("servername", 23);
            out = new PrintWriter(pingSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream()));
        } catch (IOException e) {
            return;
        }

        out.println("ping");
        System.out.println(in.readLine());
        out.close();
        in.close();
        pingSocket.close();
    }
}


Resources :

资源 :

回答by irreputable

telnet is almostbare tcp. you can probably make a socket connection, write commands, read responses.

telnet几乎是裸tcp。您可能可以建立套接字连接、写入命令、读取响应。

well - telnet does have some control commands that can be mixed with data stream. you can probably program to ignore them. or, to be perfectly protocol aware, use a java telnet client package.

好吧 - telnet 确实有一些可以与数据流混合的控制命令。您可能可以通过编程忽略它们。或者,要完全了解协议,请使用 java telnet 客户端包。

回答by David Hunt

Expectis a scripted (non-Java) possibility. It's an older tool, see the Pros and Cons at Wikipedia.

Expect是一种脚本化(非 Java)可能性。这是一个较旧的工具,请参阅Wikipedia 上的优点和缺点。

回答by Dale

The problem with doing it down at the socket level is that you suddenly need to be a protocol expert, when all you want to do is a few telnet commands.

在套接字级别执行此操作的问题在于,您突然需要成为协议专家,而您只想执行一些 telnet 命令。

I wrote a driver on top of de.mud.jta, but it leaves threads open, and I don't know where, and how to fix those. Seems to me that there must be something better out there than coding at the protocol level or this old JTA stuff.

我在 de.mud.jta 之上编写了一个驱动程序,但它使线程保持打开状态,我不知道在哪里以及如何修复它们。在我看来,一定有比协议级别的编码或旧的 JTA 东西更好的东西。

回答by Serak Shiferaw

Try Checking Netty Java Library. they have a good set of Telnet , SSH and other very useful network library

尝试检查 Netty Java 库。他们有一套很好的 Telnet 、 SSH 和其他非常有用的网络库

https://netty.io

https://netty.io