/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.sigar.cmd;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.NetConnection;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.Tcp;
/**
* Display network connections.
*/
public class Netstat extends SigarCommandBase {
private static final int LADDR_LEN = 20;
private static final int RADDR_LEN = 35;
private static final String[] HEADER = new String[] {
"Proto",
"Local Address",
"Foreign Address",
"State",
""
};
private static boolean isNumeric, wantPid, isStat;
public Netstat(Shell shell) {
super(shell);
}
public Netstat() {
super();
}
protected boolean validateArgs(String[] args) {
return true;
}
public String getUsageShort() {
return "Display network connections";
}
//poor mans getopt.
public static int getFlags(String[] args, int flags) {
int proto_flags = 0;
isNumeric = false;
wantPid = false;
isStat = false;
for (int i=0; i<args.length; i++) {
String arg = args[i];
int j = 0;
while (j<arg.length()) {
switch (arg.charAt(j++)) {
case '-':
continue;
case 'l':
flags &= ~NetFlags.CONN_CLIENT;
flags |= NetFlags.CONN_SERVER;
break;
case 'a':
flags |= NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT;
break;
case 'n':
isNumeric = true;
break;
case 'p':
wantPid = true;
break;
case 's':
isStat = true;
break;
case 't':
proto_flags |= NetFlags.CONN_TCP;
break;
case 'u':
proto_flags |= NetFlags.CONN_UDP;
break;
case 'w':
proto_flags |= NetFlags.CONN_RAW;
break;
case 'x':
proto_flags |= NetFlags.CONN_UNIX;
break;
default:
System.err.println("unknown option");
}
}
}
if (proto_flags != 0) {
flags &= ~NetFlags.CONN_PROTOCOLS;
flags |= proto_flags;
}
return flags;
}
private String formatPort(int proto, long port) {
if (port == 0) {
return "*";
}
if (!isNumeric) {
String service = this.sigar.getNetServicesName(proto, port);
if (service != null) {
return service;
}
}
return String.valueOf(port);
}
private String formatAddress(int proto, String ip,
long portnum, int max) {
String port = formatPort(proto, portnum);
String address;
if (NetFlags.isAnyAddress(ip)) {
address = "*";
}
else if (isNumeric) {
address = ip;
}
else {
try {
address = InetAddress.getByName(ip).getHostName();
} catch (UnknownHostException e) {
address = ip;
}
}
max -= port.length() + 1;
if (address.length() > max) {
address = address.substring(0, max);
}
return address + ":" + port;
}
private void outputTcpStats() throws SigarException {
Tcp stat = this.sigar.getTcp();
final String dnt = " ";
println(dnt + stat.getActiveOpens() + " active connections openings");
println(dnt + stat.getPassiveOpens() + " passive connection openings");
println(dnt + stat.getAttemptFails() + " failed connection attempts");
println(dnt + stat.getEstabResets() + " connection resets received");
println(dnt + stat.getCurrEstab() + " connections established");
println(dnt + stat.getInSegs() + " segments received");
println(dnt + stat.getOutSegs() + " segments send out");
println(dnt + stat.getRetransSegs() + " segments retransmited");
println(dnt + stat.getInErrs() + " bad segments received.");
println(dnt + stat.getOutRsts() + " resets sent");
}
private void outputStats(int flags) throws SigarException {
if ((flags & NetFlags.CONN_TCP) != 0) {
println("Tcp:");
try {
outputTcpStats();
} catch (SigarException e) {
println(" " + e);
}
}
}
//XXX currently weak sauce. should end up like netstat command.
public void output(String[] args) throws SigarException {
//default
int flags = NetFlags.CONN_CLIENT | NetFlags.CONN_PROTOCOLS;
if (args.length > 0) {
flags = getFlags(args, flags);
if (isStat) {
outputStats(flags);
return;
}
}
NetConnection[] connections = this.sigar.getNetConnectionList(flags);
printf(HEADER);
for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i];
String proto = conn.getTypeString();
String state;
if (conn.getType() == NetFlags.CONN_UDP) {
state = "";
}
else {
state = conn.getStateString();
}
ArrayList items = new ArrayList();
items.add(proto);
items.add(formatAddress(conn.getType(),
conn.getLocalAddress(),
conn.getLocalPort(),
LADDR_LEN));
items.add(formatAddress(conn.getType(),
conn.getRemoteAddress(),
conn.getRemotePort(),
RADDR_LEN));
items.add(state);
String process = null;
if (wantPid &&
//XXX only works w/ listen ports
(conn.getState() == NetFlags.TCP_LISTEN))
{
try {
long pid =
this.sigar.getProcPort(conn.getType(),
conn.getLocalPort());
if (pid != 0) { //XXX another bug
String name =
this.sigar.getProcState(pid).getName();
process = pid + "/" + name;
}
} catch (SigarException e) {
}
}
if (process == null) {
process = "";
}
items.add(process);
printf(items);
}
}
public static void main(String[] args) throws Exception {
new Netstat().processCommand(args);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
hyperic-sigar-1.6.2.zip 0积分下载
共296个文件
html:187个
java:38个
so:13个
5星 · 超过95%的资源 需积分: 34 61 下载量 187 浏览量
2016-04-26
10:58:18
上传
评论
收藏 3.18MB ZIP 举报
温馨提示
系统信息收集和报表工具。 一个开源的工具,提供了跨平台的系统信息收集的API,通过它可以获取包括: 1.操作系统的信息,包括:dataModel、cpuEndian、name、version、
资源推荐
资源详情
资源评论
收起资源包目录
hyperic-sigar-1.6.2.zip 0积分下载 (296个子文件)
default.build 1KB
rbsigar.c 13KB
_sigar.c 8KB
php_sigar.c 4KB
ChangeLog 19KB
COPYING 18KB
Sigar.cs 20KB
Ifconfig.cs 3KB
Df.cs 1KB
Free.cs 734B
CpuInfo.cs 592B
stylesheet.css 1KB
sigar-amd64-winnt.dll 384KB
sigar-x86-winnt.dll 252KB
libsigar-universal64-macosx.dylib 380KB
libsigar-universal-macosx.dylib 369KB
EXCEPTIONS 4KB
inherit.gif 57B
sigar.h 23KB
sigar_fileinfo.h 6KB
sigar_log.h 2KB
sigar_format.h 2KB
sigar_ptql.h 2KB
php_sigar.h 96B
index-all.html 479KB
Sigar.html 114KB
constant-values.html 73KB
SigarProxy.html 70KB
serialized-form.html 69KB
VM.html 69KB
ShellBase.html 42KB
ServiceConfig.html 39KB
NetFlags.html 36KB
Service.html 34KB
FileInfo.html 33KB
overview-tree.html 32KB
PrintfFormat.html 32KB
SigarCommandBase.html 31KB
Pdh.html 30KB
RegistryKey.html 27KB
Shell.html 26KB
EventLog.html 26KB
MetaBase.html 26KB
Ps.html 25KB
Iostat.html 24KB
NetInterfaceStat.html 24KB
ResourceLimit.html 24KB
NfsServerV3.html 24KB
NfsClientV3.html 24KB
FileSystemUsage.html 23KB
ShellCommandBase.html 23KB
NetStat.html 23KB
Df.html 23KB
ProcState.html 22KB
Ifconfig.html 22KB
OperatingSystem.html 22KB
ProcModuleInfo.html 21KB
ProcFileInfo.html 21KB
NfsServerV2.html 21KB
NfsClientV2.html 21KB
ProcInfo.html 21KB
ShowArgs.html 21KB
ShowEnv.html 21KB
MultiPs.html 21KB
Kill.html 21KB
NetInterfaceConfig.html 21KB
FileVersionInfo.html 21KB
Win32Service.html 20KB
Netstat.html 20KB
FileSystem.html 20KB
Time.html 20KB
LocaleInfo.html 20KB
NetRoute.html 20KB
Uptime.html 20KB
allclasses-frame.html 20KB
ReferenceMap.html 20KB
FileWatcher.html 20KB
package-summary.html 20KB
Pidof.html 20KB
Ulimit.html 20KB
CpuTimer.html 20KB
NetConnection.html 20KB
Du.html 20KB
Ls.html 20KB
CpuInfo.html 19KB
MultiwordShellCommand.html 19KB
Version.html 19KB
ShellCommand_source.html 19KB
Nfsstat.html 19KB
ShellCommand_alias.html 19KB
SysInfo.html 19KB
NetInfo.html 19KB
Free.html 19KB
Route.html 19KB
Who.html 19KB
Cpu.html 19KB
ShellCommand_set.html 19KB
Mem.html 18KB
Tcp.html 18KB
ProcMem.html 18KB
共 296 条
- 1
- 2
- 3
资源评论
- 龙扬天2018-05-05sigar-1.6.2.zipsigar-1.6.2.zipsigar-1.6.2.zip
- 沐卿枫2019-09-15可以的,可以使用
xht1351549230
- 粉丝: 16
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功