This commit is contained in:
insanity 2017-10-20 15:10:30 +09:00
commit 1076082050
4 changed files with 216 additions and 0 deletions

41
main.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"git.loafle.net/overflow/ssh_crawler/ssh"
"fmt"
)
func main() {
const ip = "192.168.1.15"
const port = "22"
const user = "administrator"
const pw = "!@#$qwer1234"
client, err := connectSSH(ip, port, user, pw)
if err != nil {
fmt.Errorf("%s", err)
}
res, err := client.CPUStat()
if err != nil {
fmt.Errorf("%s", err)
}
fmt.Printf("%s", res)
client.Close()
}
func connectSSH(ip, port, user, pw string) (*ssh.SSHClient, error){
client, err := ssh.New(ip, port, user, pw)
if err != nil {
return nil, err
}
return client, nil
}

100
ssh/ssh.go Normal file
View File

@ -0,0 +1,100 @@
package ssh
import (
"fmt"
"golang.org/x/crypto/ssh"
"bytes"
"git.loafle.net/overflow/ssh_crawler/stat"
"strconv"
)
type SSHConfig struct {
User string
Auth []ssh.AuthMethod
Host string
Port int
}
type SSHClient struct {
Session *ssh.Session
}
func New(ip, port, user, pw string) (*SSHClient, error) {
p, _ := strconv.Atoi(port)
info := &SSHConfig {
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(pw),
},
Host: ip,
Port: p,
}
session, err := info.Session()
if err != nil {
fmt.Errorf("%s", err)
return nil, err
}
return &SSHClient{
Session: session,
}, nil
}
func (info *SSHConfig) Session() (*ssh.Session, error) {
sshConfig := &ssh.ClientConfig{
User: info.User,
Auth: info.Auth,
HostKeyCallback:ssh.InsecureIgnoreHostKey(),
}
connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", info.Host, info.Port), sshConfig)
if err != nil {
return nil, err
}
session, err := connection.NewSession()
if err != nil {
return nil, err
}
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()
return nil, err
}
return session, nil
}
func (cli *SSHClient) Close() {
cli.Session.Close()
}
func (cli *SSHClient) RunCommand(command string) ([]byte, error) {
var b bytes.Buffer
cli.Session.Stdout = &b
err := cli.Session.Run(command)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (cli *SSHClient) CPUStat()([]stat.CPUStat, error) {
cpu := &stat.CPUStat{}
b, err := cli.RunCommand(cpu.Command())
if err != nil {
return nil, err
}
return cpu.Parse(bytes.NewReader(b))
}

67
stat/cpu.go Normal file
View File

@ -0,0 +1,67 @@
package stat
import (
"io"
"bufio"
"strings"
)
type CPUStat struct {
device,
user,
nice,
system,
idle,
iowait,
irq,
softIrq,
steal, // (over 2.6.11)
guest, // (over 2.6.24)
guestNice string //(over 2.6.33)
}
func (cpu CPUStat) Command() string {
return "cat /proc/stat"
}
func (cpu CPUStat) Parse(r io.Reader) ([]CPUStat, error) {
var (
CPUStats = []CPUStat{}
scanner = bufio.NewScanner(r)
)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if !strings.HasPrefix(parts[0], "cpu") {
continue
}
steal, guest, guestNice := "", "", ""
if len(parts) > 8 {
steal = parts[8]
}
if len(parts) > 9 {
guest = parts[9]
}
if len(parts) > 10 {
guestNice = parts[10]
}
CPUStats = append(CPUStats, CPUStat{
device: parts[0],
user: parts[1],
nice: parts[2],
system: parts[3],
idle: parts[4],
iowait: parts[5],
irq: parts[6],
softIrq: parts[7],
steal: steal,
guest: guest,
guestNice: guestNice,
})
}
return CPUStats, scanner.Err()
}

8
stat/stat.go Normal file
View File

@ -0,0 +1,8 @@
package stat
import "io"
type Stat interface {
Command() string
Parse(r io.Reader) ([]CPUStat, error)
}