2013-07-14 07:22:41 -04:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2013-07-14 08:02:47 -04:00
|
|
|
"errors"
|
2013-07-14 07:22:41 -04:00
|
|
|
"log"
|
|
|
|
"net"
|
2013-07-14 07:31:51 -04:00
|
|
|
"time"
|
2013-07-14 07:22:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectFunc is a convenience method for returning a function
|
|
|
|
// that just uses net.Dial to communicate with the remote end that
|
|
|
|
// is suitable for use with the SSH communicator configuration.
|
2013-07-14 08:05:22 -04:00
|
|
|
func ConnectFunc(network, addr string, timeout time.Duration) func() (net.Conn, error) {
|
2013-07-14 07:22:41 -04:00
|
|
|
return func() (net.Conn, error) {
|
2013-07-14 08:05:22 -04:00
|
|
|
timeoutCh := time.After(timeout)
|
2013-07-14 08:02:47 -04:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2013-07-14 08:05:22 -04:00
|
|
|
case <-timeoutCh:
|
2013-07-14 08:02:47 -04:00
|
|
|
return nil, errors.New("timeout connecting to remote machine")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Opening conn for SSH to %s %s", network, addr)
|
|
|
|
nc, err := net.DialTimeout(network, addr, 15*time.Second)
|
|
|
|
if err == nil {
|
|
|
|
return nc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
2013-07-14 07:22:41 -04:00
|
|
|
}
|
|
|
|
}
|