ifEntryのsnmpwalk結果をPowerShellで処理しテーブル形式で出力する

## 1. はじめに

SNMPエージェントのソフトウェアの開発に利用されるフリーのSNMPパッケージで、
代表的なものとしてはNet-SNMPがあります。

**http://net-snmp.sourceforge.net/**

この記事では、
Net-SNMPに付属しているsnmpwalkコマンドを使って、インタフェース情報が格納されている

**[ifEntry(.1.3.6.1.2.1.2.2.1)](https://cric.grenoble.cnrs.fr/Administrateurs/Outils/MIBS/?oid=.1.3.6.1.2.1.2.2.1)**

のsnmpwalk結果をPowerShellのスクリプトで処理し、テーブル形式での出力を得る処理の作成例を紹介します。

テーブル形式での出力結果は、下記のような形式です。

### 取得結果例１（テーブル形式）

```:PowerShell
ifIndex   ifDesc                        ifMtu  ifSpeed     ifAdminStatus ifOperStatus  ifLastChange                    ifInOctets    ifOutOctets
1         GigabitEthernet0/0            1500   1000000000  down(2)       down(2)       (78380142) 9 days, 1:43:21.42   0             0
2         Null0                         1500   4294967295  up(1)         up(1)         (78380328) 9 days, 1:43:23.28   0             0
3         unrouted VLAN 1               1500   4294967295  testing(3)    unknown(4)    (78364910) 9 days, 1:40:49.10
4         unrouted VLAN 1002            1500   4294967295  testing(3)    unknown(4)    (78364910) 9 days, 1:40:49.10
5         unrouted VLAN 1004            1500   4294967295  testing(3)    unknown(4)    (78364910) 9 days, 1:40:49.10
6         unrouted VLAN 1005            1500   4294967295  testing(3)    unknown(4)    (78364910) 9 days, 1:40:49.10
7         unrouted VLAN 1003            1500   4294967295  testing(3)    unknown(4)    (78364910) 9 days, 1:40:49.10
8         GigabitEthernet1/0/1          1500   1000000000  up(1)         up(1)         (78380330) 9 days, 1:43:23.30   1417965       1058025
9         GigabitEthernet1/0/2          1500   1000000000  up(1)         down(2)       (78380030) 9 days, 1:43:20.30   0             0
10        GigabitEthernet1/0/3          1500   1000000000  down(2)       down(2)       (78380050) 9 days, 1:43:20.50   0             0
11        GigabitEthernet1/0/4          1500   1000000000  down(2)       down(2)       (78380051) 9 days, 1:43:20.51   0             0
12        GigabitEthernet1/0/5          1500   1000000000  down(2)       down(2)       (78380051) 9 days, 1:43:20.51   0             0
13        GigabitEthernet1/0/6          1500   1000000000  down(2)       down(2)       (78380051) 9 days, 1:43:20.51   0             0
14        GigabitEthernet1/0/7          1500   1000000000  down(2)       down(2)       (78380051) 9 days, 1:43:20.51   0             0
15        GigabitEthernet1/0/8          1500   1000000000  down(2)       down(2)       (78380051) 9 days, 1:43:20.51   0             0
16        GigabitEthernet1/0/9          1500   1000000000  down(2)       down(2)       (78380051) 9 days, 1:43:20.51   0             0
17        GigabitEthernet1/0/10         1500   1000000000  down(2)       down(2)       (78380051) 9 days, 1:43:20.51   0             0
18        GigabitEthernet1/0/11         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
19        GigabitEthernet1/0/12         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
20        GigabitEthernet1/0/13         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
21        GigabitEthernet1/0/14         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
22        GigabitEthernet1/0/15         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
23        GigabitEthernet1/0/16         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
24        GigabitEthernet1/0/17         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
25        GigabitEthernet1/0/18         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
26        GigabitEthernet1/0/19         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
27        GigabitEthernet1/0/20         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
28        GigabitEthernet1/0/21         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
29        GigabitEthernet1/0/22         1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
30        GigabitEthernet1/0/23         1500   1000000000  up(1)         up(1)         (78380330) 9 days, 1:43:23.30   12246537      6416874
31        GigabitEthernet1/0/24         1500   1000000000  up(1)         up(1)         (78380330) 9 days, 1:43:23.30   701494        10123652
32        GigabitEthernet1/1/1          1500   1000000000  up(1)         up(1)         (78542457) 9 days, 2:10:24.57   2966021       966691
33        GigabitEthernet1/1/2          1500   1000000000  up(1)         up(1)         (78613821) 9 days, 2:22:18.21   1278151       1043005
34        GigabitEthernet1/1/3          1500   1000000000  down(2)       down(2)       (78380052) 9 days, 1:43:20.52   0             0
35        GigabitEthernet1/1/4          1500   1000000000  down(2)       down(2)       (78380053) 9 days, 1:43:20.53   0             0
```
### 取得結果例２（csv形式）

```:PowerShell
ifIndex;ifDesc;ifMtu;ifSpeed;ifAdminStatus;ifOperStatus;ifLastChange;ifInOctets;ifOutOctets
1;GigabitEthernet0/0;1500;1000000000;down(2);down(2);(78380142) 9 days, 1:43:21.42;0;0
2;Null0;1500;4294967295;up(1);up(1);(78380328) 9 days, 1:43:23.28;0;0
3;unrouted VLAN 1;1500;4294967295;testing(3);unknown(4);(78364910) 9 days, 1:40:49.10;;
4;unrouted VLAN 1002;1500;4294967295;testing(3);unknown(4);(78364910) 9 days, 1:40:49.10;;
5;unrouted VLAN 1004;1500;4294967295;testing(3);unknown(4);(78364910) 9 days, 1:40:49.10;;
6;unrouted VLAN 1005;1500;4294967295;testing(3);unknown(4);(78364910) 9 days, 1:40:49.10;;
7;unrouted VLAN 1003;1500;4294967295;testing(3);unknown(4);(78364910) 9 days, 1:40:49.10;;
8;GigabitEthernet1/0/1;1500;1000000000;up(1);up(1);(78380330) 9 days, 1:43:23.30;1417965;1058025
9;GigabitEthernet1/0/2;1500;1000000000;up(1);down(2);(78380030) 9 days, 1:43:20.30;0;0
10;GigabitEthernet1/0/3;1500;1000000000;down(2);down(2);(78380050) 9 days, 1:43:20.50;0;0
11;GigabitEthernet1/0/4;1500;1000000000;down(2);down(2);(78380051) 9 days, 1:43:20.51;0;0
12;GigabitEthernet1/0/5;1500;1000000000;down(2);down(2);(78380051) 9 days, 1:43:20.51;0;0
13;GigabitEthernet1/0/6;1500;1000000000;down(2);down(2);(78380051) 9 days, 1:43:20.51;0;0
14;GigabitEthernet1/0/7;1500;1000000000;down(2);down(2);(78380051) 9 days, 1:43:20.51;0;0
15;GigabitEthernet1/0/8;1500;1000000000;down(2);down(2);(78380051) 9 days, 1:43:20.51;0;0
16;GigabitEthernet1/0/9;1500;1000000000;down(2);down(2);(78380051) 9 days, 1:43:20.51;0;0
17;GigabitEthernet1/0/10;1500;1000000000;down(2);down(2);(78380051) 9 days, 1:43:20.51;0;0
18;GigabitEthernet1/0/11;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
19;GigabitEthernet1/0/12;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
20;GigabitEthernet1/0/13;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
21;GigabitEthernet1/0/14;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
22;GigabitEthernet1/0/15;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
23;GigabitEthernet1/0/16;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
24;GigabitEthernet1/0/17;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
25;GigabitEthernet1/0/18;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
26;GigabitEthernet1/0/19;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
27;GigabitEthernet1/0/20;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
28;GigabitEthernet1/0/21;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
29;GigabitEthernet1/0/22;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
30;GigabitEthernet1/0/23;1500;1000000000;up(1);up(1);(78380330) 9 days, 1:43:23.30;12246537;6416874
31;GigabitEthernet1/0/24;1500;1000000000;up(1);up(1);(78380330) 9 days, 1:43:23.30;701494;10123652
32;GigabitEthernet1/1/1;1500;1000000000;up(1);up(1);(78542457) 9 days, 2:10:24.57;2966021;966691
33;GigabitEthernet1/1/2;1500;1000000000;up(1);up(1);(78613821) 9 days, 2:22:18.21;1278151;1043005
34;GigabitEthernet1/1/3;1500;1000000000;down(2);down(2);(78380052) 9 days, 1:43:20.52;0;0
35;GigabitEthernet1/1/4;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
36;TenGigabitEthernet1/1/1;1500;4294967295;up(1);down(2);(78380030) 9 days, 1:43:20.30;0;0
37;TenGigabitEthernet1/1/2;1500;4294967295;up(1);down(2);(78380030) 9 days, 1:43:20.30;0;0
38;TenGigabitEthernet1/1/3;1500;4294967295;up(1);down(2);(78380031) 9 days, 1:43:20.31;0;0
39;TenGigabitEthernet1/1/4;1500;4294967295;up(1);down(2);(78380031) 9 days, 1:43:20.31;0;0
40;StackPort1;0;4294967295;up(1);up(1);(78364961) 9 days, 1:40:49.61;0;0
41;StackSub-St1-1;;4294967295;up(1);up(1);(78328198) 9 days, 1:34:41.98;;
42;StackSub-St1-2;;4294967295;up(1);up(1);(78328198) 9 days, 1:34:41.98;;
43;Vlan1;1500;1000000000;down(2);down(2);(78380047) 9 days, 1:43:20.47;201600;0
44;unrouted VLAN 10;1500;4294967295;testing(3);unknown(4);(78365771) 9 days, 1:40:57.71;;
45;GigabitEthernet2/0/1;1500;1000000000;up(1);up(1);(78410786) 9 days, 1:48:27.86;2603515;5336700
46;GigabitEthernet2/0/2;1500;1000000000;up(1);down(2);(78407801) 9 days, 1:47:58.01;0;0
47;GigabitEthernet2/0/3;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
48;GigabitEthernet2/0/4;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
49;GigabitEthernet2/0/5;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
50;GigabitEthernet2/0/6;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
51;GigabitEthernet2/0/7;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
52;GigabitEthernet2/0/8;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
53;GigabitEthernet2/0/9;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
54;GigabitEthernet2/0/10;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
55;GigabitEthernet2/0/11;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
56;GigabitEthernet2/0/12;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
57;GigabitEthernet2/0/13;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
58;GigabitEthernet2/0/14;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
59;GigabitEthernet2/0/15;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
60;GigabitEthernet2/0/16;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
61;GigabitEthernet2/0/17;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
62;GigabitEthernet2/0/18;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
63;GigabitEthernet2/0/19;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
64;GigabitEthernet2/0/20;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
65;GigabitEthernet2/0/21;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
66;GigabitEthernet2/0/22;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
67;GigabitEthernet2/0/23;1500;1000000000;up(1);up(1);(78410644) 9 days, 1:48:26.44;10535102;7893509
68;GigabitEthernet2/0/24;1500;1000000000;up(1);up(1);(78410629) 9 days, 1:48:26.29;553224;7754392
69;GigabitEthernet2/1/1;1500;1000000000;up(1);up(1);(78546477) 9 days, 2:11:04.77;1812006;5157963
70;GigabitEthernet2/1/2;1500;1000000000;up(1);up(1);(78617165) 9 days, 2:22:51.65;3052127;4740039
71;GigabitEthernet2/1/3;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
72;GigabitEthernet2/1/4;1500;1000000000;down(2);down(2);(78380053) 9 days, 1:43:20.53;0;0
73;TenGigabitEthernet2/1/1;1500;4294967295;up(1);down(2);(78407802) 9 days, 1:47:58.02;0;0
74;TenGigabitEthernet2/1/2;1500;4294967295;up(1);down(2);(78407802) 9 days, 1:47:58.02;0;0
75;TenGigabitEthernet2/1/3;1500;4294967295;up(1);down(2);(78407802) 9 days, 1:47:58.02;0;0
76;TenGigabitEthernet2/1/4;1500;4294967295;up(1);down(2);(78407802) 9 days, 1:47:58.02;0;0
77;StackPort2;0;4294967295;up(1);up(1);(78365003) 9 days, 1:40:50.03;0;0
78;StackSub-St2-1;;4294967295;up(1);up(1);(78328198) 9 days, 1:34:41.98;;
79;StackSub-St2-2;;4294967295;up(1);up(1);(78328198) 9 days, 1:34:41.98;;
80;unrouted VLAN 20;1500;4294967295;testing(3);unknown(4);(78365772) 9 days, 1:40:57.72;;
81;unrouted VLAN 100;1500;4294967295;testing(3);unknown(4);(78365772) 9 days, 1:40:57.72;;
82;unrouted VLAN 101;1500;4294967295;testing(3);unknown(4);(78365772) 9 days, 1:40:57.72;;
83;unrouted VLAN 111;1500;4294967295;testing(3);unknown(4);(78365772) 9 days, 1:40:57.72;;
84;unrouted VLAN 112;1500;4294967295;testing(3);unknown(4);(78365773) 9 days, 1:40:57.73;;
85;unrouted VLAN 113;1500;4294967295;testing(3);unknown(4);(78365773) 9 days, 1:40:57.73;;
86;unrouted VLAN 114;1500;4294967295;testing(3);unknown(4);(78365773) 9 days, 1:40:57.73;;
87;unrouted VLAN 130;1500;4294967295;testing(3);unknown(4);(78365773) 9 days, 1:40:57.73;;
88;unrouted VLAN 131;1500;4294967295;testing(3);unknown(4);(78365774) 9 days, 1:40:57.74;;
89;unrouted VLAN 132;1500;4294967295;testing(3);unknown(4);(78365774) 9 days, 1:40:57.74;;
90;Port-channel3;1500;2000000000;up(1);up(1);(78380332) 9 days, 1:43:23.32;4021480;6394725
91;Port-channel10;1500;2000000000;up(1);up(1);(78542656) 9 days, 2:10:26.56;4778027;6124830
92;Port-channel11;1500;2000000000;up(1);up(1);(78614023) 9 days, 2:22:20.23;4330278;5783224
93;Loopback0;1514;4294967295;up(1);up(1);(78380232) 9 days, 1:43:22.32;0;0
94;VLAN100;1500;1000000000;up(1);up(1);(78380328) 9 days, 1:43:23.28;11658876;7529161
95;VLAN101;1500;1000000000;down(2);down(2);(78380049) 9 days, 1:43:20.49;695092;0
96;VLAN130;1500;1000000000;down(2);down(2);(78380049) 9 days, 1:43:20.49;0;0
97;VLAN131;1500;1000000000;down(2);down(2);(78380050) 9 days, 1:43:20.50;0;0
98;VLAN132;1500;1000000000;down(2);down(2);(78380050) 9 days, 1:43:20.50;0;0
99;Vlan10;1500;1000000000;down(2);down(2);(78380048) 9 days, 1:43:20.48;0;0
100;Vlan20;1500;1000000000;down(2);down(2);(78380048) 9 days, 1:43:20.48;0;0
101;VLAN111;1500;1000000000;up(1);up(1);(78380329) 9 days, 1:43:23.29;11480668;5189904
102;VLAN112;1500;1000000000;up(1);up(1);(78412644) 9 days, 1:48:46.44;10421554;7022076
103;VLAN113;1500;1000000000;up(1);up(1);(78380329) 9 days, 1:43:23.29;530866;8901227
104;VLAN114;1500;1000000000;up(1);up(1);(78412629) 9 days, 1:48:46.29;436260;6942811
```

### 完成例

スクリプト内部の処理の説明は割愛させていただきますが、
PowerShellスクリプトの完成例は以下に配置しています。

* ifEntry版
**https://github.com/NobuyukiInoue/PS_convert_ifEntry**

* ipCidrRouteTable版
**https://github.com/NobuyukiInoue/PS_convert_ipCidrRouteTable**

ちなみに、GUI版のほとんどのSNMPマネージャ製品では、簡単にメニュー等から選択するだけで表形式でみることができます。

しかし、SNMPマネージャのデバッグ時や、障害が発生していてSNMPマネージャが利用できない場合などは、
直接、対象のNW機器に対してsnmpwalkしたいときがありますよね。

## 2. 実行例

実行する場合は、以下のような書式でsnmpwalkの実行結果を、PowerShellスクリプトに渡します。

### 書式
```:PowerShell
snmpwalk -On -v 2c -c <community> <target_ip> .1.3.6.1.2.1.2.2.1 | ./convert_ifEntry.ps1 [IFS(delimiter)]
```

### 実行例１（テーブル形式）
```:PowerShell
snmpwalk -On -v 2c -c <community> <target_ip> .1.3.6.1.2.1.2.2.1 | ./convert_ifEntry.ps1
```

### 実行例２（コンマ区切り（csv形式））
```:PowerShell
snmpwalk -On -v 2c -c <community> <target_ip> .1.3.6.1.2.1.2.2.1 | ./convert_ifEntry.ps1 ";"
```

### 実行例３（タブ区切り）
```:PowerShell
snmpwalk -On -v 2c -c <community> <target_ip> .1.3.6.1.2.1.2.2.1 | ./convert_ifEntry.ps1 "\t"
```

毎回、OIDを指定してsnmpwalkを実行するのが面倒なのであれば、
下記のようなスクリプト内でsnmpwalkとPS_convert_ifEntryを実行すると良いでしょう。

```PowerShell:main.ps1
param($target, $community)

if (-Not($target)) {
    Write-Host "Usage :"$MyInvocation.MyCommand.Name"<target> <community>"
    exit
}

if (-Not($community)) {
    $community = "public"
}

$OID = ".1.3.6.1.2.1.2.2.1"
$walkResult = snmpwalk -On -v 2c -c $community $target $OID

if ($walkResult -eq $NULL) {
    Write-Output $walkResult
    exit
}

$walkResult | ./PS_convert_ifEntry.ps1
```

## 3. 関連記事

**[ipCidrRouteTableのsnmpwalk結果をPowerShellで処理しテーブル形式で出力する](https://qiita.com/gx3n-inue/items/f1071a279ee792963cd3)**