使用 PowerShell 使用 Azure 網路監看員 管理虛擬機的封包擷取

網路監看員 封包擷取工具可讓您建立擷取會話,以記錄來自 Azure 虛擬機(VM) 的網路流量。 系統會為擷取工作階段提供篩選器,以確保您只會擷取到您想要的流量。 封包擷取有助於主動和主動診斷網路異常。 其應用程式超越異常偵測範圍,包括收集網路統計數據、取得網路入侵的見解、偵錯客戶端-伺服器通訊,以及解決各種其他網路挑戰。 網路監看員 封包擷取可讓您從遠端起始封包擷取,以減輕在特定虛擬機上手動執行的需求。

在本文中,您將瞭解如何使用 Azure PowerShell 從遠端設定、啟動、停止、下載及刪除虛擬機封包擷取。 若要瞭解如何使用 Azure 入口網站 或 Azure CLI 管理封包擷取,請參閱使用 Azure 入口網站 管理虛擬機的封包擷取或使用 Azure CLI 管理虛擬機的封包擷取。

必要條件

  • 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶

  • Azure Cloud Shell 或 Azure PowerShell。

    本文中的步驟會在 Azure Cloud Shell 中以互動方式執行 Azure PowerShell Cmdlet。 若要在 Cloud Shell 中執行命令,請選取程式代碼區塊右上角的 [ 開啟 Cloud Shell ]。 選取 [複製] 以複製程式碼,然後將其貼入 Cloud Shell 以執行。 您也可以從 Azure 入口網站內執行 Cloud Shell。

    您也可以在本機安裝 Azure PowerShell 來執行 Cmdlet。 本文需要 Az PowerShell 模組。 如需詳細資訊,請參閱 如何安裝 Azure PowerShell。 若要尋找已安裝的版本,請執行 Get-InstalledModule -Name Az。 如果您在本機執行 PowerShell,請使用 Connect-AzAccount Cmdlet 登入 Azure。

  • 具有下列輸出 TCP 連線的虛擬機:

    • 透過埠 443 對記憶體帳戶
    • 至 169.254.169.254,透過埠 80
    • 至 168.63.129.16 透過埠 8037

注意

  • 如果未針對該區域啟用 網路監看員,Azure 會在虛擬機區域中建立 網路監看員 實例。 如需詳細資訊,請參閱啟用或停用 Azure 網路監看員
  • 網路監看員 封包擷取需要 網路監看員 代理程式 VM 擴充功能才能安裝在目標虛擬機上。 如需詳細資訊,請參閱安裝 網路監看員 代理程式
  • 必要條件中列出的最後兩個IP位址和埠在所有使用 網路監看員 代理程式且偶爾可能會變更的 網路監看員 工具中都是常見的。

如果網路安全組與網路介面或網路介面所屬的子網相關聯,請確定規則存在,以允許透過先前的埠進行輸出連線。 同樣地,在將使用者定義的路由新增至您的網路時,請確定透過先前埠的輸出連線能力。

安裝 網路監看員 代理程式

若要使用封包擷取,網路監看員 代理程式虛擬機擴充功能必須安裝在虛擬機上。

使用 Get-AzVMExtension Cmdlet 來檢查擴充功能是否已安裝在虛擬機上:

# List the installed extensions on the virtual machine.
Get-AzVMExtension -VMName 'myVM' -ResourceGroupName 'myResourceGroup' | format-table Name, Publisher, ExtensionType, EnableAutomaticUpgrade 

如果擴充功能安裝在虛擬機上,則您可以看到它列在上述命令的輸出中:

Name                         Publisher                      ExtensionType            EnableAutomaticUpgrade
----                         ---------                      -------------            ----------------------
AzureNetworkWatcherExtension Microsoft.Azure.NetworkWatcher NetworkWatcherAgentLinux                   True

如果未安裝擴充功能,請使用 Set-AzVMExtension Cmdlet 來安裝它:

# Install Network Watcher agent on a Linux virtual machine.
Set-AzVMExtension -Publisher 'Microsoft.Azure.NetworkWatcher' -ExtensionType 'NetworkWatcherAgentLinux' -Name 'AzureNetworkWatcherExtension' -VMName 'myVM' -ResourceGroupName 'myResourceGroup' -TypeHandlerVersion '1.4' -EnableAutomaticUpgrade 1 
# Install Network Watcher agent on a Windows virtual machine.
Set-AzVMExtension -Publisher 'Microsoft.Azure.NetworkWatcher' -ExtensionType 'NetworkWatcherAgentWindows' -Name 'AzureNetworkWatcherExtension' -VMName 'myVM' -ResourceGroupName 'myResourceGroup' -TypeHandlerVersion '1.4' -EnableAutomaticUpgrade 1 

成功安裝延伸模組之後,您會看到下列輸出:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK 

啟動封包擷取

若要啟動擷取會話,請使用 New-AzNetworkWatcherPacketCapture Cmdlet:

