你應(yīng)該看看api process 返回的是流,按照輸出流的方法操作即可.公司上不去外網(wǎng),有代理才行,所以沒(méi)有ping 百度,ping的本機(jī).
閔行網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)從2013年開(kāi)始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CallCmd {
public static void main(String[] args) {
BufferedReader br = null;
try {
Process p = Runtime.getRuntime().exec("ping 127.0.0.1");
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb=new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
jpcap你要自己下好相應(yīng)的包和配置,不知道的就在網(wǎng)上搜吧··
import java點(diǎn)虐 .InetAddress;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.packet.EthernetPacket;
import jpcap.packet.ICMPPacket;
import jpcap.packet.IPPacket;
public class JPing {
private NetworkInterface[] devices = JpcapCaptor.getDeviceList();
private JpcapSender sender;
private JpcapCaptor jpcap;
private ICMPPacket icmpPacket;
private ListString listResult = new ArrayListString();
/**
* 組織ICMP報(bào)文發(fā)送,并開(kāi)啟線程接收?qǐng)?bào)文
* @param ip
*/
public void ping(String ip) {
try {
jpcap = JpcapCaptor.openDevice(devices[0], 200, false, 20);
sender = jpcap.getJpcapSenderInstance();
jpcap.setFilter("icmp", true);// 過(guò)濾器,只接受ICMP報(bào)文
icmpPacket = new ICMPPacket();
icmpPacket.type = ICMPPacket.ICMP_ECHO; // 發(fā)送回顯請(qǐng)求報(bào)文
icmpPacket.setIPv4Parameter(0, false, false, false, 0, false,
false, false, 0, 1010101, 100, IPPacket.IPPROTO_ICMP,
devices[0].addresses[1].address, InetAddress.getByName(ip));
// 隨意的32bytes數(shù)據(jù)
icmpPacket.data = "abcdefghijklmnopqrstuvwxyzabcdef".getBytes();
EthernetPacket ethernetPacket = new EthernetPacket();
ethernetPacket.frametype = EthernetPacket.ETHERTYPE_IP;
ethernetPacket.src_mac = devices[0].mac_address;
// 廣播地址
ethernetPacket.dst_mac = new byte[] { (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
icmpPacket.datalink = ethernetPacket;
listResult.add("Pinging " + icmpPacket.dst_ip + " with "
+ icmpPacket.data.length + " bytes of data: ");
startCapThread(jpcap);
for (int i = 0; i 5; i++) {
icmpPacket.sec = 0;
//icmpPacket.usec = System.currentTimeMillis();
icmpPacket.usec = new GregorianCalendar().getTimeInMillis();// 記錄發(fā)送時(shí)間
icmpPacket.seq = (short) (1000 + i);
icmpPacket.id = (short) (999 + i);
sender.sendPacket(icmpPacket);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 接收ICMP報(bào)文
* @param jpcap
*/
public void getIcmpPacket(JpcapCaptor jpcapCaptor) {
try {
while (true) {
long tmp = 0;
String tmpStr = null;
ICMPPacket rp;
rp = (ICMPPacket) jpcapCaptor.getPacket();
if ((rp != null) (rp.seq - rp.id == 1)
(rp.type == ICMPPacket.ICMP_ECHOREPLY)) {// 若是ICMP回應(yīng)報(bào)文,則列出。。。
tmp = (rp.sec * 1000 + rp.usec / 1000 - icmpPacket.sec
* 1000 - icmpPacket.usec); // 計(jì)算發(fā)送與接受的時(shí)間差
if (tmp = 0)
tmpStr = " 1 ms ";
else
tmpStr = "= " + tmp + " ms ";
System.out.println("Reply from "
+ rp.src_ip.getHostAddress() + ": bytes = "
+ rp.data.length + " time " + tmpStr + "TTL = "
+ rp.hop_limit);
listResult.add("Reply from " + rp.src_ip.getHostAddress()
+ ": bytes = " + rp.data.length + " time " + tmpStr
+ "TTL = " + rp.hop_limit);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 接收ICMP報(bào)文
* @param jpcap
*/
public void startCapThread(final JpcapCaptor jpcap) {
Runnable runner = new Runnable() {
public void run() {
getIcmpPacket(jpcap);
}
};
new Thread(runner).start();
}
public static void main(String[] args) {
new JPing().ping("");
}
}
可以用InetAddress的isReachable方法:
import java點(diǎn)虐 .InetAddress;
public class MainTest {
public static void main(String[] args) {
try {
int timeOut = 3000;
byte[] ip = new byte[] { (byte) 192, (byte) 168, (byte) 100, (byte) 151 };
int retry = 4;
InetAddress address = InetAddress.getByAddress(ip);
for (int i = 0; i retry; i++) {
if (address.isReachable(timeOut)) {
System.out.println(i + " OK");
} else {
System.out.println(i + " LOSS");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文題目:java代碼實(shí)現(xiàn)ping JAVA代碼實(shí)現(xiàn)計(jì)算天數(shù)差,不足一天,取兩位小數(shù)
文章鏈接:http://www.rwnh.cn/article4/ddcpiie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、建站公司、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)