发送telnet命令并使用Java读取响应 [英] Sending telnet commands and reading the response with Java

查看:289
本文介绍了发送telnet命令并使用Java读取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 all sites simultaneously and currently we can only check them one at a time (via telnet).

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

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.

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

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?

我正在继续尝试,只是在这一点集思广益......

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

代码:(删除敏感信息)

CODE: (sensitive information removed)

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


推荐答案

为什么不你只需使用套接字系统吗?

Why don't you simply use the Socket system ?

在端口23上打开所选服务器的套接字并从这里发送你自己的命令。

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

以下是一个简单示例:

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






资源:

  • oracle.com - All about sockets

这篇关于发送telnet命令并使用Java读取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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