Merge pull request #2784 from orivej/fix-ssh-port-max
Make ssh_host_port_max an inclusive bound to agree with documentation
This commit is contained in:
commit
3ad7f067ab
|
@ -24,20 +24,12 @@ func (s *stepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
|
||||||
log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d", config.SSHHostPortMin, config.SSHHostPortMax)
|
log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d", config.SSHHostPortMin, config.SSHHostPortMax)
|
||||||
var sshHostPort uint
|
var sshHostPort uint
|
||||||
var offset uint = 0
|
|
||||||
|
|
||||||
portRange := int(config.SSHHostPortMax - config.SSHHostPortMin)
|
portRange := config.SSHHostPortMax - config.SSHHostPortMin + 1
|
||||||
if portRange > 0 {
|
offset := uint(rand.Intn(int(portRange)))
|
||||||
// Have to check if > 0 to avoid a panic
|
|
||||||
offset = uint(rand.Intn(portRange))
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
sshHostPort = offset + config.SSHHostPortMin
|
sshHostPort = offset + config.SSHHostPortMin
|
||||||
if sshHostPort >= config.SSHHostPortMax {
|
|
||||||
offset = 0
|
|
||||||
sshHostPort = config.SSHHostPortMin
|
|
||||||
}
|
|
||||||
log.Printf("Trying port: %d", sshHostPort)
|
log.Printf("Trying port: %d", sshHostPort)
|
||||||
l, err := net.Listen("tcp", fmt.Sprintf(":%d", sshHostPort))
|
l, err := net.Listen("tcp", fmt.Sprintf(":%d", sshHostPort))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -45,6 +37,9 @@ func (s *stepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
offset++
|
offset++
|
||||||
|
if offset == portRange {
|
||||||
|
offset = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ui.Say(fmt.Sprintf("Found port for communicator (SSH, WinRM, etc): %d.", sshHostPort))
|
ui.Say(fmt.Sprintf("Found port for communicator (SSH, WinRM, etc): %d.", sshHostPort))
|
||||||
|
|
||||||
|
|
|
@ -37,20 +37,12 @@ func (s *StepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
if !s.SkipNatMapping {
|
if !s.SkipNatMapping {
|
||||||
log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d",
|
log.Printf("Looking for available communicator (SSH, WinRM, etc) port between %d and %d",
|
||||||
s.HostPortMin, s.HostPortMax)
|
s.HostPortMin, s.HostPortMax)
|
||||||
offset := 0
|
|
||||||
|
|
||||||
portRange := int(s.HostPortMax - s.HostPortMin)
|
portRange := int(s.HostPortMax - s.HostPortMin + 1)
|
||||||
if portRange > 0 {
|
offset := rand.Intn(portRange)
|
||||||
// Have to check if > 0 to avoid a panic
|
|
||||||
offset = rand.Intn(portRange)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
sshHostPort = offset + int(s.HostPortMin)
|
sshHostPort = offset + int(s.HostPortMin)
|
||||||
if sshHostPort >= int(s.HostPortMax) {
|
|
||||||
offset = 0
|
|
||||||
sshHostPort = int(s.HostPortMin)
|
|
||||||
}
|
|
||||||
log.Printf("Trying port: %d", sshHostPort)
|
log.Printf("Trying port: %d", sshHostPort)
|
||||||
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", sshHostPort))
|
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", sshHostPort))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -58,6 +50,9 @@ func (s *StepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
offset++
|
offset++
|
||||||
|
if offset == portRange {
|
||||||
|
offset = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a forwarded port mapping to the VM
|
// Create a forwarded port mapping to the VM
|
||||||
|
|
Loading…
Reference in New Issue