You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/rmt/remoteCmdClient.go

70 lines
1.8 KiB
Go

package rmt
import (
"b612.me/notify"
"b612.me/notify/starnotify"
"b612.me/starlog"
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
"time"
)
var (
rmtRmt string
rmtCmd RmtCmd
)
func init() {
Cmdc.Flags().StringVarP(&rmtRmt, "remote", "r", "", "Remote Address")
Cmdc.Flags().StringVarP(&rmtPol, "protocol", "o", "tcp", "Remote protocol")
Cmdc.Flags().StringVarP(&rmtCmd.Cmd, "cmd", "c", "", "command")
Cmdc.Flags().StringVarP(&rmtCmd.WorkDir, "workdir", "w", "", "workdir")
Cmdc.Flags().StringSliceVarP(&rmtCmd.Env, "env", "e", []string{}, "env")
Cmdc.Flags().BoolVarP(&rmtCmd.Daemon, "daemon", "d", false, "daemon")
Cmdc.Flags().BoolVarP(&rmtCmd.Stream, "stream", "s", false, "stream")
Cmdc.Flags().StringVarP(&rmtListenPort, "port", "p", "5780", "Remote Port")
}
var Cmdc = &cobra.Command{
Use: "rmtc",
Short: "simple remote shell client",
Run: func(cmd *cobra.Command, args []string) {
if rmtRmt == "" {
starlog.Errorln("Please Enter Remote Path")
os.Exit(1)
}
err := starnotify.NewClient("c").Connect(rmtPol, fmt.Sprintf("%s:%s", rmtRmt, rmtListenPort))
if err != nil {
starlog.Errorln("Create Remote Failed", err)
os.Exit(2)
}
starnotify.C("c").SetLink("stream", RmtSteam)
defer starnotify.C("c").Stop()
cdata, err := starnotify.C("c").SendWaitObj("cmd", rmtCmd, time.Second*3600)
if err != nil {
starlog.Errorln("Got Answer Failed:", err)
os.Exit(3)
}
data, err := cdata.Value.ToInterface()
if err != nil {
starlog.Errorln("Decode FAILED:", err)
os.Exit(4)
}
rtnData, ok := data.(RmtCmdBack)
if !ok {
starlog.Errorln("Decode FAILED2:", err)
os.Exit(5)
}
fmt.Println("Return Pid:", rtnData.RetCode)
if !rmtCmd.Stream {
fmt.Println("Return OutPut\n", rtnData.OutPut)
}
},
}
func RmtSteam(msg *notify.Message) {
fmt.Println(strings.TrimSpace(msg.Value.MustToString()))
}