func (r *Remote) Connect() error {
	if r.session != nil {
		return nil
	}
	var client *ssh.Client
	var err error
	for range r.sshConfig.ClientConfig.Auth {
		client, err = ssh.Dial("tcp", r.sshConfig.Addr, r.sshConfig.ClientConfig)
		if err != nil {
			if len(r.sshConfig.ClientConfig.Auth) == 1 {
				return fmt.Errorf("Failed to connect with username=%s identity=%s password=%s",
					r.sshConfig.Username, r.sshConfig.Identity, r.sshConfig.Password)
			}
			r.sshConfig.ClientConfig.Auth = r.sshConfig.ClientConfig.Auth[1:]
		}
	}
	if client == nil {
		return fmt.Errorf("Failed to connect to %s", r.Host.Addr)
	}
	session, err := client.NewSession()
	if err != nil {
		return fmt.Errorf("Failed to create session: %s", err)
	}
	r.session = session
	return nil
}
Connect starts an ssh session to a remote machine
package hap
import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"golang.org/x/crypto/ssh"
	gcfg "gopkg.in/gcfg.v1"
)
const happened string = "if [ \"$(git rev-parse HEAD)\" = \"$(cat .happended)\" ]; then echo \"Already completed. Commit again?\"; exit 2; fi"
type Remote struct {
	Git        Git
	Dir        string
	Host       *Host
	env        []string
	gitSSHFile string
	sshConfig  SSHConfig
	session    *ssh.Session
	writer     io.Writer
}
func NewRemote(host *Host) (*Remote, error) {
	sshConfig := NewSSHConfig(host)
	clientConfig, err := NewClientConfig(sshConfig)
	if err != nil {
		return nil, err
	}
	sshConfig.ClientConfig = clientConfig
	dir := host.GetDir()
	repo := fmt.Sprintf("ssh://%s@%s/~/%s", host.Username, host.Addr, dir)
	r := &Remote{
		sshConfig:  sshConfig,
		Git:        Git{Repo: repo, Key: sshConfig.Identity},
		Dir:        dir,
		Host:       host,
		gitSSHFile: filepath.Join(os.TempDir(), "hap-ssh-"+host.Name),
	}
	var b = "#!/bin/sh\n\nssh "
	if r.sshConfig.Identity != "" {
		b = b + " -i " + r.sshConfig.Identity
	}
	b = b + " -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o VerifyHostKeyDNS=no -o CheckHostIP=no $@"
	err = ioutil.WriteFile(r.gitSSHFile, []byte(b), 0777)
	if err != nil {
		return r, fmt.Errorf("error writing git_ssh file: %s, error: %s", r.gitSSHFile, err)
	}
	return r, nil
}