根据你的目标平台,修改"bytes from"即可
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.InetAddress
import java.net.UnknownHostException
/**
* PingDomain.java
*
* @author 判断输入的域名是否有效
*/
public class PingDomain {
public void isDomain(String addressArr) throws IOException {
boolean flag=false
InetAddress address = null
try {
address = InetAddress.getByName(addressArr)
System.out.println("address = " + address)
} catch (UnknownHostException e) {
System.out.println("输入非法!")
return
}
System.out.println("是一个域名吗?"+isReachable(address.getHostAddress()))
System.out.println("---------------------------------------")
}
private boolean isReachable(String ip) {
Runtime r = Runtime.getRuntime()
int timeout = 2
String pingCommand = "ping " + ip + " -w " + timeout
BufferedReader in = null
try {
Process p = r.exec(pingCommand)
if (p == null) {
System.out.println("Failed.")
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()))
String line = null
while ( (line = in.readLine()) != null) {
System.out.println(line)
if (line.contains("bytes from")) {
System.out.println("Conected.")
p.destroy()
return true
}
}
} catch (Exception ex) {
System.out.println("Failed.")
} finally {
try {
in.close()
} catch (Exception ex) {
}
}
return false
}
public static void main(String[] args) throws Exception {
PingDomain pmd = new PingDomain()
BufferedReader in=new BufferedReader(new InputStreamReader(System.in))
String inputline=in.readLine()
pmd.isDomain(inputline)
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)