コード例 #1
0
ファイル: cloud.go プロジェクト: sb10/vrpipe
// SSHClient returns an ssh.Client object that could be used to ssh to the
// server. Requires that port 22 is accessible for SSH.
func (s *Server) SSHClient() (*ssh.Client, error) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	if s.sshclient == nil {
		if s.provider.PrivateKey() == "" {
			log.Printf("resource file %s did not contain the ssh key\n", s.provider.savePath)
			return nil, errors.New("missing ssh key")
		}

		// parse private key and make config
		key, err := ssh.ParsePrivateKey([]byte(s.provider.PrivateKey()))
		if err != nil {
			log.Printf("failure to parse the private key: %s\n", err)
			return nil, err
		}
		sshConfig := &ssh.ClientConfig{
			User: s.UserName,
			Auth: []ssh.AuthMethod{
				ssh.PublicKeys(key),
			},
		}

		// dial in to the server, allowing certain errors that indicate that the
		// network or server isn't really ready for ssh yet; wait for up to
		// 5mins for success
		hostAndPort := s.IP + ":22"
		s.sshclient, err = ssh.Dial("tcp", hostAndPort, sshConfig)
		if err != nil {
			limit := time.After(5 * time.Minute)
			ticker := time.NewTicker(1 * time.Second)
		DIAL:
			for {
				select {
				case <-ticker.C:
					s.sshclient, err = ssh.Dial("tcp", hostAndPort, sshConfig)
					if err != nil && (strings.HasSuffix(err.Error(), "connection timed out") || strings.HasSuffix(err.Error(), "no route to host") || strings.HasSuffix(err.Error(), "connection refused")) {
						continue DIAL
					}
					// worked, or failed with a different error: stop trying
					ticker.Stop()
					break DIAL
				case <-limit:
					ticker.Stop()
					err = errors.New("giving up waiting for ssh to work")
					break DIAL
				}
			}
			if err != nil {
				return nil, err
			}
		}
	}
	return s.sshclient, nil
}
コード例 #2
0
ファイル: ssh.go プロジェクト: arfian/live
func (S *SshParm) Connect() (*ssh.Client, error) {
	var (
		cfg *ssh.ClientConfig
	)

	if S.SSHAuthType == SSHAuthType_Certificate {
		cfg = &ssh.ClientConfig{
			User: S.SSHUser,
			Auth: []ssh.AuthMethod{
				PublicKeyFile(S.SSHKeyLocation),
			},
		}
	} else {
		cfg = &ssh.ClientConfig{
			User: S.SSHUser,
			Auth: []ssh.AuthMethod{
				ssh.Password(S.SSHPassword),
			},
		}
	}

	client, e := ssh.Dial("tcp", S.SSHHost, cfg)
	return client, e

}
コード例 #3
0
ファイル: client.go プロジェクト: jkingyens/machine
func (client NativeClient) dialSuccess() bool {
	if _, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Hostname, client.Port), &client.Config); err != nil {
		log.Debugf("Error dialing TCP: %s", err)
		return false
	}
	return true
}
コード例 #4
0
// Simply waits for ssh to come up
func waitForVmSsh(d *schema.ResourceData) error {
	l := log.New(os.Stderr, "", 0)

	l.Printf("Waiting for VM ssh: %s", d.Get("name"))

	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.Password(d.Get("root_password").(string)),
		},
	}

	for {
		select {
		case <-time.After(waitForVM * time.Second):
			return fmt.Errorf("VM ssh wasn't up in %d seconds", waitForVM)
		case <-time.Tick(vmCheckInterval * time.Second):
			conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:22", d.Get("ipv4")), config)
			if err != nil {
				if strings.Contains(err.Error(), "connection refused") {
					l.Println("SSH isn't up yet")
					continue
				} else {
					l.Printf("SSH Error, ignored: %s", err.Error())
					continue
				}
			}
			conn.Close()
			l.Println("SSH alive and kicking")
			return nil
		}
	}

	return errors.New("Ssh wait should never get here")
}
コード例 #5
0
ファイル: vagrantnode.go プロジェクト: balajisiva/netplugin
//NewVagrantNode intializes a node in vagrant testbed
func NewVagrantNode(name, port, privKeyFile string) (*VagrantNode, error) {
	var (
		vnode      *VagrantNode
		err        error
		signer     ssh.Signer
		privateKey []byte
	)

	if privateKey, err = ioutil.ReadFile(privKeyFile); err != nil {
		return nil, err
	}

	if signer, err = ssh.ParsePrivateKey(privateKey); err != nil {
		return nil, err
	}

	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
	}

	vnode = &VagrantNode{Name: name}
	if vnode.client, err = ssh.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", port), config); err != nil {
		return nil, err
	}

	return vnode, nil
}
コード例 #6
0
ファイル: Scp.go プロジェクト: krishna2nd/goll
func (this *Scp) Init(target_server, user, identity string) (*Scp, error) {

	this.Server = target_server
	this.User = user
	this.Identity = identity

	AuthKey, err := PublicKeyFile(this.Identity)

	if nil != err {
		return nil, err
	}

	this.clientConfig = &ssh.ClientConfig{
		User: this.User,
		Auth: []ssh.AuthMethod{AuthKey},
	}

	ParallelController[this.Server].Connection <- token
	this.client, err = ssh.Dial("tcp", this.Server+":22", this.clientConfig)

	if nil != err {
		return nil, errors.New("Failed to dial: " + err.Error())
	}

	return this, nil
}
コード例 #7
0
ファイル: example_test.go プロジェクト: CodeJuan/deis
func ExampleSession_RequestPty() {
	// Create client config
	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.Password("password"),
		},
	}
	// Connect to ssh server
	conn, err := ssh.Dial("tcp", "localhost:22", config)
	if err != nil {
		log.Fatalf("unable to connect: %s", err)
	}
	defer conn.Close()
	// Create a session
	session, err := conn.NewSession()
	if err != nil {
		log.Fatalf("unable to create session: %s", err)
	}
	defer session.Close()
	// Set up terminal modes
	modes := ssh.TerminalModes{
		ssh.ECHO:          0,     // disable echoing
		ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
		ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
	}
	// Request pseudo terminal
	if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
		log.Fatalf("request for pseudo terminal failed: %s", err)
	}
	// Start remote shell
	if err := session.Shell(); err != nil {
		log.Fatalf("failed to start shell: %s", err)
	}
}
コード例 #8
0
ファイル: example_test.go プロジェクト: CodeJuan/deis
func ExampleDial() {
	// An SSH client is represented with a ClientConn. Currently only
	// the "password" authentication method is supported.
	//
	// To authenticate with the remote server you must pass at least one
	// implementation of AuthMethod via the Auth field in ClientConfig.
	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.Password("yourpassword"),
		},
	}
	client, err := ssh.Dial("tcp", "yourserver.com:22", config)
	if err != nil {
		panic("Failed to dial: " + err.Error())
	}

	// Each ClientConn can support multiple interactive sessions,
	// represented by a Session.
	session, err := client.NewSession()
	if err != nil {
		panic("Failed to create session: " + err.Error())
	}
	defer session.Close()

	// Once a Session is created, you can execute a single command on
	// the remote side using the Run method.
	var b bytes.Buffer
	session.Stdout = &b
	if err := session.Run("/usr/bin/whoami"); err != nil {
		panic("Failed to run: " + err.Error())
	}
	fmt.Println(b.String())
}
コード例 #9
0
ファイル: client.go プロジェクト: newxan/gopistrano
func NewConnection(host string, port int, username string, password string, key_path string) (*ssh.Client, error) {
	var config *ssh.ClientConfig
	if USEKEY {
		public_key, err := getKeyFromFile(key_path)
		if err != nil {
			log.Panic(err)
		}
		config = &ssh.ClientConfig{
			User: username,
			Auth: []ssh.AuthMethod{
				ssh.PublicKeys(public_key),
			},
		}
	} else {
		config = &ssh.ClientConfig{
			User: username,
			Auth: []ssh.AuthMethod{
				ssh.Password(password),
			},
		}
	}

	client, err := ssh.Dial("tcp", host+":"+strconv.Itoa(port), config)
	if err != nil {
		panic("Failed to dial: " + err.Error())
	}

	// Each ClientConn can support multiple interactive sessions,
	// represented by a Session.
	return client, err
}
コード例 #10
0
func DownLoadDirectoryRecurrsively(hostAndPort string, username string,
	password string, remoteSourceDirectory string, localTargetDirectory string) error {

	remoteSourceDirectoryLength := len(remoteSourceDirectory)

	authMethodSlice := make([]ssh.AuthMethod, 0)
	authMethodSlice = append(authMethodSlice, ssh.Password(password))

	clientConfig := ssh.ClientConfig{
		User: username,
		Auth: authMethodSlice,
	}
	connection, err := ssh.Dial("tcp", hostAndPort, &clientConfig)
	if err != nil {
		return err
	}
	defer connection.Close()

	// open an SFTP session over an existing ssh connection.
	client, err := sftp.NewClient(connection)
	if err != nil {
		return err
	}
	defer client.Close()

	// walk a directory
	walk := client.Walk(remoteSourceDirectory)
	for walk.Step() {
		if err := walk.Err(); err != nil {
			return err
		}

		if walk.Stat().IsDir() {
			directoryPath := localTargetDirectory + walk.Path()[remoteSourceDirectoryLength:]
			if err := os.MkdirAll(directoryPath, os.ModePerm); err != nil {
				return err
			}
		} else {
			filePath := localTargetDirectory + walk.Path()[remoteSourceDirectoryLength:]
			file, err := client.Open(walk.Path())
			if err != nil {
				return err
			}
			defer file.Close()

			outputFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
			if err != nil {
				return err
			}
			defer outputFile.Close()

			_, err = file.WriteTo(outputFile)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
コード例 #11
0
ファイル: server_test.go プロジェクト: blacklabeldata/sshh
func (suite *ServerSuite) TestClientConnection() {

	// Get signer
	signer, err := ssh.ParsePrivateKey([]byte(clientPrivateKey))
	if err != nil {
		suite.Fail("Private key could not be parsed" + err.Error())
	}

	// Configure client connection
	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
	}

	// Create client connection
	client, err := ssh.Dial("tcp", "127.0.0.1:9022", config)
	if err != nil {
		suite.Fail(err.Error())
		return
	}
	defer client.Close()

	// Open channel
	channel, requests, err := client.OpenChannel("/echo", []byte{})
	if err != nil {
		suite.Fail(err.Error())
		return
	}
	go ssh.DiscardRequests(requests)
	defer channel.Close()
}
コード例 #12
0
ファイル: server_test.go プロジェクト: blacklabeldata/sshh
func (suite *ServerSuite) TestHandlerError() {

	// Configure client connection
	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.Password("bandit"),
		},
	}

	// Create client connection
	client, err := ssh.Dial("tcp", "127.0.0.1:9022", config)
	if err != nil {
		suite.Fail(err.Error())
		return
	}
	defer client.Close()

	// Open channel
	channel, requests, err := client.OpenChannel("/bad", []byte{})
	if err != nil {
		suite.Fail(err.Error())
		return
	}
	go ssh.DiscardRequests(requests)
	defer channel.Close()
}
コード例 #13
0
ファイル: server_test.go プロジェクト: blacklabeldata/sshh
func (suite *ServerSuite) TestUnknownChannel() {

	// Get signer
	signer, err := ssh.ParsePrivateKey([]byte(clientPrivateKey))
	if err != nil {
		suite.Fail("Private key could not be parsed" + err.Error())
	}

	// Configure client connection
	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
	}

	// Create client connection
	client, err := ssh.Dial("tcp", "127.0.0.1:9022", config)
	if err != nil {
		suite.Fail(err.Error())
		return
	}
	defer client.Close()

	// Open channel
	_, _, err = client.OpenChannel("/shell", []byte{})
	suite.NotNil(err, "server should not accept shell channels")
}
コード例 #14
0
ファイル: sshClient.go プロジェクト: viliamjr/lab
func main() {

	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.Password("minhaSenha"),
		},
	}

	client, err := ssh.Dial("tcp", "meuservidor.com:22", config)
	if err != nil {
		panic("Failed to dial: " + err.Error())
	}

	session, err := client.NewSession()
	if err != nil {
		panic("Failed to create session: " + err.Error())
	}

	defer session.Close()

	var b bytes.Buffer
	session.Stdout = &b
	if err := session.Run("/usr/bin/whoami"); err != nil {
		panic("Failed to run: " + err.Error())
	}
	fmt.Println(b.String())
}
コード例 #15
0
ファイル: k-installer.go プロジェクト: talofo/k-env-setup
// Creates a ssh connection between the local machine and the remote server.
func (p *project) connect(config *ssh.ClientConfig) {

	fmt.Println("Trying connection...")

	conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", p.hostname.name, p.port.name), config)
	errorUtil.CheckError("Failed to dial: ", err)
	fmt.Println("Connection established.")

	session, err := conn.NewSession()
	errorUtil.CheckError("Failed to build session: ", err)
	defer session.Close()

	// Loops over the slice of commands to be executed on the remote.
	for step := range p.typ.program.setup {

		if p.typ.program.setup[step] == "post-update configuration" {
			p.secureCopy(conn)
		} else if p.typ.program.setup[step] == p.projectname.name+".dev" {
			p.makeDirOnLocal(step)
		} else if p.typ.program.setup[step] == "git clone" {
			p.gitOnLocal(step)
		} else {
			p.installOnRemote(step, conn)
		}
	}
}
コード例 #16
0
ファイル: example_test.go プロジェクト: 40a/ejson
func ExamplePublicKeys() {
	// A public key may be used to authenticate against the remote
	// server by using an unencrypted PEM-encoded private key file.
	//
	// If you have an encrypted private key, the crypto/x509 package
	// can be used to decrypt it.
	key, err := ioutil.ReadFile("/home/user/.ssh/id_rsa")
	if err != nil {
		log.Fatalf("unable to read private key: %v", err)
	}

	// Create the Signer for this private key.
	signer, err := ssh.ParsePrivateKey(key)
	if err != nil {
		log.Fatalf("unable to parse private key: %v", err)
	}

	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			// Use the PublicKeys method for remote authentication.
			ssh.PublicKeys(signer),
		},
	}

	// Connect to the remote server and perform the SSH handshake.
	client, err := ssh.Dial("tcp", "host.com:22", config)
	if err != nil {
		log.Fatalf("unable to connect: %v", err)
	}
	defer client.Close()
}
コード例 #17
0
ファイル: goship.go プロジェクト: cinderalla/goship
// remoteCmdOutput runs the given command on a remote server at the given hostname as the given user.
func remoteCmdOutput(username, hostname, cmd string, privateKey []byte) (b []byte, err error) {
	p, err := ssh.ParseRawPrivateKey(privateKey)
	if err != nil {
		return b, err
	}
	s, err := ssh.NewSignerFromKey(p)
	if err != nil {
		return b, err
	}
	pub := ssh.PublicKeys(s)
	clientConfig := &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{pub},
	}
	client, err := ssh.Dial("tcp", hostname, clientConfig)
	if err != nil {
		return b, errors.New("ERROR: Failed to dial: " + err.Error())
	}
	defer client.Close()
	session, err := client.NewSession()
	if err != nil {
		return b, errors.New("ERROR: Failed to create session: " + err.Error())
	}
	defer session.Close()
	b, err = session.Output(cmd)
	if err != nil {
		return b, fmt.Errorf("ERROR: Failed to run cmd on host %s: %s", hostname, err.Error())
	}
	return b, nil
}
コード例 #18
0
ファイル: input_handler.go プロジェクト: tri-star/mixtail
func (ih *InputHandler) createSession(config *InputEntry) (session *ssh.Session, err error) {

	var authMethod []ssh.AuthMethod
	var key *ssh.Signer
	if !config.Cred.IsPasswordAuth() {
		key, err = ih.parsePrivateKey(config.Cred.Identity)
		if err != nil {
			return
		}
		authMethod = []ssh.AuthMethod{ssh.PublicKeys(*key)}
	} else {
		authMethod = []ssh.AuthMethod{ssh.Password(config.Cred.Pass)}
	}

	sshConfig := new(ssh.ClientConfig)
	sshConfig.User = config.Cred.User
	sshConfig.Auth = authMethod

	port := uint16(22)
	if config.Port != 0 {
		port = config.Port
	}
	hostNameString := fmt.Sprintf("%s:%d", config.Host, port)
	client, err := ssh.Dial("tcp", hostNameString, sshConfig)
	if err != nil {
		return
	}

	session, err = client.NewSession()
	return
}
コード例 #19
0
ファイル: example_test.go プロジェクト: CodeJuan/deis
func ExampleClient_Listen() {
	config := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.Password("password"),
		},
	}
	// Dial your ssh server.
	conn, err := ssh.Dial("tcp", "localhost:22", config)
	if err != nil {
		log.Fatalf("unable to connect: %s", err)
	}
	defer conn.Close()

	// Request the remote side to open port 8080 on all interfaces.
	l, err := conn.Listen("tcp", "0.0.0.0:8080")
	if err != nil {
		log.Fatalf("unable to register tcp forward: %v", err)
	}
	defer l.Close()

	// Serve HTTP with your SSH server acting as a reverse proxy.
	http.Serve(l, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(resp, "Hello world!\n")
	}))
}
コード例 #20
0
ファイル: fabssh.go プロジェクト: benlightning/fabux
func Client(ip_port, user, password string, command map[string]string) {
	PassWd := []ssh.AuthMethod{ssh.Password(password)}
	Conf := ssh.ClientConfig{User: user, Auth: PassWd}
	Client, err := ssh.Dial("tcp", ip_port, &Conf)
	if err == nil {
		fmt.Println(ip_port, "connect status:success")
	} else {
		fmt.Print(ip_port, "connect error:", err)
		return
	}
	defer Client.Close()
	for _, cmd := range command {
		if session, err := Client.NewSession(); err == nil {
			defer session.Close()
			var Result bytes.Buffer
			session.Stderr = &Result
			session.Stdout = &Result
			err = session.Run(cmd)
			if err == nil {
				fmt.Println(ip_port, "run command:", cmd, "run status:Ok")
			} else {
				fmt.Println(ip_port, "run error:", err)
			}
			fmt.Println(ip_port, "run result:\n", Result.String())
		}
	}
}
コード例 #21
0
func (s *Command) Connect() error {
	host := helpers.StringOrDefault(s.Host, "localhost")
	user := helpers.StringOrDefault(s.User, "root")
	port := helpers.StringOrDefault(s.Port, "22")

	methods, err := s.getSSHAuthMethods()
	if err != nil {
		return err
	}

	config := &ssh.ClientConfig{
		User: user,
		Auth: methods,
	}

	connectRetries := s.ConnectRetries
	if connectRetries == 0 {
		connectRetries = 3
	}

	var finalError error

	for i := 0; i < connectRetries; i++ {
		client, err := ssh.Dial("tcp", host+":"+port, config)
		if err == nil {
			s.client = client
			return nil
		}
		time.Sleep(sshRetryInterval * time.Second)
		finalError = err
	}

	return finalError
}
コード例 #22
0
ファイル: main.go プロジェクト: andreas-venturini/drone-rsync
// Run commands on the remote host
func (rs *Rsync) run(keys *drone.Key, host string) error {

	// join the host and port if necessary
	addr := net.JoinHostPort(host, strconv.Itoa(rs.Port))

	// trace command used for debugging in the build logs
	fmt.Printf("$ ssh %s@%s -p %d\n", rs.User, addr, rs.Port)

	signer, err := ssh.ParsePrivateKey([]byte(keys.Private))
	if err != nil {
		return fmt.Errorf("Error parsing private key. %s.", err)
	}

	config := &ssh.ClientConfig{
		User: rs.User,
		Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
	}

	client, err := ssh.Dial("tcp", addr, config)
	if err != nil {
		return fmt.Errorf("Error dialing server. %s.", err)
	}

	session, err := client.NewSession()
	if err != nil {
		return fmt.Errorf("Error starting ssh session. %s.", err)
	}
	defer session.Close()

	session.Stdout = os.Stdout
	session.Stderr = os.Stderr
	return session.Run(strings.Join(rs.Commands, "\n"))
}
コード例 #23
0
ファイル: ssh.go プロジェクト: rootless4real/musicsaur
// connects to remote server using ClientSSH struct and returns *ssh.Session
func (ssh_conf *ClientSSH) connect() (*ssh.Session, error) {
	// auths holds the detected ssh auth methods
	auths := []ssh.AuthMethod{}

	// figure out what auths are requested, what is supported
	if ssh_conf.Password != "" {
		auths = append(auths, ssh.Password(ssh_conf.Password))
	}

	if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
		defer sshAgent.Close()
	}

	if pubkey, err := getKeyFile(ssh_conf.Key); err == nil {
		auths = append(auths, ssh.PublicKeys(pubkey))
	}

	config := &ssh.ClientConfig{
		User: ssh_conf.User,
		Auth: auths,
	}

	client, err := ssh.Dial("tcp", ssh_conf.Server+":"+ssh_conf.Port, config)
	if err != nil {
		return nil, err
	}

	session, err := client.NewSession()
	if err != nil {
		return nil, err
	}

	return session, nil
}
コード例 #24
0
ファイル: ssh.go プロジェクト: jmsdnns/omnitool
func dialServer(hostname string, config *ssh.ClientConfig) (*ssh.Client, error) {
	client, err := ssh.Dial("tcp", hostname, config)
	if err != nil {
		return nil, err
	}
	return client, nil
}
コード例 #25
0
ファイル: client.go プロジェクト: slawosz/ssh-commander
func executeCmd(cmd, hostname string) string {
	// return fmt.Sprintf("executing %v on %v\n", cmd, hostname)
	config := &ssh.ClientConfig{
		User:   "******",
		Auth:   []ssh.AuthMethod{ssh.Password("bob2")},
		Config: ssh.Config{Ciphers: []string{"aes192-ctr"}},
	}

	fmt.Printf("executing %v on %v\n", cmd, hostname)
	client, err := ssh.Dial("tcp", hostname, config)
	if err != nil {
		fmt.Println(err)
	}
	if client == nil {
		fmt.Printf("Connection to %v failed\n", hostname)
		return ""
	}
	session, err := client.NewSession()
	if err != err {
		panic(err)
	}
	defer session.Close()

	var stdoutBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	err = session.Start(cmd)
	if err != nil {
		fmt.Printf("Session failed with error: %v\n", err)
	}
	session.Wait()
	fmt.Printf("DONE: %v on %v\n", cmd, hostname)

	return hostname + ": " + stdoutBuf.String()
}
コード例 #26
0
ファイル: shell.go プロジェクト: rosenhouse/proctor
func (r *Runner) ConnectAndRun(host, command string, options *ConnectionOptions) (string, error) {
	signer, err := ssh.ParsePrivateKey(options.PrivateKeyPEM)
	if err != nil {
		return "", err
	}

	config := &ssh.ClientConfig{
		User: options.Username,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
	}
	client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, options.Port), config)
	if err != nil {
		return "", fmt.Errorf("failed to dial: %s", err)
	}
	defer client.Close()

	session, err := client.NewSession()
	if err != nil {
		return "", fmt.Errorf("failed to create session: ", err)
	}
	defer session.Close()

	var stdoutBytes bytes.Buffer
	session.Stdout = &stdoutBytes
	session.Stderr = os.Stderr
	if err := session.Run(command); err != nil {
		return "", fmt.Errorf("failed while running command: %s", err)
	}
	return stdoutBytes.String(), nil
}
コード例 #27
0
ファイル: remote.go プロジェクト: KLab/gohakai
func (n *Node) NewSSHSession() (session *ssh.Session, err error) {
	pkey, err := ioutil.ReadFile(n.SSHKeyFile)
	if err != nil {
		log.Println("ioutil.ReadFile(sshkey):", err)
		return session, err
	}

	s, err := ssh.ParsePrivateKey(pkey)
	if err != nil {
		log.Println("ssh.ParsePrivateKey():", err)
		return session, err
	}

	config := &ssh.ClientConfig{
		User: n.User,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(s),
		},
	}

	host := fmt.Sprintf("%s:%d", n.Host, n.Port)
	client, err := ssh.Dial("tcp", host, config)
	if err != nil {
		log.Println("ssh.Dial:", err)
		return session, err
	}

	session, err = client.NewSession()
	if err != nil {
		log.Println("cli.NewSession():", err)
		return session, err
	}

	return session, err
}
コード例 #28
0
ファイル: ssh.go プロジェクト: cmikk/sshttp
func sshClient(username, host string) (*ssh.Client, error) {
	agentconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err != nil {
		return nil, err
	}

	aclient := agent.NewClient(agentconn)

	conf := &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeysCallback(aclient.Signers),
		},
	}

	if strings.Index(host, ":") < 0 {
		host = host + ":22"
	}

	cli, err := ssh.Dial("tcp", host, conf)
	if err != nil {
		return nil, err
	}

	return cli, nil
}
コード例 #29
0
ファイル: fusion.go プロジェクト: rominirani/machine
// execute command over SSH with user / password authentication
func executeSSHCommand(command string, d *Driver) error {
	log.Debugf("Execute executeSSHCommand: %s", command)

	config := &cryptossh.ClientConfig{
		User: d.SSHUser,
		Auth: []cryptossh.AuthMethod{
			cryptossh.Password(d.SSHPassword),
		},
	}

	client, err := cryptossh.Dial("tcp", fmt.Sprintf("%s:%d", d.IPAddress, d.SSHPort), config)
	if err != nil {
		log.Debugf("Failed to dial:", err)
		return err
	}

	session, err := client.NewSession()
	if err != nil {
		log.Debugf("Failed to create session: " + err.Error())
		return err
	}
	defer session.Close()

	var b bytes.Buffer
	session.Stdout = &b

	if err := session.Run(command); err != nil {
		log.Debugf("Failed to run: " + err.Error())
		return err
	}
	log.Debugf("Stdout from executeSSHCommand: %s", b.String())

	return nil
}
コード例 #30
0
ファイル: communicator.go プロジェクト: econnell/terraform
// BastionConnectFunc is a convenience method for returning a function
// that connects to a host over a bastion connection.
func BastionConnectFunc(
	bProto string,
	bAddr string,
	bConf *ssh.ClientConfig,
	proto string,
	addr string) func() (net.Conn, error) {
	return func() (net.Conn, error) {
		log.Printf("[DEBUG] Connecting to bastion: %s", bAddr)
		bastion, err := ssh.Dial(bProto, bAddr, bConf)
		if err != nil {
			return nil, fmt.Errorf("Error connecting to bastion: %s", err)
		}

		log.Printf("[DEBUG] Connecting via bastion (%s) to host: %s", bAddr, addr)
		conn, err := bastion.Dial(proto, addr)
		if err != nil {
			bastion.Close()
			return nil, err
		}

		// Wrap it up so we close both things properly
		return &bastionConn{
			Conn:    conn,
			Bastion: bastion,
		}, nil
	}
}