LoginSignup
2
3

More than 3 years have passed since last update.

Windows OS で追加した固定ルートの指定NICを調べる方法

Last updated at Posted at 2020-12-27

はじめに

複数NICをもつ Windows OS では、スタティックルートを設定する際にはNICを指定することが推奨されるらしい。
route add コマンドを IF オプションなしで実行した時の挙動について

しかし、route print では固定ルートのIf設定が確認できなかった。
PowerShellを利用することで、固定ルートのIf指定を確認することができたため、メモを残す。
本記事では、Windows OS におけるNICを指定したスタティックルートの設定方法と、設定した固定ルートの確認方法を記載する。

固定ルートの設定/確認コマンド

Get-NetRoute PSコマンドで -PolicyStore PersistentStore を利用することで、固定ルートについてのIf設定を確認することができる。

# 追加前の固定ルートを確認する
Get-NetRoute -AddressFamily IPv4 -PolicyStore PersistentStore

# インターフェイスIndexを確認する
Get-NetIPAddress
Get-NetIPInterface

# インターフェイスインデックスを指定して固定ルートを追加する
New-NetRoute -DestinationPrefix <宛先IPアドレス/xx> -InterfaceIndex "IF index" -NextHop <Gateway IP>
  # 例: New-NetRoute -DestinationPrefix 10.0.1.0/24 -InterfaceIndex "5" -NextHop 192.168.0.1

# 追加後の固定ルートを確認する
Get-NetRoute -AddressFamily IPv4 -PolicyStore PersistentStore

# ルーティングを削除する場合は以下
Remove-NetRoute <宛先IPアドレス/xx> -InterfaceIndex "IF index" -NextHop <Gateway IP>
  # 例: Remove-NetRoute 10.0.1.0/24 -InterfaceIndex "5" -NextHop 192.168.0.1

コマンド実施例

以下に、コマンド実施例を示す。
実施環境: Windows Server 2019 Standard
追加ルート: 宛先IPアドレス=10.0.1.0/24, NextHop=192.168.0.1

image.png

PS C:\Users\azureadmin> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.1432
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.1432
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Users\azureadmin>
PS C:\Users\azureadmin> Get-NetRoute -AddressFamily IPv4 -PolicyStore PersistentStore
Get-NetRoute : No MSFT_NetRoute objects found with property 'AddressFamily' equal to 'IPv4'.  Verify the value of the property and retry.
At line:1 char:1
+ Get-NetRoute -AddressFamily IPv4 -PolicyStore PersistentStore
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (IPv4:AddressFamily) [Get-NetRoute], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound_AddressFamily,Get-NetRoute

PS C:\Users\azureadmin>
PS C:\Users\azureadmin> Get-NetIPInterface

ifIndex InterfaceAlias                  AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp     ConnectionState PolicyStore
------- --------------                  ------------- ------------ --------------- ----     --------------- -----------
5       Ethernet                        IPv6                  1500              10 Enabled  Connected       ActiveStore
1       Loopback Pseudo-Interface 1     IPv6            4294967295              75 Disabled Connected       ActiveStore
5       Ethernet                        IPv4                  1500              10 Enabled  Connected       ActiveStore
1       Loopback Pseudo-Interface 1     IPv4            4294967295              75 Disabled Connected       ActiveStore


PS C:\Users\azureadmin>
PS C:\Users\azureadmin> New-NetRoute -DestinationPrefix 10.0.1.0/24 -InterfaceIndex "5" -NextHop 192.168.0.1

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
5       10.0.1.0/24                                    192.168.0.1                                      256 10       ActiveStore
5       10.0.1.0/24                                    192.168.0.1                                      256          Persiste...


PS C:\Users\azureadmin>
PS C:\Users\azureadmin> Get-NetRoute -AddressFamily IPv4 -PolicyStore PersistentStore

ifIndex DestinationPrefix                              NextHop                                  RouteMetric ifMetric PolicyStore
------- -----------------                              -------                                  ----------- -------- -----------
5       10.0.1.0/24                                    192.168.0.1                                      256          Persiste...


PS C:\Users\azureadmin>
PS C:\Users\azureadmin> Remove-NetRoute 10.0.1.0/24 -InterfaceIndex "5" -NextHop 192.168.0.1

Confirm
Are you sure you want to perform this action?
Performing operation "Remove" on Target "NetRoute -DestinationPrefix 10.0.1.0/24 -InterfaceIndex 5 -NextHop 192.168.0.1 -Store Active"
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

Confirm
Are you sure you want to perform this action?
Performing operation "Remove" on Target "NetRoute -DestinationPrefix 10.0.1.0/24 -InterfaceIndex 5 -NextHop 192.168.0.1 -Store Persistent"
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
PS C:\Users\azureadmin>
PS C:\Users\azureadmin> Get-NetRoute -AddressFamily IPv4 -PolicyStore PersistentStore
Get-NetRoute : No MSFT_NetRoute objects found with property 'AddressFamily' equal to 'IPv4'.  Verify the value of the property and retry.
At line:1 char:1
+ Get-NetRoute -AddressFamily IPv4 -PolicyStore PersistentStore
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (IPv4:AddressFamily) [Get-NetRoute], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound_AddressFamily,Get-NetRoute

PS C:\Users\azureadmin>

参考URL

以上

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3