Bash 计算属于GigabitEthernet102的所有接口的输入/输出速率/
我试图捕获GigabitEthernet102中接口的输入速率和输出速率之和。。我有一些逻辑,但想帮助转换成一个合适的程序来实现这一点 输入Bash 计算属于GigabitEthernet102的所有接口的输入/输出速率/,bash,awk,Bash,Awk,我试图捕获GigabitEthernet102中接口的输入速率和输出速率之和。。我有一些逻辑,但想帮助转换成一个合适的程序来实现这一点 输入 GigabitEthernet102/0/0/1 is up, line protocol is up Interface state transitions: 5 Hardware is GigabitEthernet/IEEE 802.3 interface(s), address is sets.sets.sets Description
GigabitEthernet102/0/0/1 is up, line protocol is up
Interface state transitions: 5
Hardware is GigabitEthernet/IEEE 802.3 interface(s), address is sets.sets.sets
Description: xxx
Internet address is x.x.x.x
--More-- MTU 1514 bytes, BW 100000 Kbit (Max: 100000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
Full-duplex, 100Mb/s, 1000BASE-T, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 100 msec, Carrier delay (down) is 100 msec
loopback not set,
Last link flapped 10w3d
ARP type ARPA, ARP timeout 04:00:00
Last input 00:00:00, output 00:00:00
Last clearing of "show interface" counters never
30 second input rate 0 bits/sec, 0 packets/sec
30 second output rate 0 bits/sec, 0 packets/sec
5638310 packets input, 467600021 bytes, 27 total input drops
0 drops for unrecognized upper-level protocol
Received 4 broadcast packets, 186012 multicast packets
1 runts, 0 giants, 0 throttles, 0 parity
2 input errors, 1 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
4336700 packets output, 306662485 bytes, 0 total output drops
Output 44 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
GigabitEthernet102/0/0/2 is administratively down, line protocol is administratively down
Interface state transitions: 0
Hardware is GigabitEthernet/IEEE 802.3 interface(s), address is sets.sets.sets
Internet address is Unknown
MTU 1514 bytes, BW 100000 Kbit (Max: 100000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
--More-- Full-duplex, 100Mb/s, 1000BASE-T, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 100 msec, Carrier delay (down) is 100 msec
loopback not set,
Last input never, output never
Last clearing of "show interface" counters never
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
0 packets input, 0 bytes, 0 total input drops
0 drops for unrecognized upper-level protocol
Received 0 broadcast packets, 0 multicast packets
0 runts, 0 giants, 0 throttles, 0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
0 packets output, 0 bytes, 0 total output drops
Output 0 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
TenGigE0/2/0/3 is up, line protocol is up
Interface state transitions: 3
Hardware is TenGigE, address is sets.sets.sets
Layer 1 Transport Mode is LAN
--More-- Description:
Internet address is x.x.x.x
MTU 1536 bytes, BW 10000000 Kbit (Max: 10000000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
Full-duplex, 10000Mb/s, LR, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 10 msec
loopback not set,
Last link flapped 18w3d
ARP type ARPA, ARP timeout 04:00:00
Last input 00:00:00, output 00:00:00
Last clearing of "show interface" counters 10w1d
5 minute input rate 0 bits/sec, 1 packets/sec
5 minute output rate 0 bits/sec, 1 packets/sec
3907990 packets input, 341219535 bytes, 0 total input drops
10323 drops for unrecognized upper-level protocol
Received 0 broadcast packets, 218445 multicast packets
0 runts, 0 giants, 0 throttles, 0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
6795781 packets output, 490559886 bytes, 0 total output drops
Output 0 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
GigabitEthernet102/0/0/3 is up, line protocol is up
Interface state transitions: 1
Hardware is GigabitEthernet/IEEE 802.3 interface(s), address is x.x.x.x
Description: xxx
Internet address is x.x.x.x
MTU 1514 bytes, BW 100000 Kbit (Max: 100000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
Full-duplex, 100Mb/s, 1000BASE-T, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 100 msec, Carrier delay (down) is 100 msec
loopback not set,
--More-- Last link flapped 18w3d
ARP type ARPA, ARP timeout 04:00:00
Last input 00:00:00, output 00:00:00
Last clearing of "show interface" counters never
5 minute input rate 2000 bits/sec, 2 packets/sec
5 minute output rate 1000 bits/sec, 2 packets/sec
23543445 packets input, 2623268477 bytes, 66 total input drops
18599 drops for unrecognized upper-level protocol
Received 37519 broadcast packets, 225636 multicast packets
0 runts, 0 giants, 0 throttles, 0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
24806600 packets output, 2739397694 bytes, 0 total output drops
Output 3 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
Total input traffic: x bits
Total output traffic: x bits
建议的节目流程
1> 捕获启动并具有GigabitEthernet102/[0-9]{3}语法的接口
千兆以太网102/0/0/1
千兆以太网102/0/0/3
2> 使用这些接口(可能是正向查找?)再次搜索文件以捕获输入速率。对输出速率也做同样的事情
3> 使用数组执行输入/输出速率之和
所需输出
GigabitEthernet102/0/0/1 is up, line protocol is up
Interface state transitions: 5
Hardware is GigabitEthernet/IEEE 802.3 interface(s), address is sets.sets.sets
Description: xxx
Internet address is x.x.x.x
--More-- MTU 1514 bytes, BW 100000 Kbit (Max: 100000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
Full-duplex, 100Mb/s, 1000BASE-T, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 100 msec, Carrier delay (down) is 100 msec
loopback not set,
Last link flapped 10w3d
ARP type ARPA, ARP timeout 04:00:00
Last input 00:00:00, output 00:00:00
Last clearing of "show interface" counters never
30 second input rate 0 bits/sec, 0 packets/sec
30 second output rate 0 bits/sec, 0 packets/sec
5638310 packets input, 467600021 bytes, 27 total input drops
0 drops for unrecognized upper-level protocol
Received 4 broadcast packets, 186012 multicast packets
1 runts, 0 giants, 0 throttles, 0 parity
2 input errors, 1 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
4336700 packets output, 306662485 bytes, 0 total output drops
Output 44 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
GigabitEthernet102/0/0/2 is administratively down, line protocol is administratively down
Interface state transitions: 0
Hardware is GigabitEthernet/IEEE 802.3 interface(s), address is sets.sets.sets
Internet address is Unknown
MTU 1514 bytes, BW 100000 Kbit (Max: 100000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
--More-- Full-duplex, 100Mb/s, 1000BASE-T, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 100 msec, Carrier delay (down) is 100 msec
loopback not set,
Last input never, output never
Last clearing of "show interface" counters never
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
0 packets input, 0 bytes, 0 total input drops
0 drops for unrecognized upper-level protocol
Received 0 broadcast packets, 0 multicast packets
0 runts, 0 giants, 0 throttles, 0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
0 packets output, 0 bytes, 0 total output drops
Output 0 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
TenGigE0/2/0/3 is up, line protocol is up
Interface state transitions: 3
Hardware is TenGigE, address is sets.sets.sets
Layer 1 Transport Mode is LAN
--More-- Description:
Internet address is x.x.x.x
MTU 1536 bytes, BW 10000000 Kbit (Max: 10000000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
Full-duplex, 10000Mb/s, LR, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 10 msec
loopback not set,
Last link flapped 18w3d
ARP type ARPA, ARP timeout 04:00:00
Last input 00:00:00, output 00:00:00
Last clearing of "show interface" counters 10w1d
5 minute input rate 0 bits/sec, 1 packets/sec
5 minute output rate 0 bits/sec, 1 packets/sec
3907990 packets input, 341219535 bytes, 0 total input drops
10323 drops for unrecognized upper-level protocol
Received 0 broadcast packets, 218445 multicast packets
0 runts, 0 giants, 0 throttles, 0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
6795781 packets output, 490559886 bytes, 0 total output drops
Output 0 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
GigabitEthernet102/0/0/3 is up, line protocol is up
Interface state transitions: 1
Hardware is GigabitEthernet/IEEE 802.3 interface(s), address is x.x.x.x
Description: xxx
Internet address is x.x.x.x
MTU 1514 bytes, BW 100000 Kbit (Max: 100000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
Full-duplex, 100Mb/s, 1000BASE-T, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 100 msec, Carrier delay (down) is 100 msec
loopback not set,
--More-- Last link flapped 18w3d
ARP type ARPA, ARP timeout 04:00:00
Last input 00:00:00, output 00:00:00
Last clearing of "show interface" counters never
5 minute input rate 2000 bits/sec, 2 packets/sec
5 minute output rate 1000 bits/sec, 2 packets/sec
23543445 packets input, 2623268477 bytes, 66 total input drops
18599 drops for unrecognized upper-level protocol
Received 37519 broadcast packets, 225636 multicast packets
0 runts, 0 giants, 0 throttles, 0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
24806600 packets output, 2739397694 bytes, 0 total output drops
Output 3 broadcast packets, 0 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
Total input traffic: x bits
Total output traffic: x bits
尝试
1> awk '/^GigabitEthernet102/ && !/down/' sat_int_acc.txt
GigabitEthernet102/0/0/1 is up, line protocol is up
GigabitEthernet102/0/0/3 is up, line protocol is up
GigabitEthernet102/0/0/4 is up, line protocol is up
...
2> awk '/^GigabitEthernet102/ && !/down/ {intf[$1]} END {for(i in intf) print i}' sat_int_acc.txt
GigabitEthernet102/0/0/30
GigabitEthernet102/0/0/21
GigabitEthernet102/0/0/12
GigabitEthernet102/0/0/40
...
awk '/^GigabitEthernet102/ && !/down/; /^GigabitEthernet102/,/input rate/' sat_int_acc.txt
谢谢。看起来我可能已经找到了解决方案,但它不是纯awk
$ awk '/GigabitEthernet102.* is up/,/input rate/' sat_int_acc.txt | grep "input rate" | awk '{x+=$5}END {print x}'
43237000看起来我可能已经找到了解决方案,但它不是纯awk
$ awk '/GigabitEthernet102.* is up/,/input rate/' sat_int_acc.txt | grep "input rate" | awk '{x+=$5}END {print x}'
43237000根据您对正则表达式范围的尝试,以下不确定输入和输出速率。它从以
GigabitEthernet102
开头的一行读取一个块,并包含向上的,直到一个空行或文件结尾。在每个块中,它查找输入速率
或输出速率
,并对速率求和
$ cat sat_int_acc.awk
/^GigabitEthernet102.* is up/,/^$/ {
if (/input rate/) input_total += $5
if (/output rate/) output_total += $5
}
END {
print "Total input traffic: " input_total " bits"
print "Total output traffic: " output_total " bits"
}
$ awk -f sat_int_acc.awk sat_int_acc.txt
Total input traffic: 2000 bits
Total output traffic: 1000 bits
根据您对正则表达式范围的尝试,下面列出了输入和输出速率。它从以GigabitEthernet102
开头的一行读取一个块,并包含向上的,直到一个空行或文件结尾。在每个块中,它查找输入速率
或输出速率
,并对速率求和
$ cat sat_int_acc.awk
/^GigabitEthernet102.* is up/,/^$/ {
if (/input rate/) input_total += $5
if (/output rate/) output_total += $5
}
END {
print "Total input traffic: " input_total " bits"
print "Total output traffic: " output_total " bits"
}
$ awk -f sat_int_acc.awk sat_int_acc.txt
Total input traffic: 2000 bits
Total output traffic: 1000 bits
这应该与一个awk
命令一起使用
awk '/protocol is/ {f=0} /GigabitEthernet102.* is up/ {f=1} f && /5 minute/ {sum+=$5} END {print sum}' file
3000
/protocol is/{f=0}
测试是否启动新接口块,并将标志f
设置为0
/GigabitEthernet102.*启动/{f=1}
如果找到正确的接口,请将标志f
设置为1
f&&/5分钟/{sum+=$5}
如果标志f\为真且行为
5分钟`
结束{print sum}
然后打印输入和输出的总和
注意:您不需要混合使用5分钟
和30秒
输入。这应该与一个awk
命令一起使用
awk '/protocol is/ {f=0} /GigabitEthernet102.* is up/ {f=1} f && /5 minute/ {sum+=$5} END {print sum}' file
3000
/protocol is/{f=0}
测试是否启动新接口块,并将标志f
设置为0
/GigabitEthernet102.*启动/{f=1}
如果找到正确的接口,请将标志f
设置为1
f&&/5分钟/{sum+=$5}
如果标志f\为真且行为
5分钟`
结束{print sum}
然后打印输入和输出的总和
注意:您不需要将5分钟
和30秒
输入混用。这太疯狂了。Awk可以做grep所能做的一切。你看,这太疯狂了。Awk可以做grep所能做的一切。另见