douquanhui5735 2018-08-22 17:09
浏览 179

带斜杠的GoLang SSH命令似乎立即失败

I'm currently writing an app that given a parameter, will run a command on a remote server. I'm using the /x/crypto/ssh package, everything seems to go smoothly if I use one liner commands like "who" or "ls", however, if I run a more complex command such as:

"grep SOMEDATA /var/log/logfile.log"

the program immediately exits and the command execution line with nothing more than "process exited with status 1", and I don't get anything else back.

If I check the history of the user I'm having it SSH into the remote system as, I do not see the command running at all.

Has anyone else run into this type of issue before? Here's a snippet of the code I'm using to execute this (sensitive data removed of course):

func returnData(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer
hostKey, err := getHostKey("SERVERNAME")
if err != nil {
    log.Fatalln(err)
}

err = r.ParseForm()
if err != nil {
    log.Fatalln(err)
}

config := &ssh.ClientConfig{
    User: "USERNAME",
    Auth: []ssh.AuthMethod{
        ssh.Password("TESTPASS"),
    },
    HostKeyCallback: ssh.FixedHostKey(hostKey),
}

client, err := ssh.Dial("tcp", "SERVERNAME:22", config)
if err != nil {
    log.Fatalln("Creating Client Failed: ", err)
}

session, err := client.NewSession()
if err != nil {
    log.Fatalln("Creating new Session Failed: ", err)
}

session.Stdout = &b
inputData := r.Form["fname"][0]
cmdExecute := fmt.Sprintf(`sudo grep %v /var/log/logfile.log`, inputData)
log.Println(cmdExecute)
if err := session.Run(cmdExecute); err != nil {
    log.Fatalln("Getting Data From session Failed: ", err)
    log.Fatalln(b.String())
}
//log.Println(hostKey)
defer session.Close()
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?