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.