我有一些 netstat 输出正在编写脚本并想要解析。从下面的输出中解析值的最有效方法是什么?需要指出的是,我使用的是旧/bin/sh
shell,而不是bash
我正在从命令生成此输出:netstat -d -K O30S1O1
MVS TCP/IP NETSTAT CS V1R11 TCPIP Name: NCDTCP 10:12:39
DevName: OSAQO130 DevType: MPCIPA
DevStatus: Ready CfgRouter: Non ActRouter: Non
LnkName: O30S1O1 LnkType: IPAQENET LnkStatus: Ready
Speed: 0000001000
IpBroadcastCapability: No
ArpOffload: Yes ArpOffloadInfo: Yes
ActMtu: 8992
VLANid: None VLANpriority: Disabled
ReadStorage: GLOBAL (4096K) InbPerf: Balanced
ChecksumOffload: Yes SegmentationOffload: Yes
SecClass: 255 MonSysplex: No
Routing Parameters:
MTU Size: 1500 Metric: 10
DestAddr: 0.0.0.0 SubnetMask: 255.255.255.192
Multicast Specific:
Multicast Capability: Yes
Group RefCnt SrcFltMd
----- ------ --------
224.0.0.5 0000000001 Exclude
SrcAddr: None
224.0.0.1 0000000001 Exclude
SrcAddr: None
Link Statistics:
BytesIn = 13545386
Inbound Packets = 129630
Inbound Packets In Error = 0
Inbound Packets Discarded = 0
Inbound Packets With No Protocol = 0
BytesOut = 81424019
Outbound Packets = 520188
Outbound Packets In Error = 0
Outbound Packets Discarded = 0
IPv4 LAN Group Summary
LanGroup: 00006
Name Status ArpOwner VipaOwner
---- ------ -------- ---------
O30S1O1 Active O30S1O1 Yes
例如,假设我想获取多个值,例如DevName
、BytesIn
和BytesOut
。我计划每分钟左右在四个接口上运行 netstat 命令,并将输出写入 csv 文件以导入到 Excel。
答案1
无论你用什么解析它,我建议以这种格式输出:
DevName=OSAQO130
BytesIn=13545386
因为这样您就可以将脚本包含在其中eval $(...)
并准备好使用变量$DevName
和$BytesIn
。
解析perl
:
netstat -d -K O30S1O1 | perl -ne '$DevName = $1 if m/DevName: (\S+)/; $BytesIn = $1 if m/BytesIn.*= (\d+)/; END {print map("$_=${$_}\n", qw/DevName BytesIn/)}'
解析sed
:
netstat -d -K O30S1O1 | sed -ne 's/DevName: \([^ ]*\).*/DevName=\1/ p' -e 's/.*BytesIn.*= \([0-9]*\)/BytesIn=\1/ p'
在Windows中我使用bash
自带的shell git
,它有很多unix工具,包括sed
和perl
。