Reputation: 55
I need to count repeated words from different String objects stored in Array List...
I tried the following Code:
import java.io.*;
import java.util.*;
public class Runcmd {
public static void main(String[] args) {
ArrayList arrList=new ArrayList();
try
{
Process pr=Runtime.getRuntime().exec("netstat -an");
BufferedReader rd= new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=rd.readLine();
while(line!=null)
{
arrList.add(line);
line=rd.readLine();
}
Iterator it=arrList.iterator();
while(it.hasNext())
{
String sent=(String)it.next();
System.out.println(sent);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
And I get this output:
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49152 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49153 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49154 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49155 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49156 0.0.0.0:0 LISTENING
TCP 10.50.170.205:139 0.0.0.0:0 LISTENING
TCP 10.50.170.205:49904 74.125.236.86:443 ESTABLISHED
TCP 10.50.170.205:50044 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50045 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50046 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50047 23.201.102.42:80 TIME_WAIT
TCP 10.50.170.205:50048 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50049 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50050 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50051 23.57.214.140:80 TIME_WAIT
TCP 10.50.170.205:50052 23.201.102.33:80 TIME_WAIT
TCP 10.50.170.205:50053 23.57.214.140:80 TIME_WAIT
TCP [::]:135 [::]:0 LISTENING
TCP [::]:445 [::]:0 LISTENING
TCP [::]:3306 [::]:0 LISTENING
TCP [::]:49152 [::]:0 LISTENING
TCP [::]:49153 [::]:0 LISTENING
TCP [::]:49154 [::]:0 LISTENING
TCP [::]:49155 [::]:0 LISTENING
TCP [::]:49156 [::]:0 LISTENING
UDP 0.0.0.0:5355 *:*
UDP 10.50.170.205:137 *:*
UDP 10.50.170.205:138 *:*
UDP 10.50.170.205:1900 *:*
UDP 127.0.0.1:1900 *:*
UDP 127.0.0.1:49397 *:*
UDP [::1]:1900 *:*
UDP [::1]:49396 *:*
Now I want to get the list of words in number of Occurrence(LISTENING, TIME-WAIT, ESTABLISHED) For Example:
LISTENING : 17
TIME-WAIT : 10
ESTABLISHED : 1
Please tell me the idea to get like above....
Upvotes: 1
Views: 286
Reputation: 691
You just need to have a running count of the three categories, then print these out at the end.
try
{
Process pr=Runtime.getRuntime().exec("netstat -an");
BufferedReader rd= new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=rd.readLine();
while(line!=null)
{
arrList.add(line);
if(line.contains("LISTENING")) listeningCount++;
if(line.contains("TIME_WAIT")) waitingCount++;
if(line.contains("ESTABLISHED")) establishedCount++;
line=rd.readLine();
}
Iterator it=arrList.iterator();
while(it.hasNext())
{
String sent=(String)it.next();
System.out.println(sent);
}
System.out.println("LISTENING = " + listeningCount);
System.out.println("TIME_WAIT = " + waitingCount);
System.out.println("ESTABLISHED = " + establishedCount);
}
catch(IOException e)
{
e.printStackTrace();
}
Upvotes: 0
Reputation: 476709
On the risk of downvoting, you can do this way easier using bash
:
#!/bin/bash
res=$(netstat -an)
n=$(echo "$res" | grep 'LISTENING' | wc -l)
echo "LISTENING : $n"
n=$(echo "$res" | grep 'TIME-WAIT' | wc -l)
echo "TIME-WAIT : $n"
n=$(echo "$res" | grep 'ESTABLISHED' | wc -l)
echo "ESTABLISHED : $n"
Or even more compact (and flexible):
#!/bin/bash
res=$(netstat -an)
for tp in {LISTENING,TIME-WAIT,ESTABLISHED}
do
n=$(echo "$res" | grep -c "$tp")
echo "$tp : $n"
done
Upvotes: 1
Reputation: 26961
Define some count variables and check the string when printing it...
while(it.hasNext())
{
String sent=(String)it.next();
System.out.println(sent);
if (sent.indexOf("LISTENING") > 0) listening ++;
if (sent.indexOf("TIME-WAIT") > 0) wait++;
if (sent.indexOf("ESTABLISHED") > 0) established ++;
}
Upvotes: 5