ssh_crawler/ssh/ssh.go

110 lines
2.0 KiB
Go
Raw Permalink Normal View History

2017-10-20 06:10:30 +00:00
package ssh
import (
2017-10-23 09:08:19 +00:00
"bytes"
2017-10-20 06:10:30 +00:00
"fmt"
"golang.org/x/crypto/ssh"
"strconv"
2017-10-25 08:37:32 +00:00
"io/ioutil"
"time"
2017-10-20 06:10:30 +00:00
)
type SSHConfig struct {
User string
Auth []ssh.AuthMethod
Host string
Port int
}
type SSHClient struct {
2017-10-23 05:31:04 +00:00
session *ssh.Session
2017-10-23 09:08:19 +00:00
conf *SSHConfig
2017-10-20 06:10:30 +00:00
}
2017-10-25 08:37:32 +00:00
func New(ip, port, user, pw, keyFilePath string) (*SSHClient, error) {
2017-10-20 06:10:30 +00:00
p, _ := strconv.Atoi(port)
2017-10-25 08:37:32 +00:00
auth := make([]ssh.AuthMethod, 0)
if keyFilePath != "" {
key, err := parsePrivateKey(keyFilePath, pw)
if err != nil {
return nil, err
}
auth = append(auth, ssh.PublicKeys(key))
}
auth = append(auth, ssh.Password(pw))
2017-10-23 09:08:19 +00:00
conf := &SSHConfig{
2017-10-20 06:10:30 +00:00
User: user,
2017-10-25 08:37:32 +00:00
Auth: auth,
2017-10-20 06:10:30 +00:00
Host: ip,
Port: p,
}
2017-10-23 09:08:19 +00:00
return &SSHClient{conf: conf}, nil
2017-10-20 06:10:30 +00:00
}
2017-10-23 05:31:04 +00:00
func (cli *SSHClient) Session() error {
2017-10-20 06:10:30 +00:00
sshConfig := &ssh.ClientConfig{
2017-10-23 09:08:19 +00:00
User: cli.conf.User,
Auth: cli.conf.Auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
2017-10-25 08:37:32 +00:00
Timeout: time.Second * 10,
2017-10-20 06:10:30 +00:00
}
2017-10-23 05:31:04 +00:00
connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", cli.conf.Host, cli.conf.Port), sshConfig)
2017-10-20 06:10:30 +00:00
if err != nil {
2017-10-23 09:08:19 +00:00
return err
2017-10-20 06:10:30 +00:00
}
session, err := connection.NewSession()
if err != nil {
2017-10-23 05:31:04 +00:00
return err
2017-10-20 06:10:30 +00:00
}
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
session.Close()
2017-10-23 05:31:04 +00:00
return err
2017-10-20 06:10:30 +00:00
}
2017-10-23 05:31:04 +00:00
cli.session = session
2017-10-20 06:10:30 +00:00
2017-10-23 05:31:04 +00:00
return nil
2017-10-20 06:10:30 +00:00
}
func (cli *SSHClient) RunCommand(command string) ([]byte, error) {
2017-10-23 05:31:04 +00:00
if err := cli.Session(); err != nil {
2017-10-20 06:10:30 +00:00
return nil, err
}
2017-10-23 05:31:04 +00:00
var b bytes.Buffer
cli.session.Stdout = &b
2017-10-20 06:10:30 +00:00
2017-10-23 05:31:04 +00:00
err := cli.session.Run(command)
2017-10-20 06:10:30 +00:00
if err != nil {
return nil, err
}
2017-10-23 05:31:04 +00:00
cli.session.Close()
return b.Bytes(), nil
2017-10-20 06:10:30 +00:00
}
2017-10-25 08:37:32 +00:00
func parsePrivateKey(keyPath, pw string) (ssh.Signer, error) {
buff, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
if pw == "" {
return ssh.ParsePrivateKey(buff)
}
return ssh.ParsePrivateKeyWithPassphrase(buff, []byte(pw))
}