【Azure 應用服務】App Service For Linux 如何在 Web 應用實例上住抓取網絡日志


問題描述

在App Service For Windows的環境中,我們可以通過ArmClient 工具發送POST請求在Web應用的實例中抓取網絡日志,但是在App Service For Linux的環境中如何抓取網絡包呢?

抓取Windows的網絡包可參考博文:【應用服務 App Service】App Service中抓取網絡日志

 

問題解決

通過SSH方式登錄到Linux實例,使用tcpdump的工具抓取網絡包, 通過IP地址和端口443來進行過濾,生成的網絡包寫入到tmp目錄下的 appnetworktrace.pcap  文件。 命令如下:

tcpdump -i any host <your app service inbound ip address> and tcp port 443 -n -v -s 0 -w /tmp/appnetworktrace.pcap   

  • 如果在登錄SSH的時候出現 SSH CONNECTION CLOSE - Error: Timed out while waiting for handshakeError: connect ECONNREFUSED 錯誤,則是因為使用自定義容器的方式發布的Docker鏡像中沒有啟動SSH。只需要根據官方文檔啟動即可。

啟用 SSH

SSH 實現容器和客戶端之間的安全通信。 為了使自定義容器支持 SSH,你必須將其添加到 Docker 映像本身。

1) 將 sshd_config 文件 添加到存儲庫,如以下示例中所示。

Port            2222
ListenAddress       0.0.0.0
LoginGraceTime      180
X11Forwarding       yes
Ciphers aes128-cbc,3des-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
MACs hmac-sha1,hmac-sha1-96
StrictModes         yes
SyslogFacility      DAEMON
PasswordAuthentication  yes
PermitEmptyPasswords    no
PermitRootLogin     yes
Subsystem sftp internal-sftp

 備注: 此文件配置 OpenSSH 並且必須包括以下項:

  • Port 必須設置為 2222。
  • Ciphers 必須至少包含此列表中的一項:aes128-cbc,3des-cbc,aes256-cbc
  • MACs 必須至少包含此列表中的一項:hmac-sha1,hmac-sha1-96

 

2) 向存儲庫添加 ssh_setup 腳本文件,以使用 ssh-keygen 創建 SSH 密鑰。

#!/bin/sh

if [ ! -f "/etc/ssh/ssh_host_rsa_key" ]; then
    # generate fresh rsa key
    ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
fi

if [ ! -f "/etc/ssh/ssh_host_dsa_key" ]; then
    # generate fresh dsa key
    ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
fi

if [ ! -f "/etc/ssh/ssh_host_ecdsa_key" ]; then
    # generate fresh ecdsa key
    ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t dsa
fi

if [ ! -f "/etc/ssh/ssh_host_ed25519_key" ]; then
    # generate fresh ecdsa key
    ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t dsa
fi

#prepare run dir
    if [ ! -d "/var/run/sshd" ]; then
    mkdir -p /var/run/sshd
fi

 

3) 在 Dockerfile 中,添加以下命令:

# Install OpenSSH and set the password for root to "Docker!". In this example, "apk add" is the install instruction for an Alpine Linux-based image.
RUN apk add openssh \
     && echo "root:Docker!" | chpasswd 

# Copy the sshd_config file to the /etc/ssh/ directory
COPY sshd_config /etc/ssh/

# Copy and configure the ssh_setup file
RUN mkdir -p /tmp
COPY ssh_setup.sh /tmp
RUN chmod +x /tmp/ssh_setup.sh \
    && (sleep 1;/tmp/ssh_setup.sh 2>&1 > /dev/null)

# Open port 2222 for SSH access
EXPOSE 80 2222

 

4) 在容器的啟動腳本中啟動 SSH 服務器。

/usr/sbin/sshd

 

 

參考資料

為 Azure 應用服務配置自定義容器, 啟動SSHhttps://docs.microsoft.com/zh-cn/azure/app-service/configure-custom-container?pivots=container-linux#enable-ssh

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM