示例#1
0
文件: ssh.go 项目: npe9/minimega
func sshClientConnect(host, port, user, password string) (*sshConn, error) {
	sc := &sshConn{}
	sc.Config = &ssh.ClientConfig{
		User: user,
		Auth: []ssh.ClientAuth{
			ssh.ClientAuthPassword(sshPassword(password)),
		},
	}

	// url notation requires leading and trailing [] on ipv6 addresses
	dHost := host
	if isIPv6(dHost) {
		dHost = "[" + dHost + "]"
	}

	var err error
	sc.Client, err = ssh.Dial("tcp", dHost+":"+port, sc.Config)
	if err != nil {
		log.Print(err)
		return nil, err
	}

	sc.Session, err = sc.Client.NewSession()
	if err != nil {
		log.Print(err)
		return nil, err
	}

	//	sc.Session.Stdout = &sc.StdoutBuf
	sc.Stdout, err = sc.Session.StdoutPipe()
	if err != nil {
		log.Print(err)
		return nil, err
	}
	sc.Stdin, err = sc.Session.StdinPipe()
	if err != nil {
		log.Print(err)
		return nil, err
	}

	if err := sc.Session.Shell(); err != nil {
		log.Print(err)
		return nil, err
	}

	sc.Host = host

	return sc, nil
}
示例#2
0
文件: ssh.go 项目: npe9/minimega
func sshClientConnect(host string, protocol string) {
	sc := &sshConn{}
	sc.Config = &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.ClientAuth{
			ssh.ClientAuthPassword(sshPassword("password")),
		},
	}

	// url notation requires leading and trailing [] on ipv6 addresses
	dHost := host
	if isIPv6(dHost) {
		dHost = "[" + dHost + "]"
	}

	var err error
	sc.Client, err = ssh.Dial(protocol, dHost+PORT, sc.Config)
	if err != nil {
		log.Errorln(err)
		return
	}

	sc.Session, err = sc.Client.NewSession()
	if err != nil {
		log.Errorln(err)
		return
	}

	sc.Session.Stdout = &sc.StdoutBuf
	sc.Stdin, err = sc.Session.StdinPipe()
	if err != nil {
		log.Errorln(err)
		return
	}

	if err := sc.Session.Shell(); err != nil {
		log.Errorln(err)
		return
	}

	sc.Host = host

	sshConns = append(sshConns, sc)
}