我想知道如何列出我的系统上使用Java打开的所有TCP连接。 我正在使用CentOS。
我也不知道从哪里开始。 任何指针都会有帮助。
提前致谢
感谢提示我必须做这样的事情
Q)为当前正在侦听的所有tcp端口标识任何新build立的连接
并继续每5秒轮询一次。 当不再有任何已build立的连接时,该脚本应该终止。
public class TCPConnections { public HashSet<Integer> establishedConnections = new HashSet<Integer>(); public HashSet<Integer> listeningConnections = new HashSet<Integer>(); public static void main(String[] args) { // TODO Auto-generated method stub TCPConnections tcpConnections = new TCPConnections(); try{ do{ tcpConnections.getListeningConnections(); Thread.sleep(5000); tcpConnections.getEstablishedConnections(); }while(!tcpConnections.establishedConnections.isEmpty()); } catch(Exception ex){ ex.printStackTrace(); } } public void getEstablishedConnections(){ String netstat = new String(); try { String line; establishedConnections = new HashSet<Integer>(); String[] cmd = { "/bin/sh", "-c", "netstat -atn | grep -w tcp | grep ESTABLISHED" }; java.lang.Process p = Runtime.getRuntime().exec(cmd); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { String[] portNo = line.split("\\s+"); if(portNo[3] != null && !portNo[3].equalsIgnoreCase(" ")){ String str = portNo[3].split(":")[1]; if( str != null && str.matches("[0-9]+")){ establishedConnections.add(Integer.parseInt(str)); if(listeningConnections.contains(Integer.parseInt(str))){listeningConnections.remove(Integer.parseInt(str)); System.out.println(" New connection established on port : "+Integer.parseInt(str)); } } } netstat = netstat + " \n" + line; } System.out.println(netstat); input.close(); } catch (Exception err) { err.printStackTrace(); } } public void getListeningConnections(){ String netstat = new String(); try { String line; listeningConnections = new HashSet<Integer>(); String[] cmd = { "/bin/sh", "-c", "netstat -atn | grep -w tcp | grep LISTEN" }; java.lang.Process p = Runtime.getRuntime().exec(cmd); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { String[] portNo = line.split("\\s+"); if(portNo[3] != null && !portNo[3].equalsIgnoreCase(" ")){ String str = portNo[3].split(":")[1]; if( str != null && str.matches("[0-9]+")){ listeningConnections.add(Integer.parseInt(str)); } } netstat = netstat + " \n" + line; } System.out.println(netstat); input.close(); } catch (Exception err) { err.printStackTrace(); } } }
我面临的问题是几个端口总是处于已build立状态,而less数端口总是处于Listen状态,因此do-while循环将永远运行。 请帮我解决这个问题。
Java似乎没有任何内容支持这一点,这并不奇怪,因为类似netstat
的功能依赖于操作系统。
你有两个选择:
1)解析netstat
的输出:
$ netstat -tn Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.1.140:48352 74.125.225.134:80 ESTABLISHED
2)解析/proc/net/tcp
(和tcp6
, udp
, udp6
, unix
,如果你在意的话):
$ cat /proc/net/tcp sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode 0: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 17120 1 ffff8800797c4700 100 0 0 10 0 1: 0100007F:0019 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 14821 1 ffff8800797c4000 100 0 0 10 0 2: 8C01A8C0:BCE0 86E17D4A:0050 01 00000000:00000000 00:00000000 00000000 1000 0 20164 1 ffff8800797c4e00 24 0 0 10 -1
这可能会更令人生畏,但会是首选的方法,因为它不依赖于netstat
存在(并在您的PATH
等)
您可以使用SNMP获取TCPConnTable
或TCPConnectionTable
,无论哪个支持。
有一个名为NetSNMP的Java SNMP库,毫无疑问也是其他的。