package bridge
import (
"net"
"strings"
"time"
"github.com/avast/retry-go"
"github.com/limanmys/render-engine/pkg/helpers"
"golang.org/x/crypto/ssh"
)
func InitShellWithPassword (username , password , host , port string ) (*ssh .Client , error ) {
config := &ssh .ClientConfig {
User : username ,
Auth : []ssh .AuthMethod {
ssh .Password (password ),
},
HostKeyCallback : ssh .InsecureIgnoreHostKey (),
Timeout : time .Second * 5 ,
}
ipAddress , err := helpers .ResolveIP (host )
if err != nil {
return nil , err
}
var conn *ssh .Client
err = retry .Do (
func () error {
conn , err = ssh .Dial ("tcp" , net .JoinHostPort (ipAddress , port ), config )
if err != nil {
if strings .Contains (err .Error(), "unable to authenticate" ) {
return retry .Unrecoverable (err )
}
return err
}
return nil
},
retry .Attempts (5 ),
retry .Delay (1 *time .Second ),
)
if err != nil {
return nil , err
}
return conn , nil
}
func InitShellWithCert (username , certificate , host , port string ) (*ssh .Client , error ) {
key , err := ssh .ParsePrivateKey ([]byte (certificate ))
if err != nil {
return nil , err
}
config := &ssh .ClientConfig {
User : username ,
Auth : []ssh .AuthMethod {
ssh .PublicKeys (key ),
},
HostKeyCallback : ssh .InsecureIgnoreHostKey (),
Timeout : time .Second * 5 ,
}
ipAddress , err := helpers .ResolveIP (host )
if err != nil {
return nil , err
}
var conn *ssh .Client
err = retry .Do (
func () error {
conn , err = ssh .Dial ("tcp" , net .JoinHostPort (ipAddress , port ), config )
if err != nil {
if strings .Contains (err .Error(), "unable to authenticate" ) {
return retry .Unrecoverable (err )
}
return err
}
return nil
},
retry .Attempts (5 ),
retry .Delay (1 *time .Second ),
)
return conn , nil
}
func VerifySSH (username , password , host , port string ) bool {
config := &ssh .ClientConfig {
User : username ,
Auth : []ssh .AuthMethod {
ssh .Password (password ),
},
HostKeyCallback : ssh .InsecureIgnoreHostKey (),
Timeout : time .Second * 5 ,
}
ipAddress , err := helpers .ResolveIP (host )
if err != nil {
return false
}
var conn *ssh .Client
err = retry .Do (
func () error {
conn , err = ssh .Dial ("tcp" , net .JoinHostPort (ipAddress , port ), config )
if err != nil {
if strings .Contains (err .Error(), "unable to authenticate" ) {
return retry .Unrecoverable (err )
}
return err
}
return nil
},
retry .Attempts (5 ),
retry .Delay (1 *time .Second ),
)
defer conn .Close ()
return true
}
func VerifySSHCertificate (username , certificate , host , port string ) bool {
key , err := ssh .ParsePrivateKey ([]byte (certificate ))
if err != nil {
return false
}
config := &ssh .ClientConfig {
User : username ,
Auth : []ssh .AuthMethod {
ssh .PublicKeys (key ),
},
HostKeyCallback : ssh .InsecureIgnoreHostKey (),
Timeout : time .Second * 5 ,
}
ipAddress , err := helpers .ResolveIP (host )
if err != nil {
return false
}
var conn *ssh .Client
err = retry .Do (
func () error {
conn , err = ssh .Dial ("tcp" , net .JoinHostPort (ipAddress , port ), config )
if err != nil {
if strings .Contains (err .Error(), "unable to authenticate" ) {
return retry .Unrecoverable (err )
}
return err
}
return nil
},
retry .Attempts (5 ),
retry .Delay (1 *time .Second ),
)
if err != nil {
return false
}
defer conn .Close ()
return true
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .