golang's ssh dialing is working
This commit is contained in:
parent
0efb27374d
commit
d0869861ad
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
config.yaml
|
||||
.vscode/
|
19
main.go
19
main.go
@ -1,11 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//fmt.Println("Hello!")
|
||||
|
||||
//var config Configuration
|
||||
|
||||
config := initializeConfig("config.yaml")
|
||||
//fmt.Println(config.Hosts)
|
||||
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
30
ssh.go
@ -6,6 +6,12 @@ import (
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// modes := ssh.TerminalModes{
|
||||
// ssh.ECHO: 0,
|
||||
// ssh.TTY_OP_ISPEED: 14400,
|
||||
// ssh.TTY_OP_OSPEED: 14400,
|
||||
// }
|
||||
|
||||
type SSHConnection struct {
|
||||
Username string
|
||||
Password string
|
||||
@ -13,6 +19,8 @@ type SSHConnection struct {
|
||||
Hosts []string
|
||||
}
|
||||
|
||||
type SSHCleints []*ssh.Client
|
||||
|
||||
func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) {
|
||||
var sshConn SSHConnection
|
||||
sshConn.Username = config.Username
|
||||
@ -25,13 +33,29 @@ func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfi
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password(sshConn.Password),
|
||||
},
|
||||
HostKeyCallback: sshConn.getHostKeys(),
|
||||
}
|
||||
|
||||
return sshConn, sshConfig
|
||||
}
|
||||
|
||||
func (sshConn SSHConnection) DialConnection(sshConnec *ssh.ClientConfig) {
|
||||
for _, j := range sshConn.Hosts {
|
||||
fmt.Println(j)
|
||||
func (s SSHConnection) getHostKeys() ssh.HostKeyCallback {
|
||||
return ssh.InsecureIgnoreHostKey()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user