工具類之----用於識別指定的機器是否可用工具類

某銀行現場環境有這麼一個情況,就是網絡不互通,但是個別的機器某一個是可以通的,那麼其他的資源設備如果進行操作呢?留意了一個工具類。如下

package com.simp.util.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import com.simp.env.ShellEnv;
import com.simp.util.jsp.Parameter;
import com.simp.util.process.ProcessTool;

/**
 * 用於識別指定的機器是否可用工具類
 * @author mc
 *
 */
public class NetToolkit {

	/**
	 * 識別指定機器的網絡狀態與端口狀態的工具類
	 * @param ip
	 * @return
	 */
	public static boolean ping(String ip) {
		boolean rv = ping(ip, 3, 3000);
		
		if (!rv) {
			rv = pingByRuntime(ip); 
		}
		
		return rv;
	}

	/**
	 * 識別指定的機器是否能夠ping通,需要提供機器ip、超時時間和嘗試次數
	 * 但防火牆和服務器配置可能阻塞請求,使其在某些特定的端口可以訪問時處於不可到達狀態。如果可以獲得權限,則典型實現將使用 ICMP ECHO REQUEST;否則它將試圖在目標主機的端口 7 (Echo) 上建立 TCP 連接。 
	 * 
	 * @param ip
	 * @param retry
	 * @param timeOut
	 * @return
	 */
	public  static boolean ping(String ip , int retry , int timeOut) {
		
		boolean flag = false ;
		InetAddress address = null ;
		try {
			
			address = InetAddress.getByName(ip) ;
			for(int i = 0 ; i < retry ; i++) {
				if(address.isReachable(timeOut)) {
					flag = true ;
					break ;
				}			
			}
		
		} catch(UnknownHostException e) {
		} catch (IOException e) {
		}
		
		return flag ;
	}

	/**
	 * 使用ping命令 確定IP或域名是否可達 前提根據防火牆等相關配置
	 * @param addrs
	 * @return
	 */
	public static boolean pingByRuntime(String addrs) {
		Runtime runtime = Runtime.getRuntime(); // 獲取當前程序的運行進對象
		Process process = null; // 聲明處理類對象
		String line = null; // 返回行信息
		InputStream is = null; // 輸入流
		InputStreamReader isr = null; // 字節流
		BufferedReader br = null;
		boolean res = false;// 結果
		try {
			process = runtime.exec("ping " + addrs); // PING
			is = process.getInputStream(); // 實例化輸入流
			isr = new InputStreamReader(is);// 把輸入流轉換成字節流
			br = new BufferedReader(isr);// 從字節中讀取文本
			while ((line = br.readLine()) != null) {
				if (line.contains("TTL")) {
					res = true;
					break;
				}
			}
		} catch (IOException e) {
			runtime.exit(1);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
				}
			}
			
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
				}
			}
			
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
		
		return res;
	}

	/**
	 * 識別指定機器的端口是否開啓
	 * @param ip
	 * @param port
	 * @return
	 */
	public static boolean checkPort(String ip , int port) {
		return checkPort(ip , port , 3000) ;
	}
	
	/**
	 * 識別指定機器的端口是否開啓
	 * @param ip
	 * @param port
	 * @param timeOut
	 * @return
	 */
	public static boolean checkPort(String ip , int port , int timeOut) {
		
		Socket soket = new Socket();
		boolean flag = false ;
		
		try {
			soket.connect(new InetSocketAddress(ip, port), timeOut);
			
			if(soket.isConnected()) {
				flag = true ;
				
			}
			
		} catch (IOException e) {
		} finally {
			if(soket.isConnected()) {
				try {
					soket.close() ;
				} catch (IOException e) {
				}
			}
			
		}
		
		return flag ;
	}
	
	
	/**
	 * 識別指定機器的端口是否開啓 (udp)
	 * @param ip
	 * @param port
	 * @param timeOut
	 * @return
	 */
	public static boolean check_udp_port(String ip , int port) {
		
		if(!Parameter.isIp(ip)){ //zhanghe 190315 安全檢查ip格式
		    System.out.println("check_udp_port ip format err:" + ip);
			return false;
		}
		
		if(!Parameter.isNum(String.valueOf(port))){ //zhanghe 190315 安全檢查port格式
		    System.out.println("check_udp_port port format err:" + ip);
			return false;
		}
		
		String cmd = ShellEnv.SHELL_UDP + " " + ip + " " + port;
		
		return ProcessTool.exec(cmd, 50, true);
		
	}
	
}

這個工具類只是在配置了NAT連接池ip(就是保存了一系列ip,查看這些ip哪個和目標ip是通的),進行查找然後返回這個ip的工具類後續在整理如何進行項目進行不通時其它項目操作資源

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章