Exemplo n.º 1
0
func (c *comm) reconnect() (err error) {
	if c.conn != nil {
		c.conn.Close()
	}

	// Set the conn and client to nil since we'll recreate it
	c.conn = nil
	c.client = nil

	log.Printf("reconnecting to TCP connection for SSH")
	c.conn, err = c.config.Connection()
	if err != nil {
		// Explicitly set this to the REAL nil. Connection() can return
		// a nil implementation of net.Conn which will make the
		// "if c.conn == nil" check fail above. Read here for more information
		// on this psychotic language feature:
		//
		// http://golang.org/doc/faq#nil_error
		c.conn = nil

		log.Printf("reconnection error: %s", err)
		return
	}

	log.Printf("handshaking with SSH")
	c.client, err = ssh.Client(c.conn, c.config.SSHConfig)
	if err != nil {
		log.Printf("handshake error: %s", err)
	}

	return
}
Exemplo n.º 2
0
func (s *server) Dial(config *ssh.ClientConfig) *ssh.ClientConn {
	s.cmd = exec.Command("sshd", "-f", s.configfile, "-i")
	stdin, err := s.cmd.StdinPipe()
	if err != nil {
		s.t.Fatal(err)
	}
	stdout, err := s.cmd.StdoutPipe()
	if err != nil {
		s.t.Fatal(err)
	}
	s.cmd.Stderr = os.Stderr // &s.output
	err = s.cmd.Start()
	if err != nil {
		s.t.FailNow()
		s.Shutdown()
		s.t.Fatal(err)
	}
	conn, err := ssh.Client(&client{stdin, stdout}, config)
	if err != nil {
		s.t.FailNow()
		s.Shutdown()
		s.t.Fatal(err)
	}
	return conn
}
func (s *server) TryDial(config *ssh.ClientConfig) (*ssh.ClientConn, error) {
	sshd, err := exec.LookPath("sshd")
	if err != nil {
		s.t.Skipf("skipping test: %v", err)
	}

	c1, c2, err := unixConnection()
	if err != nil {
		s.t.Fatalf("unixConnection: %v", err)
	}

	s.cmd = exec.Command(sshd, "-f", s.configfile, "-i", "-e")
	f, err := c2.File()
	if err != nil {
		s.t.Fatalf("UnixConn.File: %v", err)
	}
	defer f.Close()
	s.cmd.Stdin = f
	s.cmd.Stdout = f
	s.cmd.Stderr = &s.output
	if err := s.cmd.Start(); err != nil {
		s.t.Fail()
		s.Shutdown()
		s.t.Fatalf("s.cmd.Start: %v", err)
	}
	s.clientConn = c1
	return ssh.Client(c1, config)
}
Exemplo n.º 4
0
Arquivo: ssh_test.go Projeto: kr/runx
func TestSSHD(t *testing.T) {
	block, _ := pem.Decode([]byte(testClientPrivateKey))
	rsakey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
	pub, _ := ssh.NewPublicKey(&rsakey.PublicKey)
	cmd, c, err := startSSHD(ssh.MarshalAuthorizedKey(pub))
	if err != nil {
		t.Fatal(err)
	}
	defer cmd.Wait()
	defer cmd.Process.Kill()
	u, err := user.Current()
	if err != nil {
		t.Fatal(err)
	}
	_ = u
	config := &ssh.ClientConfig{
		User: u.Username,
		Auth: []ssh.ClientAuth{ssh.ClientAuthKeyring(&keyring{rsakey})},
	}
	client, err := ssh.Client(c, config)
	if err != nil {
		t.Fatal(err)
	}
	sess, err := client.NewSession()
	if err != nil {
		t.Fatal(err)
	}
	out, err := sess.Output("echo hello")
	if err != nil {
		t.Fatal(err)
	}
	if string(out) != "hello\n" {
		t.Fatalf("out = %q want %q", string(out), "hello\n")
	}
}
Exemplo n.º 5
0
func (s *server) Dial(config *ssh.ClientConfig) *ssh.ClientConn {
	s.cmd = exec.Command("sshd", "-f", s.configfile, "-i")
	r1, w1, err := os.Pipe()
	if err != nil {
		s.t.Fatal(err)
	}
	s.cmd.Stdout = w1
	r2, w2, err := os.Pipe()
	if err != nil {
		s.t.Fatal(err)
	}
	s.cmd.Stdin = r2
	s.cmd.Stderr = os.Stderr
	if err := s.cmd.Start(); err != nil {
		s.t.Fail()
		s.Shutdown()
		s.t.Fatalf("s.cmd.Start: %v", err)
	}
	conn, err := ssh.Client(&client{wc: w2, r: r1}, config)
	if err != nil {
		s.t.Fail()
		s.Shutdown()
		s.t.Fatalf("ssh.Client: %v", err)
	}
	return conn
}
Exemplo n.º 6
0
func (s *server) TryDial(config *ssh.ClientConfig) (*ssh.ClientConn, error) {
	sshd, err := exec.LookPath("sshd")
	if err != nil {
		s.t.Skipf("skipping test: %v", err)
	}
	s.cmd = exec.Command(sshd, "-f", s.configfile, "-i", "-e")
	r1, w1, err := os.Pipe()
	if err != nil {
		s.t.Fatal(err)
	}
	s.cmd.Stdout = w1
	r2, w2, err := os.Pipe()
	if err != nil {
		s.t.Fatal(err)
	}
	s.cmd.Stdin = r2
	s.cmd.Stderr = os.Stderr
	if err := s.cmd.Start(); err != nil {
		s.t.Fail()
		s.Shutdown()
		s.t.Fatalf("s.cmd.Start: %v", err)
	}

	return ssh.Client(&client{wc: w2, r: r1}, config)
}
Exemplo n.º 7
0
Arquivo: ssh.go Projeto: reth-/mole
func sshOnConn(conn net.Conn, h conf.Host) (*ssh.ClientConn, error) {
	var auths []ssh.ClientAuth

	if h.Pass != "" {
		auths = append(auths, ssh.ClientAuthPassword(password(h.Pass)))
		auths = append(auths, ssh.ClientAuthKeyboardInteractive(challenge(h.Pass)))
	}

	if h.Key != "" {
		k := &keyring{}
		err := k.loadPEM([]byte(h.Key))
		if err != nil {
			return nil, err
		}
		auths = append(auths, ssh.ClientAuthKeyring(k))
	}

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

	debugln("handshake & authenticate")
	client, err := ssh.Client(conn, config)
	if err != nil {
		return nil, err
	}
	return client, nil
}
Exemplo n.º 8
0
Arquivo: pool.go Projeto: kr/sshpool
func (p *Pool) dial(network, addr string, config *ssh.ClientConfig, deadline time.Time) (net.Conn, *ssh.ClientConn, error) {
	dial := p.Dial
	if dial == nil {
		dialer := net.Dialer{Deadline: deadline}
		dial = dialer.Dial
	}
	netC, err := dial(network, addr)
	if err != nil {
		return nil, nil, err
	}
	sshC, err := ssh.Client(netC, config)
	if err != nil {
		netC.Close()
		return nil, nil, err
	}
	return netC, sshC, nil
}
Exemplo n.º 9
0
func (c *comm) reconnect() (err error) {
	if c.conn != nil {
		c.conn.Close()
	}

	log.Printf("reconnecting to TCP connection for SSH")
	c.conn, err = c.config.Connection()
	if err != nil {
		log.Printf("reconnection error: %s", err)
		return
	}

	log.Printf("handshaking with SSH")
	c.client, err = ssh.Client(c.conn, c.config.SSHConfig)
	if err != nil {
		log.Printf("handshake error: %s", err)
	}

	return
}
Exemplo n.º 10
0
// Create a new NETCONF session using an existing net.Conn.
func DialSSHWithAddress(conn *net.Conn, config *ssh.ClientConfig) (*Session, error) {
	var (
		t   TransportSSH
		err error
	)

	t.sshConn, err = ssh.Client(*conn, config)
	if err != nil {
		return nil, err
	}

	err = t.setSessionAndPipes()
	if err != nil {
		return nil, err
	}

	err = t.requestNetconfSubsystem()
	if err != nil {
		return nil, err
	}

	return NewSession(&t), nil
}
Exemplo n.º 11
0
func (conn *SSHRawConnection) GetClientConn(reconnect bool) (*ssh.ClientConn, error) {
	if !reconnect && nil != conn.clientConn {
		return conn.clientConn, nil
	} else {
		if nil != conn.clientConn {
			conn.clientConn.Close()
		}
		conn.clientConn = nil
		dial := net.Dial
		if nil != sshLocalProxy {
			dial = func(network, addr string) (net.Conn, error) {
				return util.HttpTunnelDial(network, addr, sshLocalProxy)
			}
		}
		if c, err := dial("tcp", conn.Server); nil != err {
			return nil, err
		} else {
			conn.clientConn, err = ssh.Client(c, conn.ClientConfig)
			return conn.clientConn, err
		}
	}
	return nil, nil
}
Exemplo n.º 12
0
func (s *server) Dial() *ssh.ClientConn {
	s.cmd = exec.Command("sshd", "-f", s.configfile, "-i")
	stdin, err := s.cmd.StdinPipe()
	if err != nil {
		s.t.Fatal(err)
	}
	stdout, err := s.cmd.StdoutPipe()
	if err != nil {
		s.t.Fatal(err)
	}
	s.cmd.Stderr = os.Stderr
	err = s.cmd.Start()
	if err != nil {
		s.Shutdown()
		s.t.Fatal(err)
	}

	user, err := user.Current()
	if err != nil {
		s.Shutdown()
		s.t.Fatal(err)
	}
	kc := new(keychain)
	kc.keys = append(kc.keys, rsakey)
	config := &ssh.ClientConfig{
		User: user.Username,
		Auth: []ssh.ClientAuth{
			ssh.ClientAuthKeyring(kc),
		},
	}
	conn, err := ssh.Client(&client{stdin, stdout}, config)
	if err != nil {
		s.Shutdown()
		s.t.Fatal(err)
	}
	return conn
}
Exemplo n.º 13
0
func (c *comm) reconnect() (err error) {
	if c.conn != nil {
		c.conn.Close()
	}

	// Set the conn and client to nil since we'll recreate it
	c.conn = nil
	c.client = nil

	log.Printf("reconnecting to TCP connection for SSH")
	c.conn, err = c.config.Connection()
	if err != nil {
		log.Printf("reconnection error: %s", err)
		return
	}

	log.Printf("handshaking with SSH")
	c.client, err = ssh.Client(c.conn, c.config.SSHConfig)
	if err != nil {
		log.Printf("handshake error: %s", err)
	}

	return
}
Exemplo n.º 14
0
func (conn *Conn) connect() (err error) {
	if conn.connected {
		panic("BUG: connect() called on connected socket/client!")
	}

	// dial manually so the tcp socket can be closed directly since it's hidden
	// if you use ssh.Dial, might also be handy for tuning?
	conn.netconn, err = net.Dial("tcp", conn.address)
	if err != nil {
		conn.connected = false
		return
	}

	conn.client, err = ssh.Client(conn.netconn, conn.config)
	if err != nil {
		conn.connected = false
		return
	}

	conn.Started = time.Now()
	conn.connected = true

	return
}
Exemplo n.º 15
0
// Creates a new packer.Communicator implementation over SSH. This takes
// an already existing TCP connection and SSH configuration.
func New(c net.Conn, config *ssh.ClientConfig) (result *comm, err error) {
	client, err := ssh.Client(c, config)
	result = &comm{client}
	return
}
Exemplo n.º 16
0
		if port != "" {
			arg = strings.Replace(arg, "%p", port, -1)
		}
		arg = strings.Replace(arg, "%r", config.User, -1)
		proxyCommand[i] = arg
	}
	client, server := net.Pipe()
	logger.Debugf(`executing proxy command %q`, proxyCommand)
	cmd := exec.Command(proxyCommand[0], proxyCommand[1:]...)
	cmd.Stdin = server
	cmd.Stdout = server
	cmd.Stderr = os.Stderr
	if err := cmd.Start(); err != nil {
		return nil, err
	}
	return ssh.Client(client, config)
}

func (c *goCryptoCommand) ensureSession() (*ssh.Session, error) {
	if c.sess != nil {
		return c.sess, nil
	}
	if len(c.signers) == 0 {
		return nil, fmt.Errorf("no private keys available")
	}
	if c.user == "" {
		currentUser, err := user.Current()
		if err != nil {
			return nil, fmt.Errorf("getting current user: %v", err)
		}
		c.user = currentUser.Username