Skip to content

Commit

Permalink
更新ssh-agent和直接指定私钥登录方式共存,更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Jul 27, 2020
1 parent 068a6fc commit 9f99d65
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ AB两台服务器中的项目均将日志写到文件系统的`/home/data/logs/l
#tail_flags="--retry --follow=name"

# 服务器配置,可以配置多个
# 如果不提供password,则使用当前用户的ssh公钥,建议采用该方式,使用密码方式不安全
# 如果不提供 password, 则默认使用系统配置的 ssh-agent 设置,
# 你也可以通过指定 private_key_path 配置项来指定使用特定的私钥来登录 (private_key_path=/home/mylxsw/.ssh/id_rsa)
# server_name, hostname, user 配置为必选,其它可选
[servers]

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/mylxsw/remote-tail

go 1.14

require (
github.com/BurntSushi/toml v0.3.1
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
57 changes: 35 additions & 22 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,57 @@ type Client struct {
*ssh.Client
}

func (this *Client) Connect() error {
func (sshClient *Client) Connect() error {

conf := ssh.ClientConfig{
User: this.User,
User: sshClient.User,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if this.Password != "" {
conf.Auth = append(conf.Auth, ssh.Password(this.Password))
} else {
socket := os.Getenv("SSH_AUTH_SOCK")//if occcur error "Failed to open SSH_AUTH_SOCK: dial unix: missing address" , excute command: eval `ssh-agent`,and enter passphrase
if sshClient.Password != "" {
conf.Auth = append(conf.Auth, ssh.Password(sshClient.Password))
} else if sshClient.PrivateKeyPath != "" {
privateKey, err := getPrivateKey(sshClient.PrivateKeyPath)
if err != nil {
return err
}

conn, err := net.Dial("unix", socket)
conf.Auth = append(conf.Auth, privateKey)
} else {
// if occur error "Failed to open SSH_AUTH_SOCK: dial unix: missing address",
// execute command: eval `ssh-agent`,and enter passphrase
conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
log.Fatalf("Failed to open SSH_AUTH_SOCK: %v", err)
}

agentClient := agent.NewClient(conn)

if err != nil {
return err
}
conf.Auth = append(conf.Auth,
// Use a callback rather than PublicKeys so we only consult the
// agent once the remote server wants it.
ssh.PublicKeysCallback(agentClient.Signers),
)
// Use a callback rather than PublicKeys so we only consult the
// agent once the remote server wants it.
conf.Auth = append(conf.Auth, ssh.PublicKeysCallback(agentClient.Signers))
}
client, err := ssh.Dial("tcp", this.Host, &conf)
client, err := ssh.Dial("tcp", sshClient.Host, &conf)

if err != nil {
return fmt.Errorf("unable to connect: %v", err)
}

this.Client = client
sshClient.Client = client

return nil
}

// Close the connection
func (this *Client) Close() {
this.Client.Close()
func (sshClient *Client) Close() {
sshClient.Client.Close()
}

// Get the private key for current user
func getPrivateKey(privateKeyPath string) (ssh.AuthMethod, error) {
if privateKeyPath == "" {
privateKeyPath = filepath.Join(os.Getenv("HOME"), ".ssh/id_rsa")
if !fileExist(privateKeyPath) {
defaultPrivateKeyPath := filepath.Join(os.Getenv("HOME"), ".ssh/id_rsa")
log.Printf("Warning: private key path [%s] does not exist, using default %s instead", privateKeyPath, defaultPrivateKeyPath)

privateKeyPath = defaultPrivateKeyPath
}

key, err := ioutil.ReadFile(privateKeyPath)
Expand All @@ -89,3 +93,12 @@ func CreateTerminalModes() *ssh.TerminalModes {
ssh.TTY_OP_OSPEED: 14400,
}
}

func fileExist(path string) bool {
_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return false
}

return true
}

0 comments on commit 9f99d65

Please sign in to comment.