Linux 如何向SSH提供密码?

Linux 如何向SSH提供密码?,linux,unix,ssh,go,Linux,Unix,Ssh,Go,我需要使用经过密码验证的scp从服务器下载文件。如何使用Go执行此操作?尝试了以下代码,但未传入密码 package main import ( "os/exec" "time" ) func main() { password := "password" cmd := exec.Command("scp", "admin@192.168.1.150:file", "file") in, err := cmd.StdinPipe() if e

我需要使用经过密码验证的scp从服务器下载文件。如何使用Go执行此操作?尝试了以下代码,但未传入密码

package main

import (
    "os/exec"
    "time"
)

func main() {
    password := "password"
    cmd := exec.Command("scp", "admin@192.168.1.150:file", "file")

    in, err := cmd.StdinPipe()
    if err != nil {
        panic(err)
    }

    defer in.Close()

    out, err := cmd.StdoutPipe()
    if err != nil {
        panic(err)
    }

    defer out.Close()

    if err = cmd.Run(); err != nil {
        panic(err)
    }

    go func() {
        time.Sleep(10 * time.Second)
        _, err = in.Write([]byte(password + "\n"))
        if err != nil {
            panic(err)
        }
    }()
}
编辑:我最终使用了gexpect(github.com/ThomasRooney/gexpect)库


这个自我回答问题的答案可能会有帮助:


至少,他在回答中提到他“能够使用密码获得ssh访问”,这在问题中没有明确提及-这就是为什么你在搜索网站时可能找不到它的原因?

不要这样做;为公钥/私钥配置
ssh
。在某些情况下,如果您能提供一些帮助,这将非常有用:)这样做的问题是,密码提示不是指向stdin/stdout,而是指向/dev/tty设备。也许可以使用伪终端连接解决这个问题,但我还没有看到它完成。@ThomasDickey有趣的是,你能告诉我更多关于如何实现这个问题的信息吗?解释会很长(我不使用go编程,我的解释需要一些修改才能对你有用)。我用strace检查了我对scp使用/dev/tty的记忆。在使用SSH库时,它连接到服务器并从服务器调用scp。我在服务器上没有可用的scp。@user2509594:要使scp正常工作,您必须在服务器上有scp。scp在ssh上工作,但技术上与之无关,而且双方都需要scp二进制文件,因为它是通过
ssh hostname scp-t
package main

import (
    "github.com/ThomasRooney/gexpect"
    "log"
)

func main() {
    child, err := gexpect.Spawn("scp admin@192.168.1.150:file file")
    if err != nil {
        log.Fatalln(err)
    }
    child.Expect("password:")
    child.SendLine("password")
    child.Interact()
    child.Close()
}