// Home | Go Back //
package agent.ifmib;
import java.io.*;
import java.util.*;
import java.net.*;
import com.ireasoning.protocol.snmp.SnmpOctetString;
public class Util
{
private static ArrayList _netifs;
static HashMap _stateMap = new HashMap();
static
{
_stateMap.put("LISTEN", "2");
_stateMap.put("SYN_SENT", "3");
_stateMap.put("SYN_RECEIVED", "4");
_stateMap.put("ESTABLISHED", "5");
_stateMap.put("FIN_WAIT1", "6");
_stateMap.put("FIN_WAIT_1", "6");
_stateMap.put("FIN_WAIT2", "7");
_stateMap.put("FIN_WAIT_2", "7");
_stateMap.put("CLOSE_WAIT", "8");
_stateMap.put("LAST_ACK", "9");
_stateMap.put("CLOSING", "10");
_stateMap.put("TIME_WAIT", "11");
_stateMap.put("DELETE_TCB", "12");
}
public static String[] tokenize(String text, String delim)
{
StringTokenizer st = new StringTokenizer(text, delim);
int size = st.countTokens() ;
String[] ret = new String[size];
int i = 0;
while (st.hasMoreTokens())
{
ret[i++] = st.nextToken();
}
return ret;
}
public synchronized static ArrayList netstat()
{
try
{
String cmd = "netstat -p TCP -n";
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader (new InputStreamReader (proc.getInputStream() ));
String s;
String srcIp, destIp, state;
int srcPort, destPort, stateCode;
ArrayList conns = new ArrayList();
while ((s = br.readLine ())!= null)
{
s = s.trim();
if(s.startsWith("TCP"))
{
String[] sections = tokenize(s, " ");
if(sections.length != 4) continue;
String [] fields = tokenize(sections[1], ":");
srcIp = fields[0];
srcPort = Integer.parseInt(fields[1]);
fields = tokenize(sections[2], ":");
destIp = fields[0];
destPort = Integer.parseInt(fields[1]);
state = sections[3];
conns.add(new Connection(srcIp, srcPort, destIp, destPort, Integer.parseInt((String)_stateMap.get(state)) ));
}
}
return conns;
}
catch(IOException e)
{
System.out.println( e );
}
return null;
}
public static ArrayList ipconfig()
{
if(_netifs != null)
{
return _netifs;
}
try
{
String cmd = "ipconfig /all";
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader (new InputStreamReader (proc.getInputStream() ));
String s;
String srcIp, destIp, state;
int srcPort, destPort, stateCode;
ArrayList netifs = new ArrayList();
NetInterface netif = new NetInterface();
netifs.add(netif);
netif.ipAddress = "0.0.0.0";
netif.descr = "MS TCP Loopback interface";
int num = 0;
while ((s = br.readLine ())!= null)
{
num ++;
s = s.trim();
if(s.endsWith(":") && ! s.endsWith(". :"))
{
netif = new NetInterface();
netifs.add(netif);
continue;
}
int pos = s.indexOf(".");
if(pos < 0) continue;
String name = s.substring(0, pos).trim();
pos = s.indexOf(":", pos);
String value = s.substring(pos + 1);
if(name.equals("Description"))
{
netif.descr = value;
}
else if(name.equals("Subnet Mask"))
{
netif.mask = value;
}
else if(name.equals("Physical Address"))
{
netif.physAddress = SnmpOctetString.convertPhysAddress(value);
}
else if(name.equals("IP Address"))
{
netif.ipAddress= value;
}
}
_netifs = netifs;
return netifs;
}
catch(IOException e)
{
System.out.println( e);
}
return null;
}
}
class Connection
{
public Connection(String srcIp, int srcPort, String destIp, int destPort, int state)
{
this.srcIp = srcIp;
this.srcPort = srcPort;
this.destIp = destIp;
this.destPort = destPort;
this.state = state;
}
String srcIp;
int srcPort;
String destIp;
int destPort;
int state;
}
class NetInterface
{
String physAddress = "";
String mask = "";
String descr = "";
String ipAddress = "127.0.0.1";
public String toString()
{
return "physAddress=" + physAddress + "\n" +
"mask=" + mask + "\n" +
"descr=" + descr + "\n" +
"ipAddress=" + ipAddress ;
}
}