代码拉取完成,页面将自动刷新
package ssh
import (
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"io"
"net"
"os"
)
func WithKey(username string, path string, keyCallBack ssh.HostKeyCallback) (ssh.ClientConfig, error) {
signer, err := readKey(path)
if err != nil {
return ssh.ClientConfig{}, err
}
return AuthKey(username, []ssh.AuthMethod{ssh.PublicKeys(signer)}, keyCallBack)
}
func WithPassphrase(path string, passpharase []byte, username string, keyCallBack ssh.HostKeyCallback) (ssh.ClientConfig, error) {
signer, err := readKeyWithPassphrase(path, passpharase)
if err != nil {
return ssh.ClientConfig{}, err
}
return AuthKey(username, []ssh.AuthMethod{ssh.PublicKeys(signer)}, keyCallBack)
}
func WithAgent(username string, keyCallBack ssh.HostKeyCallback) (ssh.ClientConfig, error) {
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)
if err != nil {
return ssh.ClientConfig{}, err
}
client := agent.NewClient(conn)
return AuthKey(username, []ssh.AuthMethod{ssh.PublicKeysCallback(client.Signers)}, keyCallBack)
}
func WithPassword(username string, password string, keyCallBack ssh.HostKeyCallback) (ssh.ClientConfig, error) {
return AuthKey(username, []ssh.AuthMethod{ssh.Password(password)}, keyCallBack)
}
func AuthKey(username string, auths []ssh.AuthMethod, keyCallBack ssh.HostKeyCallback) (ssh.ClientConfig, error) {
return ssh.ClientConfig{
User: username,
Auth: auths,
HostKeyCallback: keyCallBack,
}, nil
}
func PublicKey(file string) ssh.AuthMethod {
signer, err := readKey(file)
if err != nil {
return nil
}
return ssh.PublicKeys(signer)
}
func PublicKeys(filenames []string) []ssh.AuthMethod {
var methods []ssh.AuthMethod
for _, filename := range filenames {
signer := PublicKey(filename)
if signer != nil {
methods = append(methods, signer)
}
}
return methods
}
func readKey(filename string) (ssh.Signer, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
bytes, _ := io.ReadAll(f)
return ssh.ParsePrivateKey(bytes)
}
func readKeyWithPassphrase(filename string, passpharase []byte) (ssh.Signer, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
bytes, _ := io.ReadAll(f)
return ssh.ParsePrivateKeyWithPassphrase(bytes, passpharase)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。