-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add password provider to SSH connection (#63)
* Add password provider to SSH connection This allows to implement a custom password provider (e.g. user input) for provate keys which require a password * Fix ssh-agent usage * rearrange code * smaller fixes
- Loading branch information
Showing
2 changed files
with
101 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"syscall" | ||
|
||
"github.com/k0sproject/rig" | ||
"golang.org/x/crypto/ssh/terminal" | ||
) | ||
|
||
/* | ||
This example shows how to use a key password provider | ||
*/ | ||
|
||
func main() { | ||
user := flag.String("user", "root", "SSH User") | ||
host := flag.String("host", "localhost", "Host") | ||
flag.Parse() | ||
conn := rig.Connection{ | ||
SSH: &rig.SSH{ | ||
User: *user, | ||
Address: *host, | ||
PasswordCallback: func() (string, error) { | ||
fmt.Println("Enter password:") | ||
pass, err := terminal.ReadPassword(int(syscall.Stdin)) | ||
return string(pass), err | ||
}, | ||
}, | ||
} | ||
if err := conn.Connect(); err != nil { | ||
panic(err) | ||
} | ||
defer conn.Disconnect() | ||
output, err := conn.ExecOutput("ls -al") | ||
if err != nil { | ||
panic(err) | ||
} | ||
println(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters