Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hostkey #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ Do not need to think about Dials, sessions, defers and public keys...Let easyssh

## So easy to use!

[Run a command on remote server and get STDOUT output](https://github.com/hypersleep/easyssh/blob/master/example/run.go)
[Run a command on remote server and get STDOUT output](https://github.com/pnrmx/easyssh/blob/master/example/run.go)

[Upload a file to remote server](https://github.com/hypersleep/easyssh/blob/master/example/scp.go)
[Upload a file to remote server](https://github.com/pnrmx/easyssh/blob/master/example/scp.go)
46 changes: 38 additions & 8 deletions easyssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ import (
// Note: easyssh looking for private key in user's home directory (ex. /home/john + Key).
// Then ensure your Key begins from '/' (ex. /.ssh/id_rsa)
type MakeConfig struct {
User string
Server string
Key string
Port string
Password string
User string
Server string
Key string
Port string
Password string
FixedHost string
}

// returns ssh.Signer from user you running app home path + cutted key path.
// (ex. pubkey,err := getKeyFile("/.ssh/id_rsa") )
func getKeyFile(keypath string) (ssh.Signer, error) {
usr, err := user.Current()
usr, err := user.Current() //returns user{uid,gid,username,name,homedir}
if err != nil {
return nil, err
}
Expand All @@ -56,6 +57,28 @@ func getKeyFile(keypath string) (ssh.Signer, error) {
return pubkey, nil
}

// ex. getHostKey ("/.ssh/known_hosts")
func getHostKey(hostpath string) (ssh.PublicKey, error) {

usr, err := user.Current() //returns user{uid,gid,username,name,homedir}
if err != nil {
return nil, err
}

file := usr.HomeDir + hostpath
buf, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

_, _, hostkey, _, _, err := ssh.ParseKnownHosts(buf)
if err != nil {
return nil, err
}

return hostkey, nil
}

// connects to remote server using MakeConfig struct and returns *ssh.Session
func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
// auths holds the detected ssh auth methods
Expand All @@ -75,9 +98,16 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
auths = append(auths, ssh.PublicKeys(pubkey))
}

hostkey, err := getHostKey(ssh_conf.FixedHost)
if err != nil {
return nil, err
}
hostCallBack := ssh.FixedHostKey(hostkey)

config := &ssh.ClientConfig{
User: ssh_conf.User,
Auth: auths,
User: ssh_conf.User,
Auth: auths,
HostKeyCallback: hostCallBack,
}

client, err := ssh.Dial("tcp", ssh_conf.Server+":"+ssh_conf.Port, config)
Expand Down
1 change: 1 addition & 0 deletions example/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
// Optional key or Password without either we try to contact your agent SOCKET
//Password: "password",
Key: "/.ssh/id_rsa",
FixedHost: "/.ssh/known_hosts",
Port: "22",
}

Expand Down
1 change: 1 addition & 0 deletions example/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func main() {
User: "root",
Server: "example.com",
Password: "123qwe",
FixedHost: "/.ssh/known_hosts",
Port: "22",
}

Expand Down