douqinlin5094 2019-08-29 14:55
浏览 94

如何通过golang标准ssh库成功调用.exe?

These days there is a task where I need to send a ssh command based on a Golang program (you can deem it as a simple main.go file that includes a ssh function) to invoke a .exe application (i.e. agent.exe) compiled by Golang in Windows Server 2012.

That is to say, main funciton (golang ssh library) --ssh connection(start agent.exe command)-->agent.exe--agent.exe is running.

However, if I use a very simple command, such as tasklist, it works. But it would go wrong when I use a much complex command as cmd /C start agent.exe, and the agent.exe is not running.

Why I can't get the right result? cmd /C start agent.exe can always work in cmd.

here is the simply ssh code

package main

import (
    "fmt"
    "golang.org/x/crypto/ssh"
    "golang.org/x/crypto/ssh/terminal"
    "io"
    "net"
    "os"
    "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 main() {
    cli := New("IP", "username", "password", port)
    ***output, err := cli.Run("cmd /C start C:/opt/app/agent.exe")***
    fmt.Printf("%v
%v", output, err)
}

Then I go build it on Windows. The following is a daemon exe file. It's made of golang and is compiled by "go build -ldflags “-H=windowsgui”. No matter what I do, using the main.go can't start the agent.exe on a windows server while running the command in the cmd is just ok. It's hard for me to understand the reasons. enter image description here

enter image description here

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路
    • ¥15 MATLAB报错输入参数太多
    • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入