java telnet 服务器_java中 用telnet 判断服务器远程端口是否开启

package net.jweb.common.util;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.util.Map;public classUsingProcessBuilder {public staticString getNodeStatuss() {

System.out.println("UsingProcessBuilder.getNodeStatuss()");

String command= "telnet 192.168.4.200 3306";

command= "cmd /d dir";//ok

command = "cmd";//ok

command = "calc";//ok

command = "telnet";//不行

Process process;

StringBuffer sb= newStringBuffer();try{//Process proc=Runtime.getRuntime().exec("notepad");

process=Runtime.getRuntime().exec(command);//InputStream inputStream = process.getInputStream();//

//int read;//byte[] b = new byte[1024*1024];//while ((read=inputStream.read(b))!=-1) {//String str = new String(b);//sb.append(str);//}//inputStream.close();

final InputStream is1=process.getInputStream();newThread() {public voidrun() {

BufferedReader br= new BufferedReader(newInputStreamReader(is1));try{

String lineB= null;while ((lineB = br.readLine()) != null)

{if (lineB != null)

System.out.println(lineB);

}

}catch(IOException e) {

e.printStackTrace();

}

}

}.start();

}catch(IOException e) {

e.printStackTrace();

}returnsb.toString();

}/**

* 获取Windows系统下的网卡的MAC地址

*

* @return*/

public staticString getPhysicalAddress() {

Process p= null;try{//执行ipconfig /all命令//"cmd ", " 192.168.4.200", " 3306"//"ipconfig", "/all"//p = new ProcessBuilder("ipconfig", "/all").start();//p = new ProcessBuilder("telnet", "host 192.168.4.200").start();

p = Runtime.getRuntime().exec("telnet 192.168.4.200 3306");//p = new ProcessBuilder("cmd", " dir").start();

} catch(IOException e) {

e.printStackTrace();return "eeeeeeeee";

}byte[] b = new byte[1024];int readbytes = -1;

StringBuffer sb= newStringBuffer();//读取进程输出值

InputStream in =p.getInputStream();try{while ((readbytes = in.read(b)) != -1) {

sb.append(new String(b, 0, readbytes));

}

}catch(IOException e1) {

}finally{try{in.close();

}catch(IOException e2) {

}

}returnsb.toString();

}public static voidexecuteMyCommand2() {

ProcessBuilder pb= null;

String sysatt= null;try{//创建一个进程示例

pb = new ProcessBuilder("cmd.exe");//获取系统参数并打印显示

Map env =pb.environment();//Iterator it = env.keySet().iterator();//while (it.hasNext()) {//sysatt = (String) it.next();//System.out.println("System Attribute:" + sysatt + "="//+ env.get(sysatt));//}//设置工作目录

pb.directory(new File("C:\\Windows\\winsxs\\wow64_microsoft-windows-commandprompt_31bf3856ad364e35_6.1.7601.17514_none_f387767e655cd5ab"));

Process p=pb.start();//将要执行的Windows命令写入

BufferedWriter bw = new BufferedWriter(newOutputStreamWriter(

p.getOutputStream()));//'/r/n'是必须写入的//bw.write("test.bat /r/n");

bw.write("ping -t www.baidu.com /r/n");//flush()方法是必须调用的

bw.flush();//将执行结果打印显示

InputStream is =p.getInputStream();

InputStreamReader isr= new InputStreamReader(is, "GBK");

BufferedReader br= newBufferedReader(isr);

String line;while ((line = br.readLine()) != null) {

System.out.println(line);

}

}catch(Exception e) {

e.printStackTrace();

}

}/**

* 执行自定义的一个命令,该命令放在C:/temp下,并且需要2个环境变量的支持。*/

public staticboolean executeMyCommand() {//创建系统进程创建器

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");//获得进程的环境

Map env =pb.environment();//设置和去除环境变量

env.put("VAR1", "myValue");

env.remove("VAR0");

env.put("VAR2", env.get("VAR1") + ";");//切换工作目录

pb.directory(new File("C:/temp"));try{//得到进程实例

Process p =pb.start();//等待该进程执行完毕

if (p.waitFor() != 0) {//如果进程运行结果不为0,表示进程是错误退出的//获得进程实例的错误输出

InputStream error =p.getErrorStream();//do something

}//获得进程实例的标准输出

InputStream sdin =p.getInputStream();

}catch(IOException e) {

}catch(InterruptedException e) {

}return true;

}public static voidmain(String[] args) {

String address=UsingProcessBuilder.getNodeStatuss();//executeMyCommand2();

System.out.println(address);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值