func sshExec(client *ssh.Client, cmd string) (string, string, int, error) { framework.Logf("Executing '%s' on %v", cmd, client.RemoteAddr()) session, err := client.NewSession() if err != nil { return "", "", 0, fmt.Errorf("error creating session to host %s: '%v'", client.RemoteAddr(), err) } defer session.Close() // Run the command. code := 0 var bout, berr bytes.Buffer session.Stdout, session.Stderr = &bout, &berr err = session.Run(cmd) if err != nil { // Check whether the command failed to run or didn't complete. if exiterr, ok := err.(*ssh.ExitError); ok { // If we got an ExitError and the exit code is nonzero, we'll // consider the SSH itself successful (just that the command run // errored on the host). if code = exiterr.ExitStatus(); code != 0 { err = nil } } else { // Some other kind of error happened (e.g. an IOError); consider the // SSH unsuccessful. err = fmt.Errorf("failed running `%s` on %s: '%v'", cmd, client.RemoteAddr(), err) } } return bout.String(), berr.String(), code, err }
func CopyFile(conn *ssh.Client, FileName, DirectoryPath string) bool { defer conn.Close() if !strings.HasSuffix(DirectoryPath, "/") { DirectoryPath = DirectoryPath + "/" } con, err := sftp.NewClient(conn, sftp.MaxPacket(5e9)) if err != nil { color.Red("%s传输文件新建会话错误: %s\n", conn.RemoteAddr(), err) return false } sFile, _ := os.Open(FileName) defer sFile.Close() dFile := DirectoryPath + FileName fmt.Printf("%s 目标路径:%s\n", conn.RemoteAddr(), dFile) File, err := con.OpenFile(dFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR) if err != nil { color.Red("%s 创建文件%s错误: %s \n", conn.RemoteAddr(), dFile, err) return false } defer File.Close() for { buf := make([]byte, 1024) n, err := sFile.Read(buf) if err != nil { if err.Error() == "EOF" { break } return false } File.Write(buf[:n]) } Result <- fmt.Sprintf("上传%s到%s成功.\n", FileName, conn.RemoteAddr()) return true }
func writeRemoteFile(sshClient *ssh.Client, data, dir, fileName string, mode os.FileMode) error { Logf(fmt.Sprintf("Writing remote file '%s/%s' on %v", dir, fileName, sshClient.RemoteAddr())) session, err := sshClient.NewSession() if err != nil { return fmt.Errorf("error creating session to host %s: '%v'", sshClient.RemoteAddr(), err) } defer session.Close() fileSize := len(data) go func() { // ignore errors here. scp whould return errors if something goes wrong. pipe, _ := session.StdinPipe() defer pipe.Close() fmt.Fprintf(pipe, "C%#o %d %s\n", mode, fileSize, fileName) io.Copy(pipe, strings.NewReader(data)) fmt.Fprint(pipe, "\x00") }() if err := session.Run(fmt.Sprintf("scp -t %s", dir)); err != nil { return err } return nil }
func writeRemoteFile(sshClient *ssh.Client, data, dir, fileName string, mode os.FileMode) error { framework.Logf(fmt.Sprintf("Writing remote file '%s/%s' on %v", dir, fileName, sshClient.RemoteAddr())) session, err := sshClient.NewSession() if err != nil { return fmt.Errorf("error creating session to host %s: '%v'", sshClient.RemoteAddr(), err) } defer session.Close() fileSize := len(data) pipe, err := session.StdinPipe() if err != nil { return err } defer pipe.Close() if err := session.Start(fmt.Sprintf("scp -t %s", dir)); err != nil { return err } fmt.Fprintf(pipe, "C%#o %d %s\n", mode, fileSize, fileName) io.Copy(pipe, strings.NewReader(data)) fmt.Fprint(pipe, "\x00") pipe.Close() return session.Wait() }
func scp(Client *ssh.Client, File io.Reader, size int64, path string) { filename := filepath.Base(path) dirname := strings.Replace(filepath.Dir(path), "\\", "/", -1) defer Client.Close() session, err := Client.NewSession() if err != nil { fmt.Println("创建Session失败:", err) return } go func() { w, _ := session.StdinPipe() fmt.Fprintln(w, "C0644", size, filename) io.CopyN(w, File, size) fmt.Fprint(w, "\x00") w.Close() }() if err := session.Run(fmt.Sprintf("/usr/bin/scp -qrt %s", dirname)); err != nil { fmt.Println("执行scp命令失败:", err) session.Close() return } else { fmt.Printf("%s 发送成功.\n", Client.RemoteAddr()) session.Close() } if session, err = Client.NewSession(); err == nil { defer session.Close() buf, err := session.Output(fmt.Sprintf("/usr/bin/md5sum %s", path)) if err != nil { fmt.Println("检查md5失败:", err) return } fmt.Printf("%s 的MD5:\n%s\n", Client.RemoteAddr(), string(buf)) } }
func Run(Con *ssh.Client, cmd string) { defer Con.Close() s, err := Con.NewSession() if err != nil { color.Red("%s:新建会话失败.命令未执行.", Con.RemoteAddr()) return } fmt.Printf("成功连接:%s\n", Con.RemoteAddr()) buf, err := s.Output(cmd) if err != nil { color.Red("%s:命令执行失败.", Con.RemoteAddr()) return } str := fmt.Sprintf("%s 的执行结果:\n%s\n", Con.RemoteAddr().String(), string(buf)) fmt.Println(str) Result <- str }