golang's ssh dialing is working

This commit is contained in:
Wyatt J. Miller 2019-08-14 18:31:13 -04:00
parent 0efb27374d
commit d0869861ad
5 changed files with 47 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
config.yaml config.yaml
.vscode/

19
main.go
View File

@ -1,11 +1,28 @@
package main package main
import (
"fmt"
"golang.org/x/crypto/ssh"
)
func main() { func main() {
//fmt.Println("Hello!") //fmt.Println("Hello!")
//var config Configuration //var config Configuration
config := initializeConfig("config.yaml") config := initializeConfig("config.yaml")
//fmt.Println(config.Hosts)
sshConn, sshConfig := initializeConnection(config) sshConn, sshConfig := initializeConnection(config)
sshConn.DialConnection(sshConfig) //fmt.Println(sshConn.Hosts)
clientConns := sshConn.dialConnection(sshConfig)
fmt.Println(clientConns)
sshSuccess, err := ssh.Dial("tcp", "crash.local:22", sshConfig)
if err != nil {
panic(err)
}
fmt.Println(sshSuccess)
} }

30
ssh.go
View File

@ -6,6 +6,12 @@ import (
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
// modes := ssh.TerminalModes{
// ssh.ECHO: 0,
// ssh.TTY_OP_ISPEED: 14400,
// ssh.TTY_OP_OSPEED: 14400,
// }
type SSHConnection struct { type SSHConnection struct {
Username string Username string
Password string Password string
@ -13,6 +19,8 @@ type SSHConnection struct {
Hosts []string Hosts []string
} }
type SSHCleints []*ssh.Client
func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) { func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) {
var sshConn SSHConnection var sshConn SSHConnection
sshConn.Username = config.Username sshConn.Username = config.Username
@ -25,13 +33,29 @@ func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfi
Auth: []ssh.AuthMethod{ Auth: []ssh.AuthMethod{
ssh.Password(sshConn.Password), ssh.Password(sshConn.Password),
}, },
HostKeyCallback: sshConn.getHostKeys(),
} }
return sshConn, sshConfig return sshConn, sshConfig
} }
func (sshConn SSHConnection) DialConnection(sshConnec *ssh.ClientConfig) { func (s SSHConnection) getHostKeys() ssh.HostKeyCallback {
for _, j := range sshConn.Hosts { return ssh.InsecureIgnoreHostKey()
fmt.Println(j)
} }
func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints {
ClientConn := SSHCleints{}
for _, j := range s.Hosts {
hostPort := fmt.Sprintf("%s:22", j)
connection, err := ssh.Dial("tcp", hostPort, sshConfig)
if err != nil {
fmt.Errorf("Can't connect", err)
}
ClientConn = append(ClientConn, connection)
}
return ClientConn
} }