今天闲来无事,整理了一下以前的小东西,有点笨拙,实难拿出手,望大神 斧正。
需求:给了一些IP地址,让ping一下 那些地址是通的,哪些是不通的。
于是在网上查找了一下资料 自己整理了一下,就写出了这个小东西。
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PingServer {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("192.168.100.137");
list.add("192.168.100.138");
list.add("192.168.100.139");
list.add("192.168.100.140");
list.add("192.168.100.142");
Map<String, String> map = new HashMap<String, String>();
map = PingServer.ping(list);
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
}
public static Map ping(List<String> ipList) {
String result = null;
Map<String, String> map = new HashMap<String, String>();
for (String ip : ipList) {
if (ip != null && !"".equals(ip)) {
try {
result = hostReachable(ip);
if(result.equals("false")){
String msg=" 不可达";
map.put(ip, msg);
}else{
map.put(ip, result);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return map;
}
private static String hostReachable(String ip) throws UnsupportedEncodingException {//主机ip可达
if(ip==null){
return "false";
}
StringBuffer sb = new StringBuffer();
Runtime run = Runtime.getRuntime();
Process process = null;
InputStream is = null;
try {
Properties props = System.getProperties();
String os = props.getProperty("os.name");
// windows
if (os.startsWith("W")) {
process = run.exec("ping " + ip + " -n 3 -w 8000");//ping3次 如果8000毫秒后没有收到结果为不通
// linux
} else {
if (ip.indexOf(":") < 0) {//没有“:” pingipv4 有":" pingipv6
process = run.exec("ping " + ip + " -c 3 -w 8000");
} else {
process = run.exec("ping6 " + ip + " -c 3 -w 8000");
}
}
is = process.getInputStream();
int t = 0;
while ((t = is.read()) != -1) {
sb.append((char) t);
}
} catch (IOException ex) {
return "false";
} finally {
try {
if (is != null) {
is.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
return "false";
}
}
String resultUpperCase = sb.toString().toUpperCase();//stringbuffer 转换string类型并大写
int tag = resultUpperCase.indexOf("TTL");//查找第一个 字符所在的位置
int ms = resultUpperCase.indexOf("MS")+2;
String res = null;
if (resultUpperCase.getBytes().length != resultUpperCase.length()) {
String result = new String(sb.toString().getBytes("iso8859-1"),"gb2312");
//如果通 返回相应时间
// System.out.println("resultUpperCase:"+result.trim());//打印ping结果
if (tag >= 0) {
res = result.substring(result.indexOf("时间")+2,result.indexOf("ms")+2);//相应时间
// System.out.println("相应时间中文:"+res);
}
}else{
if (tag >= 0) {
res = resultUpperCase.substring(resultUpperCase.indexOf("TIME")+4,resultUpperCase.indexOf("MS")+2);//相应时间
// System.out.println("相应时间英文:"+res);
}
}
if (tag == -1) {//没有找到
return "false";
} else {
//log("ok");
return res;
}
}
public static boolean isChinese(String str){//判断是否为中文
boolean temp = false;
Pattern p=Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher m=p.matcher(str);
if(m.find()){
temp = true;
}
return temp;
}
}
需求:给了一些IP地址,让ping一下 那些地址是通的,哪些是不通的。
于是在网上查找了一下资料 自己整理了一下,就写出了这个小东西。
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PingServer {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("192.168.100.137");
list.add("192.168.100.138");
list.add("192.168.100.139");
list.add("192.168.100.140");
list.add("192.168.100.142");
Map<String, String> map = new HashMap<String, String>();
map = PingServer.ping(list);
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
}
public static Map ping(List<String> ipList) {
String result = null;
Map<String, String> map = new HashMap<String, String>();
for (String ip : ipList) {
if (ip != null && !"".equals(ip)) {
try {
result = hostReachable(ip);
if(result.equals("false")){
String msg=" 不可达";
map.put(ip, msg);
}else{
map.put(ip, result);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return map;
}
private static String hostReachable(String ip) throws UnsupportedEncodingException {//主机ip可达
if(ip==null){
return "false";
}
StringBuffer sb = new StringBuffer();
Runtime run = Runtime.getRuntime();
Process process = null;
InputStream is = null;
try {
Properties props = System.getProperties();
String os = props.getProperty("os.name");
// windows
if (os.startsWith("W")) {
process = run.exec("ping " + ip + " -n 3 -w 8000");//ping3次 如果8000毫秒后没有收到结果为不通
// linux
} else {
if (ip.indexOf(":") < 0) {//没有“:” pingipv4 有":" pingipv6
process = run.exec("ping " + ip + " -c 3 -w 8000");
} else {
process = run.exec("ping6 " + ip + " -c 3 -w 8000");
}
}
is = process.getInputStream();
int t = 0;
while ((t = is.read()) != -1) {
sb.append((char) t);
}
} catch (IOException ex) {
return "false";
} finally {
try {
if (is != null) {
is.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
return "false";
}
}
String resultUpperCase = sb.toString().toUpperCase();//stringbuffer 转换string类型并大写
int tag = resultUpperCase.indexOf("TTL");//查找第一个 字符所在的位置
int ms = resultUpperCase.indexOf("MS")+2;
String res = null;
if (resultUpperCase.getBytes().length != resultUpperCase.length()) {
String result = new String(sb.toString().getBytes("iso8859-1"),"gb2312");
//如果通 返回相应时间
// System.out.println("resultUpperCase:"+result.trim());//打印ping结果
if (tag >= 0) {
res = result.substring(result.indexOf("时间")+2,result.indexOf("ms")+2);//相应时间
// System.out.println("相应时间中文:"+res);
}
}else{
if (tag >= 0) {
res = resultUpperCase.substring(resultUpperCase.indexOf("TIME")+4,resultUpperCase.indexOf("MS")+2);//相应时间
// System.out.println("相应时间英文:"+res);
}
}
if (tag == -1) {//没有找到
return "false";
} else {
//log("ok");
return res;
}
}
public static boolean isChinese(String str){//判断是否为中文
boolean temp = false;
Pattern p=Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher m=p.matcher(str);
if(m.find()){
temp = true;
}
return temp;
}
}