如何解决无法发送数据包:sftp出现EOF错误
我正在Go中使用SFTP模块。我的代码能够轮询和读取某些文件,但是在特定阶段之后面临此错误。读取文件后,我将对另一个应用程序进行HTTP调用以传递文件。 HTTP呼叫的超时为120秒,但我尚未为ssh / sftp客户端设置任何超时。发布我正在使用的代码。帮我弄清楚为什么会发生此错误。 http调用多次失败,导致每次等待120秒。我想念什么吗?
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func getFiles() {
sshconfig.SetDefaults()
cipherOrder := sshconfig.Ciphers
sshconfig.Ciphers = append(cipherOrder,"aes256-cbc","3des-cbc")
config := &ssh.ClientConfig{
User: user,HostKeyCallback: ssh.InsecureIgnoreHostKey(),Auth: []ssh.AuthMethod{
ssh.Password(pwd),},Config: sshconfig,}
// connect to the server
client,err := ssh.Dial("tcp",host,config)
if err != nil {
log.Println("Failed to dial")
panic(err)
}
log.Println("SUCCESSFULLY CONNECTED TO SFTP SERVER")
// open an SFTP session over an existing ssh connection.
sftp,err := sftp.NewClient(client)
if err != nil {
log.Println("Failed to establish an SSH session!")
panic(err)
}
log.Println("Established an SSH session!")
defer sftp.Close()
//read files from the root directory
files,err = sftp.ReadDir(folder)
if err != nil {
log.Println("Unable to read sftp directory")
panic(err)
}
log.Println("OPENED DIRECTORY")
for file := range files {
fileName := files[file].Name()
log.Println("Found file inside dir: ",fileName)
// Open the source file
srcFile,err := sftp.Open(folder + fileName)
if err != nil {
log.Println("Unable to open file") //it is failing here
panic(err) // failed to send packet: EOF
}
log.Println("Opened file")
//create file in input directory
dstFile,err := os.Create(inputDest + fileName)
if err != nil {
log.Println("Unable to create destination file")
panic(err)
}
log.Println("Created destination file")
// defer dstFile.Close()
// Copy the file
srcFile.WriteTo(dstFile)
log.Println("Copied contents from source to destination file.")
dstFile.Close()
srcFile.Close()
//read file to bytes
bodyBytes,err := ioutil.ReadFile(inputDest + fileName)
if err != nil {
log.Println("Unable to read file to parse!")
panic(err)
}
log.Println("Read file to pass on to second application!")
//Http call with timeout 120s
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。