fix
This commit is contained in:
parent
5993430397
commit
23d78b6f0e
@ -1,5 +1,5 @@
|
||||
appname = go_ops
|
||||
runmode = dev
|
||||
runmode = test
|
||||
RecoverPanic=true
|
||||
RouterCaseSensitive = true
|
||||
MaxMemory = 1 << 26
|
||||
|
@ -13,3 +13,21 @@ func (c *MainController) Get() {
|
||||
c.Data["Email"] = "pengtao@kingsome.com"
|
||||
c.TplName = "index.tpl"
|
||||
}
|
||||
|
||||
func (this *MainController) Post() {
|
||||
jsoninfo := this.GetString("jsoninfo")
|
||||
if jsoninfo == "" {
|
||||
this.Ctx.WriteString("jsoninfo is empty")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
type TestController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *TestController) Get() {
|
||||
c.Data["Website"] = "kingsome.me"
|
||||
c.Data["Email"] = "test@kingsome.com"
|
||||
c.TplName = "index.tpl"
|
||||
}
|
||||
|
8
go.mod
8
go.mod
@ -2,9 +2,15 @@ module go_ops
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/astaxie/beego v1.12.1
|
||||
require github.com/astaxie/beego v1.12.2
|
||||
|
||||
require (
|
||||
github.com/OwnLocal/goes v1.0.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
|
||||
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 // indirect
|
||||
github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373 // indirect
|
||||
github.com/smartystreets/goconvey v1.6.4
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
|
||||
)
|
||||
|
5
main.go
5
main.go
@ -2,10 +2,15 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
_ "go_ops/routers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := logs.NewLogger()
|
||||
log.SetLogger(logs.AdapterConsole)
|
||||
log.Debug("This is a debug message!")
|
||||
|
||||
beego.SetStaticPath("/down1", "download1")
|
||||
beego.Run()
|
||||
}
|
||||
|
@ -7,4 +7,9 @@ import (
|
||||
|
||||
func init() {
|
||||
beego.Router("/", &controllers.MainController{})
|
||||
//beego.Get("/test1", func(ctx *context.Context) {
|
||||
// ctx.Output.Body([]byte("helo world!"))
|
||||
//})
|
||||
beego.Router("/test", &controllers.TestController{})
|
||||
|
||||
}
|
||||
|
79
utils/remote_runcmd.go
Normal file
79
utils/remote_runcmd.go
Normal file
@ -0,0 +1,79 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cli struct {
|
||||
IP string //IP地址
|
||||
Username string //用户名
|
||||
Password string //密码
|
||||
Port int //端口号
|
||||
client *ssh.Client //ssh客户端
|
||||
LastResult string //最近一次Run的结果
|
||||
}
|
||||
|
||||
//创建命令行对象
|
||||
//@param ip IP地址
|
||||
//@param username 用户名
|
||||
//@param password 密码
|
||||
//@param port 端口号,默认22
|
||||
func New(ip string, username string, password string, port ...int) *Cli {
|
||||
cli := new(Cli)
|
||||
cli.IP = ip
|
||||
cli.Username = username
|
||||
cli.Password = password
|
||||
if len(port) <= 0 {
|
||||
cli.Port = 22
|
||||
} else {
|
||||
cli.Port = port[0]
|
||||
}
|
||||
return cli
|
||||
}
|
||||
|
||||
//执行shell
|
||||
//@param shell shell脚本命令
|
||||
func (c Cli) Run(shell string) (string, error) {
|
||||
if c.client == nil {
|
||||
if err := c.connect(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
session, err := c.client.NewSession()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer session.Close()
|
||||
buf, err := session.CombinedOutput(shell)
|
||||
|
||||
c.LastResult = string(buf)
|
||||
return c.LastResult, err
|
||||
}
|
||||
|
||||
//连接
|
||||
func (c *Cli) connect() error {
|
||||
config := ssh.ClientConfig{
|
||||
User: c.Username,
|
||||
Auth: []ssh.AuthMethod{ssh.Password(c.Password)},
|
||||
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||
return nil
|
||||
},
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%d", c.IP, c.Port)
|
||||
sshClient, err := ssh.Dial("tcp", addr, &config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.client = sshClient
|
||||
return nil
|
||||
}
|
||||
|
||||
func runcmd() {
|
||||
cli := New("192.168.100.21", "root", "kingsome2018", 22)
|
||||
output, err := cli.Run("free -h")
|
||||
fmt.Printf("%v\n%v", output, err)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user