java telnet 输出到文件,发送telnet命令并使用Java读取响应

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.

The problem is we want to monitor all sites simultaneously and currently we can only check them one at a time (via 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.

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 ?

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

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 :

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值