PDA

View Full Version : Send a ping to multiple ip adresses


Gastonnn
May 6, 2014, 01:29 AM
hey,i made a java program wish takes a list of ip adresses from an XML file and then send to each one of them a ping in order to take out the value of time from each one.now the problem is with the output(i want to print write the values of time in a form of a matrix in a text file) here is some of my code and i would be glad if someone helps me out:



public void actionPerformed(ActionEvent evt) {
private boolean stop = false; // start or stop the ping
stop = false;
try {
final Formatter x = new Formatter("C:/Users/VAIO/workspace/tcc/gastoon/kkk.txt");
PrintWriter writer;
writer = new PrintWriter("C:/Users/VAIO/workspace/tcc/gastoon/kkk.txt");

for (int m = 0; m < 10; m++) {
if (stop) break;



DocumentBuilderFactory BuilderFactory=DocumentBuilderFactory.newInstance( );
DocumentBuilder db=BuilderFactory.newDocumentBuilder();
Document doc=db.parse("C:/Users/VAIO/workspace/tcc/gastoon/adresStore.xml");
doc.normalize();


NodeList rootNodes=doc.getElementsByTagName("connection");
Node rootNode=rootNodes.item(0);
Element rootElement=(Element) rootNode;

NodeList l=rootElement.getElementsByTagName("users");
Node users=l.item(0);
Element element=(Element) users;

NodeList nl=element.getElementsByTagName("user");
List<String> allIps = new ArrayList<String>();

for(int I=0;i<nl.getLength();i++){

Node user=nl.item(I);
Element ele=(Element) user;
String adrss=ele.getElementsByTagName("ipAdress").item(0).getTextContent();//+" -n 1";
allIps.add(I, adrss);

}
for(String n : allIps){

writer.print(allIps);

for(int I=0;i<allIps.size();i++)
{
String pingCmd = "ping " +allIps.get(I) +" -n 1";
String pingResult = "";
String str="";

try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);


BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
pingResult += inputLine;
}

String[] lines = pingResult.split("\n");

for (int k=0;k<lines.length;k++) {
String line=lines[k];

if ((line.contains("temps=") && (line.contains(allIps.get(I))))){
int index = line.indexOf("temps=");
String time = line.substring(index + "temps=".length(),line.indexOf("ms"));

writer.print(time);
writer.println("\n");

System.out.println(time);
}
}
}
}
catch (IOException ie) { System.out.println(ie);
}
}}
writer.flush();}}