func (s *server) TryDial(config *ssh.ClientConfig) (*ssh.Client, 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 conn, chans, reqs, err := ssh.NewClientConn(c1, "", config) if err != nil { return nil, err } return ssh.NewClient(conn, chans, reqs), nil }
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") sshConn, sshChan, req, err := ssh.NewClientConn(c.conn, c.address, c.config.SSHConfig) if err != nil { log.Printf("handshake error: %s", err) } if sshConn != nil { c.client = ssh.NewClient(sshConn, sshChan, req) } return }
func executeCmd(cmd, hostname string, config *ssh.ClientConfig, timeout time.Duration) (string, error) { // Dial up TCP connection to remote machine. conn, err := net.Dial("tcp", hostname+":22") if err != nil { return "", fmt.Errorf("Failed to ssh connect to %s. Make sure \"PubkeyAuthentication yes\" is in your sshd_config: %s", hostname, err) } defer util.Close(conn) util.LogErr(conn.SetDeadline(time.Now().Add(timeout))) // Create new SSH client connection. sshConn, sshChan, req, err := ssh.NewClientConn(conn, hostname+":22", config) if err != nil { return "", fmt.Errorf("Failed to ssh connect to %s: %s", hostname, err) } // Use client connection to create new client. client := ssh.NewClient(sshConn, sshChan, req) // Client connections can support multiple interactive sessions. session, err := client.NewSession() if err != nil { return "", fmt.Errorf("Failed to ssh connect to %s: %s", hostname, err) } var stdoutBuf bytes.Buffer session.Stdout = &stdoutBuf if err := session.Run(cmd); err != nil { return "", fmt.Errorf("Errored or Timeout out while running \"%s\" on %s: %s", cmd, hostname, err) } return stdoutBuf.String(), nil }
func Hop(through *ssh.Client, toaddr string, c *ssh.ClientConfig) (*ssh.Client, error) { hopconn, err := through.Dial("tcp", toaddr) if err != nil { return nil, err } conn, chans, reqs, err := ssh.NewClientConn(hopconn, toaddr, c) if err != nil { return nil, err } return ssh.NewClient(conn, chans, reqs), nil }
func TestSetupForwardAgent(t *testing.T) { a, b, err := netPipe() if err != nil { t.Fatalf("netPipe: %v", err) } defer a.Close() defer b.Close() _, socket, cleanup := startAgent(t) defer cleanup() serverConf := ssh.ServerConfig{ NoClientAuth: true, } serverConf.AddHostKey(testSigners["rsa"]) incoming := make(chan *ssh.ServerConn, 1) go func() { conn, _, _, err := ssh.NewServerConn(a, &serverConf) if err != nil { t.Fatalf("Server: %v", err) } incoming <- conn }() conf := ssh.ClientConfig{} conn, chans, reqs, err := ssh.NewClientConn(b, "", &conf) if err != nil { t.Fatalf("NewClientConn: %v", err) } client := ssh.NewClient(conn, chans, reqs) if err := ForwardToRemote(client, socket); err != nil { t.Fatalf("SetupForwardAgent: %v", err) } server := <-incoming ch, reqs, err := server.OpenChannel(channelType, nil) if err != nil { t.Fatalf("OpenChannel(%q): %v", channelType, err) } go ssh.DiscardRequests(reqs) agentClient := NewClient(ch) testAgentInterface(t, agentClient, testPrivateKeys["rsa"], nil) conn.Close() }
// Create a new NETCONF session using an existing net.Conn. func NewSSHSession(conn net.Conn, config *ssh.ClientConfig) (*Session, error) { var ( t TransportSSH err error ) c, chans, reqs, err := ssh.NewClientConn(conn, conn.RemoteAddr().String(), config) if err != nil { return nil, err } t.sshClient = ssh.NewClient(c, chans, reqs) err = t.setupSession() if err != nil { return nil, err } return NewSession(&t), nil }
func (s *sshServer) run(c *gc.C) { netconn, err := s.listener.Accept() c.Assert(err, gc.IsNil) defer func() { err = netconn.Close() c.Assert(err, gc.IsNil) }() conn, chans, reqs, err := cryptossh.NewServerConn(netconn, s.cfg) c.Assert(err, gc.IsNil) s.client = cryptossh.NewClient(conn, chans, reqs) var wg sync.WaitGroup defer wg.Wait() sessionChannels := s.client.HandleChannelOpen("session") c.Assert(sessionChannels, gc.NotNil) for newChannel := range sessionChannels { c.Assert(newChannel.ChannelType(), gc.Equals, "session") channel, reqs, err := newChannel.Accept() c.Assert(err, gc.IsNil) wg.Add(1) go func() { defer wg.Done() defer channel.Close() for req := range reqs { switch req.Type { case "exec": c.Assert(req.WantReply, jc.IsTrue) n := binary.BigEndian.Uint32(req.Payload[:4]) command := string(req.Payload[4 : n+4]) c.Assert(command, gc.Equals, testCommandFlat) req.Reply(true, nil) channel.Write([]byte("abc value\n")) _, err := channel.SendRequest("exit-status", false, cryptossh.Marshal(&struct{ n uint32 }{0})) c.Assert(err, gc.IsNil) return default: c.Fatalf("Unexpected request type: %v", req.Type) } } }() } }
func NewTunnelledSSHClient(user, tunaddr, tgtaddr string, checker *HostKeyChecker, agentForwarding bool, timeout time.Duration) (*SSHForwardingClient, error) { clientConfig, err := sshClientConfig(user, checker) if err != nil { return nil, err } tunaddr = maybeAddDefaultPort(tunaddr) tgtaddr = maybeAddDefaultPort(tgtaddr) var tunnelClient *gossh.Client dialFunc := func(echan chan error) { var err error tunnelClient, err = gossh.Dial("tcp", tunaddr, clientConfig) echan <- err } err = timeoutSSHDial(dialFunc, timeout) if err != nil { return nil, err } var targetConn net.Conn dialFunc = func(echan chan error) { tgtTCPAddr, err := net.ResolveTCPAddr("tcp", tgtaddr) if err != nil { echan <- err return } targetConn, err = tunnelClient.DialTCP("tcp", nil, tgtTCPAddr) echan <- err } err = timeoutSSHDial(dialFunc, timeout) if err != nil { return nil, err } c, chans, reqs, err := gossh.NewClientConn(targetConn, tgtaddr, clientConfig) if err != nil { return nil, err } return newSSHForwardingClient(gossh.NewClient(c, chans, reqs), agentForwarding) }
proxyCommand[i] = arg } client, server := net.Pipe() logger.Tracef(`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 } conn, chans, reqs, err := ssh.NewClientConn(client, addr, config) if err != nil { return nil, err } return ssh.NewClient(conn, chans, reqs), nil } 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