package main
import (
"fmt"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"time"
)
func main() {
// 创建SSH客户端配置
config := &ssh.ClientConfig{
Timeout: time.Second * 5, // 设置SSH连接超时时间为5秒
User: "username",
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 假设不验证host key
}
// 设置密码或者密钥认证
password := "password"
config.Auth = []ssh.AuthMethod{
ssh.Password(password),
}
// 建立SSH连接
sshClient, err := ssh.Dial("tcp", "remote.host:22", config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
defer sshClient.Close()
// 开启SFTP会话
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
panic("Failed to start SFTP subsystem: " + err.Error())
}
defer sftpClient.Close()
// 此处可以进行文件操作
// ...
}
0 条评论