github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/communicator/ssh/connect.go (about)

     1  package ssh
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  )
     7  
     8  // ConnectFunc is a convenience method for returning a function
     9  // that just uses net.Dial to communicate with the remote end that
    10  // is suitable for use with the SSH communicator configuration.
    11  func ConnectFunc(network, addr string) func() (net.Conn, error) {
    12  	return func() (net.Conn, error) {
    13  		c, err := net.DialTimeout(network, addr, 15*time.Second)
    14  		if err != nil {
    15  			return nil, err
    16  		}
    17  
    18  		if tcpConn, ok := c.(*net.TCPConn); ok {
    19  			tcpConn.SetKeepAlive(true)
    20  			tcpConn.SetKeepAlivePeriod(5 * time.Second)
    21  		}
    22  
    23  		return c, nil
    24  	}
    25  }