本文整理汇总了Golang中golang.org/x/crypto/ssh.Password函数的典型用法代码示例。如果您正苦于以下问题:Golang Password函数的具体用法?Golang Password怎么用?Golang Password使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Password函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: makeConfig
func makeConfig(user string, password string, privateKey string) (config *ssh.ClientConfig) {
if password == "" && user == "" {
log.Fatal("No password or private key available")
}
config = &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
if privateKey != "" {
log.Println(privateKey)
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
log.Fatalf("ssh.ParsePrivateKey error:%v", err)
}
clientkey := ssh.PublicKeys(signer)
config = &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
clientkey,
},
}
}
return
}
示例2: main
func main() {
if len(os.Args) != 5 {
fmt.Fprintf(os.Stderr, "Usage: %s <host> <username> <password> <remote name>\n",
os.Args[0])
os.Exit(1)
}
conn, err := sshcp.NewConn(
os.Args[1],
os.Args[2],
os.Args[4],
ssh.Password(os.Args[3]),
)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open connection: %s", err)
return
}
defer conn.Close()
_, err = io.Copy(conn, os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "error copying data: %s", err)
return
}
fmt.Fprintln(os.Stderr, "done")
}
示例3: NewNativeConfig
func NewNativeConfig(user string, auth *Auth) (ssh.ClientConfig, error) {
var (
authMethods []ssh.AuthMethod
)
for _, k := range auth.Keys {
key, err := ioutil.ReadFile(k)
if err != nil {
return ssh.ClientConfig{}, err
}
privateKey, err := ssh.ParsePrivateKey(key)
if err != nil {
return ssh.ClientConfig{}, err
}
authMethods = append(authMethods, ssh.PublicKeys(privateKey))
}
for _, p := range auth.Passwords {
authMethods = append(authMethods, ssh.Password(p))
}
return ssh.ClientConfig{
User: user,
Auth: authMethods,
}, nil
}
示例4: ExampleSession_RequestPty
func ExampleSession_RequestPty() {
// Create client config
config := &ssh.ClientConfig{
User: "username",
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)
}
}
示例5: connect
// 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
}
示例6: TestHandshakeTimeout
func TestHandshakeTimeout(t *testing.T) {
clientConfig := &ssh.ClientConfig{
User: "user",
Auth: []ssh.AuthMethod{
ssh.Password("pass"),
},
}
address := newMockBrokenServer(t)
conn := func() (net.Conn, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
t.Fatalf("unable to dial to remote side: %s", err)
}
return conn, err
}
config := &Config{
Connection: conn,
SSHConfig: clientConfig,
HandshakeTimeout: 50 * time.Millisecond,
}
_, err := New(address, config)
if err != ErrHandshakeTimeout {
// Note: there's another error that can come back from this call:
// ssh: handshake failed: EOF
// This should appear in cases where the handshake fails because of
// malformed (or no) data sent back by the server, but should not happen
// in a timeout scenario.
t.Fatalf("Expected handshake timeout, got: %s", err)
}
}
示例7: ExampleDial
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: "username",
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())
}
示例8: TestHandlerError
func (suite *ServerSuite) TestHandlerError() {
// Configure client connection
config := &ssh.ClientConfig{
User: "jonny.quest",
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()
}
示例9: DownLoadDirectoryRecurrsively
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
}
示例10: sshConfig
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
config := state.Get("config").(*Config)
var privateKey string
var auth []gossh.AuthMethod
if config.Comm.SSHPassword != "" {
auth = []gossh.AuthMethod{
gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
}
if config.Comm.SSHPrivateKey != "" {
if priv, ok := state.GetOk("privateKey"); ok {
privateKey = priv.(string)
}
signer, err := gossh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
if err != nil {
return nil, err
}
auth = append(auth, gossh.PublicKeys(signer))
}
return &gossh.ClientConfig{
User: config.Comm.SSHUsername,
Auth: auth,
}, nil
}
示例11: main
func main() {
config := &ssh.ClientConfig{
User: "meuUsuario",
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())
}
示例12: Connect
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
}
示例13: New
// New returns a new SFTP remote Cache implementated.
func New(server, username, password, key string) (cache.Cache, error) {
config := &ssh.ClientConfig{
Timeout: time.Minute * 5,
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
// private key authentication takes precedence
if key != "" {
signer, err := ssh.ParsePrivateKey([]byte(key))
if err != nil {
return nil, err
}
config.Auth[0] = ssh.PublicKeys(signer)
}
// create the ssh connection and client
client, err := ssh.Dial("tcp", server, config)
if err != nil {
return nil, err
}
// open the sftp session using the ssh connection
sftp, err := sftp.NewClient(client)
if err != nil {
client.Close()
return nil, err
}
return &cacher{sftp, client}, nil
}
示例14: ClientPassAuth
//ClientPassAuth provides a means of returning a ssh.Client and a Authentication function for a one time authentication procedure
func ClientPassAuth(addr string, dial, server time.Duration, cb MetaFunc) PasswordAuthCallback {
reqs := make(chan struct{})
return func(meta ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
config := &ssh.ClientConfig{}
config.User = meta.User()
config.Auth = []ssh.AuthMethod{
ssh.Password(string(pass)),
}
cl, err := DialClient(dial, server, addr, config, reqs)
if cb != nil {
cb(err, meta, pass, &SSHClient{
Client: cl,
Meta: meta,
Pass: pass,
})
}
if err != nil {
go func() {
reqs <- struct{}{}
}()
return nil, err
}
close(reqs)
return nil, nil
}
}
示例15: TestNew_Invalid
func TestNew_Invalid(t *testing.T) {
clientConfig := &ssh.ClientConfig{
User: "user",
Auth: []ssh.AuthMethod{
ssh.Password("i-am-invalid"),
},
}
address := newMockLineServer(t)
conn := func() (net.Conn, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
t.Errorf("Unable to accept incoming connection: %v", err)
}
return conn, err
}
config := &Config{
Connection: conn,
SSHConfig: clientConfig,
}
_, err := New(address, config)
if err == nil {
t.Fatal("should have had an error connecting")
}
}