【Container App】在容器中抓取網(wǎng)絡包的方法
問題描述
在上兩篇博文中,我們介紹了Azure Container App容器應用中來測試與外部接口的連通性。通過curl 和 nc 命令發(fā)送請求以及長ping端口。
但是,如果遇見必須抓取網(wǎng)絡包的情況下,我們有什么好辦法呢?
本文就將分享一種Container App中安裝tcpdump后,抓取網(wǎng)絡包,并且通過storage account獲取到網(wǎng)絡包文件。
問題解答
話不多說,直接展示操作步驟。
第一步:安裝了tcpdump
PS: 本次實驗中,使用 tdnf install tcpdump 進行安裝。因不同的linux基礎鏡像需要的安裝命令有差別,可根據(jù)情況自主選擇安裝方式
第二步:執(zhí)行 tcpdump 開啟抓取網(wǎng)絡包
Tips:
- 在抓取之前,可以通過 tcpdump -D 列出所有的網(wǎng)卡
- 然后使用tcpdump -i eth0 監(jiān)測是否有流量經(jīng)過 eth0 網(wǎng)卡,如果無,根據(jù)上一步列表中切換網(wǎng)卡名
- 使用dst host 和 port來過濾網(wǎng)絡包,以避免抓取的流量包過大
示例命令:
# List all the NIC( network interface card) tcpdump -D # Check the real traffic tcpdump -i eth0 # filter by dst host & port tcpdump -i eth0 dst host <your host> and port 443 -w port-443-$RANDOM.cap # filter by dst port tcpdump -i eth0 dst port 443 -w port-443-$RANDOM.cap #all traffic tcpdump -i eth0 -w port-443-$RANDOM.cap
注意:在抓取中復現(xiàn)問題后或抓取到滿足要求的時間段后,通過 Ctrl + C 停止抓包命令!

第三步:使用 crul 調(diào)用 Storage Account PUT請求上傳網(wǎng)絡包
命令模板:
curl -X PUT -k -T <file name> -H "x-ms-blob-type: BlockBlob" "https://[storage_account_name].blob.core.chinacloudapi.cn/[container_name]/[blob_name]?[sas_token]"
Tips:
- <file name> : 第二步中保存的網(wǎng)絡包文件名,本例中的文件名是:port-443-22290.cap
- [storage_account_name], [container_name],[blob_name],[sas_token]:均是需要從Azure Storage account中獲取, blob_name 為上傳后的文件名。

實驗成功!
## 獲取Storage Account SAS URL截圖

參考資料
當在復雜的環(huán)境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 云中,恰是如此!

浙公網(wǎng)安備 33010602011771號