本文整理汇总了Golang中golang.org/x/crypto/ssh/agent.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ListKeys
// ListKeys connects to the local SSH Agent and lists all the public keys
// loaded into it. It returns user friendly error message when it has trouble.
func ListKeys() ([]*agent.Key, error) {
sshAuthSock := os.Getenv("SSH_AUTH_SOCK")
if sshAuthSock == "" {
return nil, fmt.Errorf(
"The SSH_AUTH_SOCK environment variable is not set, which normally\n" +
"means that no SSH Agent is running.")
}
conn, err := net.Dial("unix", sshAuthSock)
if err != nil {
return nil, fmt.Errorf(
"Error connecting to agent: %s\n\n"+
"The agent address is detected using the SSH_AUTH_SOCK environment\n"+
"variable. Please verify this variable is correct and the SSH agent\n"+
"is properly set up.",
err)
}
defer conn.Close()
agent := agent.NewClient(conn)
loadedKeys, err := agent.List()
if err != nil {
return nil, fmt.Errorf("Error listing keys: %s", err)
}
return loadedKeys, err
}
示例2: connect
func (this *SSHExecutor) connect() error {
// auths holds the detected ssh auth methods
auths := []ssh.AuthMethod{}
// figure out what auths are requested, what is supported
if this.Password != "" {
auths = append(auths, ssh.Password(this.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()
}
config := &ssh.ClientConfig{
User: this.User,
Auth: auths,
}
client, err := ssh.Dial("tcp", this.Hostname+":"+strconv.Itoa(this.Port), config)
if err != nil {
return err
}
session, err := client.NewSession()
if err != nil {
return err
}
this.Session = session
return nil
}
示例3: Client
func (conf *SshConfig) Client() (*ssh.Client, error) {
auths := []ssh.AuthMethod{}
if !Empty(conf.Password) {
auths = append(auths, ssh.Password(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(conf.Key); err == nil {
auths = append(auths, ssh.PublicKeys(pubkey))
}
config := &ssh.ClientConfig{
User: conf.User,
Auth: auths,
}
client, err := ssh.Dial("tcp", conf.Name+":"+conf.Port, config)
if err != nil {
return nil, err
}
return client, nil
}
示例4: 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
}
示例5: initAuthMethod
// initAuthMethod initiates SSH authentication method.
func initAuthMethod() {
var signers []ssh.Signer
// If there's a running SSH Agent, try to use its Private keys.
sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err == nil {
agent := agent.NewClient(sock)
signers, _ = agent.Signers()
}
// Try to read user's SSH private keys form the standard paths.
files := []string{
os.Getenv("HOME") + "/.ssh/id_rsa",
os.Getenv("HOME") + "/.ssh/id_dsa",
}
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
continue
}
signer, err := ssh.ParsePrivateKey(data)
if err != nil {
continue
}
signers = append(signers, signer)
}
authMethod = ssh.PublicKeys(signers...)
}
示例6: Connection
func Connection(info ConnetctionInfo) *ssh.Client {
var auths []ssh.AuthMethod
auths = append(auths, privateKey...)
if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
}
auths = append(auths, ssh.Password(info.Passwd))
config := ssh.ClientConfig{
User: info.User,
Auth: auths,
}
for i := 0; i < 3; i++ {
//conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", info.IP, info.Port), &config)
conn, err := ssh.DialTimeOut("tcp", fmt.Sprintf("%s:%s", info.IP, info.Port), 30, &config)
if err == nil {
return conn
}
if i == 2 && err != nil {
ErrorList = append(ErrorList, fmt.Sprintf("连接%s失败:%s\n", info.IP, err))
return nil
}
time.Sleep(1e9)
}
return nil
}
示例7: main
func main() {
auth_socket := os.Getenv("SSH_AUTH_SOCK")
if auth_socket == "" {
log.Fatal(errors.New("no $SSH_AUTH_SOCK defined"))
}
conn, err := net.Dial("unix", auth_socket)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ag := agent.NewClient(conn)
auths := []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)}
config := &ssh.ClientConfig{
User: Username,
Auth: auths,
HostKeyCallback: KeyScanCallback,
}
var wg sync.WaitGroup
go out(&wg)
reader := bufio.NewReader(os.Stdin)
for {
server, err := reader.ReadString('\n')
if err == io.EOF {
break
}
server = server[:len(server)-1] // chomp
wg.Add(2) // dial and print
go dial(server, config, &wg)
}
wg.Wait()
}
示例8: sshClient
func sshClient(username, host string) (*ssh.Client, error) {
agentconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return nil, err
}
aclient := agent.NewClient(agentconn)
conf := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(aclient.Signers),
},
}
if strings.Index(host, ":") < 0 {
host = host + ":22"
}
cli, err := ssh.Dial("tcp", host, conf)
if err != nil {
return nil, err
}
return cli, nil
}
示例9: DialSSH
func DialSSH(addr string, username string, keyFilename string) (conn *SSHConn, err error) {
conn = &SSHConn{}
if keyFilename != "" {
pemKey, err := ioutil.ReadFile(keyFilename)
if err != nil {
return
}
signer, err := ssh.ParsePrivateKey(pemKey)
if err != nil {
return
}
conn.Auth = ssh.PublicKeys(signer)
} else {
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return
}
conn.Auth = ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
conn.Config = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
conn.Auth,
},
}
conn.Client, err = ssh.Dial("tcp", fmt.Sprintf("%s:22", addr), conn.Config)
return
}
示例10: New
// New returns a new agent.Agent and the (custom) connection it uses
// to communicate with a running pagent.exe instance (see README.md)
func New() (agent.Agent, net.Conn, error) {
if !Available() {
return nil, nil, errors.New("SSH agent requested but Pageant not running")
}
return agent.NewClient(&conn{}), nil, nil
}
示例11: ExampleClientAgent
func ExampleClientAgent() {
// ssh-agent has a UNIX socket under $SSH_AUTH_SOCK
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)
if err != nil {
log.Fatalf("net.Dial: %v", err)
}
agentClient := agent.NewClient(conn)
config := &ssh.ClientConfig{
User: "username",
Auth: []ssh.AuthMethod{
// Use a callback rather than PublicKeys
// so we only consult the agent once the remote server
// wants it.
ssh.PublicKeysCallback(agentClient.Signers),
},
}
sshc, err := ssh.Dial("tcp", "localhost:22", config)
if err != nil {
log.Fatalf("Dial: %v", err)
}
// .. use sshc
sshc.Close()
}
示例12: SSHAgent
func SSHAgent() ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}
示例13: NewSshExecWithAuth
func NewSshExecWithAuth(user string) *SshExec {
sshexec := &SshExec{}
authSocket := os.Getenv("SSH_AUTH_SOCK")
if authSocket == "" {
log.Fatal("SSH_AUTH_SOCK required, check that your ssh agent is running")
return nil
}
agentUnixSock, err := net.Dial("unix", authSocket)
if err != nil {
log.Fatal(err)
return nil
}
agent := agent.NewClient(agentUnixSock)
signers, err := agent.Signers()
if err != nil {
log.Fatal(err)
return nil
}
sshexec.clientConfig = &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signers...)},
}
return sshexec
}
示例14: Connect
func (sfs *SftpFileSystem) Connect() error {
var auths []ssh.AuthMethod
if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
}
if len(sfs.sp.Password) != 0 {
auths = append(auths, ssh.Password(sfs.sp.Password))
}
config := ssh.ClientConfig{
User: sfs.sp.Username,
Auth: auths,
}
var err error
sfs.sshClient, err = ssh.Dial("tcp", sfs.sp.Server, &config)
if err != nil {
log.Println("unable to connect to [%s]: %v", sfs.sp.Server, err)
return err
}
sfs.sftpClient, err = sftp.NewClient(sfs.sshClient)
if err != nil {
log.Println("unable to start sftp subsytem: %v", err)
return err
}
return nil
}
示例15: reconnect
func (ctx *ExecContext) reconnect() (err error) {
if ctx.hostname != "" {
ctx.isReconnecting = true
username := ctx.username
addr := fmt.Sprintf("%s:%d", ctx.hostname, ctx.port)
ctx.unlock()
agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
ctx.lock()
ctx.isReconnecting = false
return err
}
defer agentConn.Close()
ag := agent.NewClient(agentConn)
auths := []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)}
config := &ssh.ClientConfig{
User: username,
Auth: auths,
}
conn, err := net.DialTimeout("tcp", addr, networkTimeout)
if err != nil {
ctx.lock()
ctx.isReconnecting = false
return err
}
timeoutConn := &Conn{conn, networkTimeout, networkTimeout}
c, chans, reqs, err := ssh.NewClientConn(timeoutConn, addr, config)
if err != nil {
ctx.lock()
ctx.isReconnecting = false
return err
}
client := ssh.NewClient(c, chans, reqs)
// Send periodic keepalive messages
go func() {
t := time.NewTicker(networkTimeout / 2)
defer t.Stop()
for {
<-t.C
_, _, err := client.Conn.SendRequest("[email protected]", true, nil)
if err != nil {
ctx.lock()
if ctx.sshClient == client {
ctx.isConnected = false
}
ctx.unlock()
return
}
}
}()
ctx.lock()
ctx.isReconnecting = false
ctx.sshClient = client
}
ctx.isConnected = true
return nil
}