Esempio n. 1
0
func (conn *Conn) Connect() error {
	var auth []ssh.ClientAuth

	// only load a private key if requested ~/.ssh/id_rsa is _not_ loaded automatically
	// ssh-agent should be the usual path
	if conn.Key != "" {
		kr := new(keyring)
		if err := kr.loadPEM(conn.Key); err != nil {
			log.Fatal("Couldn't load specified private key '", conn.Key, "': ", err)
		}
		auth = append(auth, ssh.ClientAuthKeyring(kr))
	}

	agentSock := os.Getenv("SSH_AUTH_SOCK")

	// ssh-agent support, might need to reuse this in the future?
	// how bad are 100's or 1000's of connections to the agent?
	if agentSock != "" {
		sock, err := net.Dial("unix", agentSock)
		if err != nil {
			log.Fatal("Could not connect to SSH_AUTH_SOCK. Is ssh-agent running?")
		}

		agent := ssh.NewAgentClient(sock)
		auth = append(auth, ssh.ClientAuthAgent(agent))
	}

	conn.config = &ssh.ClientConfig{
		User: conn.User,
		Auth: auth,
	}

	return conn.connect()
}
Esempio n. 2
0
func RunProgram(name string, conf map[string]interface{}) *Program {
	programs := conf["programs"].(map[string]interface{})
	progConfig := (programs[name]).(map[string]interface{})
	logMaxSize := int(conf["log.maxsize"].(float64))
	// construct the program structure
	program := Program{
		Name:   name,
		Config: conf,
		Outch:  make(chan string),
		Errch:  make(chan string),
		quit:   make(chan bool),
		config: progConfig,
		host:   progConfig["host"].(string),
		user:   progConfig["user"].(string),
		local:  localCommands(progConfig["local"].([]interface{})),
		remote: remoteCommands(progConfig["remote"].([]interface{})),
		outlog: &Log{lines: make([]string, logMaxSize)},
		errlog: &Log{lines: make([]string, logMaxSize)},
	}

	// ssh-agen
	agent_sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err != nil {
		panic(err)
	}
	defer agent_sock.Close()

	// ssh-client
	config := &ssh.ClientConfig{
		User: program.user,
		Auth: []ssh.ClientAuth{
			ssh.ClientAuthAgent(ssh.NewAgentClient(agent_sock)),
		},
	}
	dest := program.host + ":22"
	program.client, err = ssh.Dial("tcp", dest, config)
	if err != nil {
		panic(err)
	}
	go program.runProgram()
	return &program
}
Esempio n. 3
0
func makeConfig() *ssh.ClientConfig {
	clientAuth := []ssh.ClientAuth{}

	sshAuthSock := os.Getenv("SSH_AUTH_SOCK")
	if sshAuthSock != "" {
		for {
			sock, err := net.Dial("unix", sshAuthSock)
			if err != nil {
				netErr := err.(net.Error)
				if netErr.Temporary() {
					time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
					continue
				}

				reportErrorToUser("Cannot open connection to SSH agent: " + netErr.Error())
			} else {
				agent := ssh.NewAgentClient(sock)
				identities, err := agent.RequestIdentities()
				if err != nil {
					reportErrorToUser("Cannot request identities from ssh-agent: " + err.Error())
				} else if len(identities) > 0 {
					clientAuth = append(clientAuth, ssh.ClientAuthAgent(agent))
				}
			}

			break
		}
	}

	if haveKeyring {
		clientAuth = append(clientAuth, keyring)
	}

	return &ssh.ClientConfig{
		User: user,
		Auth: clientAuth,
	}
}
Esempio n. 4
0
func main() {
	if len(os.Args) != 4 {
		log.Fatalln("usage: go-ssh <username> <host>:<port> <cmd>")
	}

	agent, e := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if e != nil {
		panic(e)
	}
	defer agent.Close()

	auths := []ssh.ClientAuth{
		ssh.ClientAuthAgent(ssh.NewAgentClient(agent)),
	}

	clientConfig := &ssh.ClientConfig{
		User: os.Args[1],
		Auth: auths,
	}

	client, err := ssh.Dial("tcp", os.Args[2], clientConfig)
	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()

	session.Stdout = os.Stdout
	session.Stderr = os.Stderr

	if err := session.Run(os.Args[3]); err != nil {
		panic("Failed to run: " + err.Error())
	}
}
Esempio n. 5
0
func (c *Client) Connect() (e error) {
	if c.Port == 0 {
		c.Port = 22
	}
	var auths []ssh.ClientAuth

	if c.password != "" {
		auths = append(auths, ssh.ClientAuthPassword(c))
	}

	if c.Agent, e = net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); e == nil {
		auths = append(auths, ssh.ClientAuthAgent(ssh.NewAgentClient(c.Agent)))
	}

	config := &ssh.ClientConfig{
		User: c.User,
		Auth: auths,
	}
	c.Conn, e = ssh.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port), config)
	if e != nil {
		return e
	}
	return nil
}
Esempio n. 6
0
File: main.go Progetto: kr/sftp
func main() {
	var auths []ssh.ClientAuth
	if agent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.ClientAuthAgent(ssh.NewAgentClient(agent)))
	}
	if *PASS != "" {
		auths = append(auths, ssh.ClientAuthPassword(password(*PASS)))
	}

	config := ssh.ClientConfig{
		User: *USER,
		Auth: auths,
	}
	addr := fmt.Sprintf("%s:%d", *HOST, *PORT)
	conn, err := ssh.Dial("tcp", addr, &config)
	if err != nil {
		log.Fatalf("unable to connect to [%s]: %v", addr, err)
	}
	defer conn.Close()

	client, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatalf("unable to start sftp subsytem: %v", err)
	}
	defer client.Close()
	switch cmd := flag.Args()[0]; cmd {
	case "ls":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		walker := client.Walk(flag.Args()[1])
		for walker.Step() {
			if err := walker.Err(); err != nil {
				log.Println(err)
				continue
			}
			fmt.Println(walker.Path())
		}
	case "fetch":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		f, err := client.Open(flag.Args()[1])
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		if _, err := io.Copy(os.Stdout, f); err != nil {
			log.Fatal(err)
		}
	case "put":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		f, err := client.Create(flag.Args()[1])
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		if _, err := io.Copy(f, os.Stdin); err != nil {
			log.Fatal(err)
		}
	case "stat":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		f, err := client.Open(flag.Args()[1])
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		fi, err := f.Stat()
		if err != nil {
			log.Fatalf("unable to stat file: %v", err)
		}
		fmt.Printf("%s %d %v\n", fi.Name(), fi.Size(), fi.Mode())
	case "rm":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		if err := client.Remove(flag.Args()[1]); err != nil {
			log.Fatalf("unable to remove file: %v", err)
		}
	case "mv":
		if len(flag.Args()) < 3 {
			log.Fatalf("%s %s: old and new name required", cmd, os.Args[0])
		}
		if err := client.Rename(flag.Args()[1], flag.Args()[2]); err != nil {
			log.Fatalf("unable to rename file: %v", err)
		}
	default:
		log.Fatal("unknown subcommand: %v", cmd)
	}
}