问题描述

在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 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 应用服务配置自定义容器, 启动SSH: https://docs.microsoft.com/zh-cn/azure/app-service/configure-custom-container?pivots=container-linux#enable-ssh

 

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。