密码登入:
package mainimport ("fmt""golang.org/x/crypto/ssh"
)func main() {// 目标服务器地址targetAddr := "ip:22"// 创建 SSH 客户端连接sshConfig := &ssh.ClientConfig{User: "root",Auth: []ssh.AuthMethod{ssh.Password("passwd"),},HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Ignore host key verification (unsafe)}sshClient, err := ssh.Dial("tcp", targetAddr, sshConfig)if err != nil {fmt.Println("Error creating SSH client:", err)return}defer sshClient.Close()// 在 SSH 连接上执行一些操作,这里以发送登入请求为例session, err := sshClient.NewSession()if err != nil {fmt.Println("Error creating SSH session:", err)return}defer session.Close()// 这里可以执行其他 SSH 操作,例如执行命令、传输文件等// 以下演示发送一个简单的命令output, err := session.CombinedOutput("ls -l")if err != nil {fmt.Println("Error executing command:", er