go客户端ssh交换机
连接交换机代码
使用库golang.org/x/crypto/ssh
func (s *SwitchConf) switchNewSession() error {config := &ssh.ClientConfig{User: s.User,Auth: []ssh.AuthMethod{ssh.Password(s.Password),},HostKeyCallback: ssh.InsecureIgnoreHostKey(),Timeout: 30 * time.Second, // 设置超时时间Config: ssh.Config{ Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "aes256-gcm@openssh.com", "chacha20-poly1305@openssh.com"},}, //添加了很多加密方式,为了应对不同的密码规则}address := fmt.Sprintf("%s:22", s.Ip)// 建立 SSH 连接client, err := ssh.Dial("tcp", address, config)if err != nil {log.Fatalf("Failed to dial: %v", err)return nil}//defer client.Close()go s.waitConnectClosed(client)// 创建会话session, err := client.NewSession()if err != nil {fmt.Println("Failed to create session:", err)return nil}s.Session = session///设置terminalmodes的方式modes := ssh.TerminalModes{ssh.ECHO: 0, // disable echoingssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaudssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud}//建立伪终端err := s.Session.RequestPty("xterm", 80, 80, modes)if err != nil {fmt.Println("创建requestpty出错", err)return err}// 使用 io.MultiWriter 同时写入 os.Stdout 和缓冲区//multiWriter := io.MultiWriter(os.Stdout, &buf)s.input, err = s.Session.StdinPipe()if err != nil {fmt.Println("StdinPipe错误: ", err)return err}s.output, err = s.Session.StdoutPipe()if err != nil {fmt.Println("StdoutPipe: ", err)return err}s.errput, err = s.Session.StderrPipe()if err != nil {fmt.Println("StderrPipe: ", err)return err}err = s.Session.Shell()if err != nil {fmt.Println("创建shell出错: ", err)return err}go s.getErrorRealTime()return nil
}
问题
提示unable to authenticate, attempted methods [none password], no supported methods remain
原因:密码错误