package service import ( "fmt" "log" "path" "path/filepath" "testing" ) const ( username = "root" password = "Legu.cc()123" ip = "10.0.0.9" port = 22 cmd = "ls" cmds = "cd /home/liwei/go_dreamfactory&&git pull&&exit" //exit结尾,否则程序不会退出 ) func TestRunShell(t *testing.T) { ciphers := []string{} ssh := &SSHService{} err := ssh.Connect(username, password, ip, "", port, ciphers) if err != nil { t.Fatal(err) } // ssh.RunShell("cd /opt") ssh.RunShell(cmds) } func TestScp(t *testing.T) { ciphers := []string{} ssh := &SSHService{} err := ssh.Connect(username, password, ip, "", port, ciphers) if err != nil { t.Fatal(err) } path := filepath.Join(`E:/svn/dreamworks/client/dreamworks/ExcelFile/JSON/`, "game_vikingchallenge.json") fmt.Println(path) if err := ssh.ScpCopy(path, "/opt/json/"); err != nil { fmt.Println(err) } } func TestPath(t *testing.T) { Dir := `E:/svn/dreamworks/client/dreamworks/ExcelFile/JSON/` mypath := filepath.Join(Dir, "a.json") s := filepath.ToSlash(mypath) fmt.Println(path.Base(s)) } func TestMath(t *testing.T) { a := float64(10) / float64(3) fmt.Println(a) } func TestScpDown(t *testing.T) { ciphers := []string{} ssh := &SSHService{} err := ssh.Connect(username, password, ip, "", port, ciphers) if err != nil { t.Fatal(err) } sftpCli, err = ssh.getSftp() if err != nil { fmt.Errorf("new sftp client error: %w", err) } remoteFiles, err := sftpCli.ReadDir("/home/liwei/go_dreamfactory/bin/log") if err != nil { log.Println("scpCopy:", err) } for _, v := range remoteFiles { fmt.Println(v.Name()) if err := ssh.ScpDownload("G:/log/", "/home/liwei/go_dreamfactory/bin/log/"+v.Name()); err != nil { fmt.Println(err) } } }