# Place the virtual machine configuration into a variable.
$vm = Get-AzVM -ResourceGroupName 'myResourceGroup' -Name 'myVM'

# Place the storage account configuration into a variable.
$storageAccount = Get-AzStorageAccount -ResourceGroupName 'myResourceGroup' -Name 'mystorageaccount'

# Start the Network Watcher capture session.
New-AzNetworkWatcherPacketCapture -Location 'eastus' -PacketCaptureName 'myVM_1' -TargetVirtualMachineId $vm.Id  -StorageAccountId $storageAccount.Id 

啟動擷取工作階段之後,您會看到下列輸出:

ProvisioningState Name   BytesToCapturePerPacket TotalBytesPerSession TimeLimitInSeconds
----------------- ----   ----------------------- -------------------- ------------------
Succeeded         myVM_1 0                       1073741824           18000

下表描述您可以搭配 New-AzNetworkWatcherPacketCapture Cmdlet 使用的選擇性參數:

參數 description
-Filter 新增 filter(s) 以只擷取您想要的流量。 例如,您只能擷取特定IP位址到特定埠的TCP流量。
-TimeLimitInSeconds 設定擷取會話的最大持續時間。 默認值為18000秒(5小時)。
-BytesToCapturePerPacket 設定每個封包要擷取的最大位元元組數目。 如果未使用或輸入 0,則會擷取所有位元組。
-TotalBytesPerSession 設定擷取的位元組總數。 到達該值之後,封包擷取就會停止。 如果未使用,最多可擷取 1 GB(1,073,741,824 個字節)。
-LocalFilePath 如果您想要將擷取儲存在目標虛擬機中,請輸入有效的本機檔案路徑(例如 C:\Capture\myVM_1.cap)。 如果您使用 Linux 機器,路徑必須以 /var/captures 開頭。

停止封包擷取

使用 Stop-AzNetworkWatcherPacketCapture Cmdlet 手動停止執行中的封包擷取會話。

# Manually stop a packet capture session.
Stop-AzNetworkWatcherPacketCapture -Location 'eastus' -PacketCaptureName 'myVM_1'

注意

不論在目前執行的擷取會話或已停止的會話上執行,Cmdlet 都不會傳回回應。

取得封包擷取

使用 Get-AzNetworkWatcherPacketCapture Cmdlet 來擷取封包擷取的狀態(執行中或已完成)。

# Get information, properties, and status of a packet capture.
Get-AzNetworkWatcherPacketCapture -Location 'eastus' -PacketCaptureName 'myVM_1'

下列輸出是 Cmdlet 輸出 Get-AzNetworkWatcherPacketCapture 的範例。 下列範例是在擷取完成之後。 PacketCaptureStatus 值為 Stopped,具有 TimeExceeded 的 StopReason。 此值顯示封包擷取成功並執行其時間。

ProvisioningState Name   Target                                                                                                                              BytesToCapturePerPacket TotalBytesPerSession TimeLimitInSeconds
----------------- ----   ------                                                                                                                              ----------------------- -------------------- ------------------
Succeeded         myVM_1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM 0                       1073741824           18000

注意

若要在輸出中取得更多詳細數據,請將 新增 | Format-List 至命令結尾。

下載封包擷取

結束封包擷取會話之後,產生的擷取檔案會儲存至 Azure 記憶體、目標虛擬機上的本機檔案或兩者。 封包擷取的儲存目的地會在建立封包期間指定。 如需詳細資訊,請參閱 啟動封包擷取

如果指定記憶體帳戶,擷取檔案會儲存至記憶體帳戶,路徑如下:

https://{storageAccountName}.blob.core.windows.net/network-watcher-logs/subscriptions/{subscriptionId}/resourcegroups/{storageAccountResourceGroup}/providers/microsoft.compute/virtualmachines/{virtualMachineName}/{year}/{month}/{day}/packetcapture_{UTCcreationTime}.cap

若要下載儲存至 Azure 記憶體的封包擷取檔案,請使用 Get-Az 儲存體 BlobContent Cmdlet:

# Download the packet capture file from Azure storage container.
Get-AzStorageBlobContent -Container 'network-watcher-logs' -Blob 'subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/myresourcegroup/providers/microsoft.compute/virtualmachines/myvm/2024/01/25/packetcapture_22_44_54_342.cap' -Destination 'C:\Capture\myVM_1.cap'

注意

您也可以使用 Azure 儲存體 Explorer,從記憶體帳戶容器下載擷取檔案。 儲存體總管 是一個獨立應用程式,可讓您輕鬆地用來存取和使用 Azure 儲存體 數據。 如需詳細資訊,請參閱開始使用儲存體總管

刪除封包擷取

# Remove a packet capture resource.
Remove-AzNetworkWatcherPacketCapture -Location 'eastus' -PacketCaptureName 'myVM_1'

重要

刪除 網路監看員 中的封包擷取並不會從記憶體帳戶或虛擬機中刪除擷取檔案。 如果您不再需要擷取檔案,您必須從記憶體帳戶手動將其刪除,以避免產生記憶體成本。