Packer 在尝试建立 SSH 连接时循环通过端口

Packer loops through ports when attempting to establish SSH connection

当 Packer 到达 "Waiting for SSH to become available..." 步骤时。

我的日志显示

14:07:29 [INFO] Attempting SSH connection...
14:07:29 reconnecting to TCP connection for SSH
14:07:29 handshaking with SSH
14:07:29 handshake error: ssh: handshake failed: read tcp 127.0.0.1:60372->127.0.0.1:3057: read: connection reset by peer
14:07:29 [DEBUG] SSH handshake err: ssh: handshake failed: read tcp 127.0.0.1:60372->127.0.0.1:3057: read: connection reset by peer

14:07:36 [INFO] Attempting SSH connection...
14:07:36 reconnecting to TCP connection for SSH
14:07:36 handshaking with SSH
14:07:36 handshake error: ssh: handshake failed: read tcp 127.0.0.1:60376->127.0.0.1:3057: read: connection reset by peer
14:07:36 [DEBUG] SSH handshake err: ssh: handshake failed: read tcp 127.0.0.1:60376->127.0.0.1:3057: read: connection reset by peer

注意每次尝试时使用不同的端口。

60372
60376

Packer 每 7 秒尝试一个新端口。

有没有办法在构建之前或期间配置端口以避免这种 try/fail 方法?

这是建立 ssh 连接的源端口。它由 OS 分配,具有随机可用的高端口。

问题不在于 SSH 服务器或 TCP/IP。这与 Packer 的设计方式有关。

创建 VM 后,Packer.io 将 运行 引导命令。这需要时间,而且时间因机器而异。在那段时间你会看到 "Waiting for SSH to become available..."。在后台,Packer.io 将尝试建立 SSH 连接。日志中充斥着这样的消息

Linux
14:07:29 [INFO] Attempting SSH connection...
14:07:29 reconnecting to TCP connection for SSH
14:07:29 handshaking with SSH
14:07:29 handshake error: ssh: handshake failed: read tcp 127.0.0.1:60372->127.0.0.1:3057: read: connection reset by peer
14:07:29 [DEBUG] SSH handshake err: ssh: handshake failed: read tcp    127.0.0.1:60372->127.0.0.1:3057: read: connection reset by peer

Windows
15:54:31 packer.exe: 2017/02/01 15:54:31 [INFO] Attempting SSH connection...
15:54:31 packer.exe: 2017/02/01 15:54:31 reconnecting to TCP connection for SSH
15:54:31 packer.exe: 2017/02/01 15:54:31 handshaking with SSH
15:54:31 packer.exe: 2017/02/01 15:54:31 handshake error: ssh: handshake failed: read tcp 127.0.0.1:62691->127.0.0.1:4289: wsarecv: An existing connection was forcibly closed by the remote host.
15:54:31 packer.exe: 2017/02/01 15:54:31 [DEBUG] SSH handshake err: ssh: handshake failed: read tcp 127.0.0.1:62691->127.0.0.1:4289: wsarecv: An existing connection was forcibly closed by the remote host.

OS 将启动,来宾上的 SSH 服务器将可用。那时应该建立从主机到来宾的 SSH 连接。

在 Windows 10 Pro 和 Ubuntu 16.04.1 TLs 上转载并确认