Skip to content

Commit

Permalink
Merge pull request #6 from datalandfill/add-privatekey-with-passphare
Browse files Browse the repository at this point in the history
add support passpharse
  • Loading branch information
sfreiberg authored Jul 19, 2022
2 parents 495cbb8 + a5226bd commit fff00fd
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions simplessh.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,37 @@ func ConnectWithKeyFileTimeout(host, username, privKeyPath string, timeout time.
return ConnectWithKeyTimeout(host, username, string(privKey), timeout)
}

// Connect with a private key with passphrase. If privKeyPath is an empty string it will attempt
// to use $HOME/.ssh/id_rsa. If username is empty simplessh will attempt to get the current user.
func ConnectWithKeyFilePassphraseTimeout(host, username, privKeyPath string, passPhrase string, timeout time.Duration) (*Client, error) {
if privKeyPath == "" {
currentUser, err := user.Current()
if err == nil {
privKeyPath = filepath.Join(currentUser.HomeDir, ".ssh", "id_rsa")
}
}
pemKey, err := ioutil.ReadFile(privKeyPath)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKeyWithPassphrase(pemKey, []byte(passPhrase))
if err != nil {
return nil, err
}

return ConnectWithKeyPassphraseTimeout(host, username, signer, timeout)
}

// Same as ConnectWithKeyFile but allows a custom timeout. If username is empty simplessh will attempt to get the current user.
func ConnectWithKeyFile(host, username, privKeyPath string) (*Client, error) {
return ConnectWithKeyFileTimeout(host, username, privKeyPath, DefaultTimeout)
}

// KeyFile with a passphrase
func ConnectWithKeyFilePassphrase(host, username, privKeyPath string, passPhrase string) (*Client, error) {
return ConnectWithKeyFilePassphraseTimeout(host, username, privKeyPath, passPhrase, DefaultTimeout)
}

// Connect with a private key with a custom timeout. If username is empty simplessh will attempt to get the current user.
func ConnectWithKeyTimeout(host, username, privKey string, timeout time.Duration) (*Client, error) {
signer, err := ssh.ParsePrivateKey([]byte(privKey))
Expand All @@ -100,6 +126,13 @@ func ConnectWithKeyTimeout(host, username, privKey string, timeout time.Duration
return connect(username, host, authMethod, timeout)
}

// Connect with a private key with passphrase with a custom timeout. If username is empty simplessh will attempt to get the current user.
func ConnectWithKeyPassphraseTimeout(host, username string, signer ssh.Signer, timeout time.Duration) (*Client, error) {
authMethod := ssh.PublicKeys(signer)

return connect(username, host, authMethod, timeout)
}

// Connect with a private key. If username is empty simplessh will attempt to get the current user.
func ConnectWithKey(host, username, privKey string) (*Client, error) {
return ConnectWithKeyTimeout(host, username, privKey, DefaultTimeout)
Expand Down

0 comments on commit fff00fd

Please sign in to comment.