func getSftpClient(conf Config) []*sftp.Client { // process the keyfile buf, err := ioutil.ReadFile(conf.KeyFile) if err != nil { log.Fatalf("error in reading private key file %s\n", err) } key, err := ssh.ParsePrivateKey(buf) if err != nil { log.Fatalf("error in parsing private key %s\n", key) } // client config config := &ssh.ClientConfig{ User: conf.User, Auth: []ssh.AuthMethod{ssh.PublicKeys(key)}, } // connection clients := make([]*sftp.Client, 0) for _, r := range conf.Remotes { c, err := ssh.Dial("tcp", r, config) if err != nil { log.Fatalf("error in ssh connection %s\n", err) } // sftp handler sftp, err := sftp.NewClient(c) if err != nil { log.Fatalf("error in sftp connection %s\n", err) } clients = append(clients, sftp) } return clients }
func (c *DockerDeployClient) copyFile(source string, target string) error { sftp, err := sftp.NewClient(c.sshClient) if err != nil { log.Fatalf("Could not initialize SFTP connection: %v", err) } defer sftp.Close() tf, err := sftp.Create(target) if err != nil { return err } defer tf.Close() sf, err := os.Open(source) if err != nil { return err } defer sf.Close() n, err := io.Copy(tf, sf) if err != nil { return err } log.Printf("Artifact from %v to %v copied. %v Bytes transferred.", source, target, n) return nil }
func (sfs *SftpFileSystem) Connect() error { var auths []ssh.AuthMethod if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil { auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers)) } if len(sfs.sp.Password) != 0 { auths = append(auths, ssh.Password(sfs.sp.Password)) } config := ssh.ClientConfig{ User: sfs.sp.Username, Auth: auths, } var err error sfs.sshClient, err = ssh.Dial("tcp", sfs.sp.Server, &config) if err != nil { log.Println("unable to connect to [%s]: %v", sfs.sp.Server, err) return err } sfs.sftpClient, err = sftp.NewClient(sfs.sshClient) if err != nil { log.Println("unable to start sftp subsytem: %v", err) return err } return nil }
func (c *SftpClient) Connect() error { auth := []ssh.AuthMethod{} if c.authMethod == "key" { key, _ := c.GetKey(c.keyPath) auth = []ssh.AuthMethod{ ssh.PublicKeys(key), } } else if c.authMethod == "password" { auth = []ssh.AuthMethod{ ssh.Password(c.password), } } config := &ssh.ClientConfig{ User: c.username, Auth: auth, } sHost := strings.Join([]string{c.hostname, strconv.FormatInt(c.port, 10)}, ":") sshClient, err := ssh.Dial("tcp", sHost, config) if err != nil { return err } sftpClient, err := sftp.NewClient(sshClient) if err == nil { c.Client = sftpClient } return err }
func New(snapshot map[string]time.Time, ctx context.Context, config *Config, remoteRoot string) (Poller, error) { sshConn, err := ssh.Dial("tcp", config.HostPort, config.Ssh) if err != nil { glog.Fatalf("cannot ssh.Dial: %s", err) return nil, err } client, err := sftp.NewClient(sshConn) if err != nil { sshConn.Close() glog.Fatalf("cannot create sftp client: %s", err) return nil, err } p := &poller{ root: remoteRoot, config: config, conn: sshConn, client: client, updatesChan: make(chan *Update), snapshot: snapshot, resumes: make(map[string]int64), } go p.loop(ctx) glog.V(2).Infoln("poller: started") return p, nil }
// Sftp uploads sourceFile to remote machine func (ssh_conf *MakeConfig) Sftp(src, dst string) error { session, err := ssh_conf.connect() if err != nil { ssh_conf.close() return err } defer session.Close() ssh_conf.SftpClient, err = sftp.NewClient(ssh_conf.Client) if err != nil { return err } defer func() { ssh_conf.SftpClient.Close() ssh_conf.SftpClient = nil }() sf, err := os.Open(src) if err != nil { return err } defer sf.Close() f, err := ssh_conf.SftpClient.Create(dst) if err != nil { return err } defer f.Close() if _, err := io.Copy(f, sf); err != nil { return err } return nil }
//FS creates a ftp client on the remote connection and allows file base op func (h *SSHProtocolLink) FS() flux.ActionStackInterface { req := flux.NewAction() eo := flux.NewAction() st := flux.NewActionStackBy(req, eo) ch := req.Chain(2) ch.OverrideBefore(1, func(b interface{}, next flux.ActionInterface) { ce, ok := b.(*sftp.Client) if !ok { return } ce.Close() }) cl, err := sftp.NewClient(h.conn) if err != nil { st.Complete(err) } else { st.Complete(cl) } return st }
// New returns a new SFTP remote Cache implementated. func New(server, username, password, key string) (cache.Cache, error) { config := &ssh.ClientConfig{ Timeout: time.Minute * 5, User: username, Auth: []ssh.AuthMethod{ ssh.Password(password), }, } // private key authentication takes precedence if key != "" { signer, err := ssh.ParsePrivateKey([]byte(key)) if err != nil { return nil, err } config.Auth[0] = ssh.PublicKeys(signer) } // create the ssh connection and client client, err := ssh.Dial("tcp", server, config) if err != nil { return nil, err } // open the sftp session using the ssh connection sftp, err := sftp.NewClient(client) if err != nil { client.Close() return nil, err } return &cacher{sftp, client}, nil }
// UploadFile uploads a local file to the given location on the server. func (s *Server) UploadFile(source string, dest string) (err error) { sshClient, err := s.SSHClient() if err != nil { return } client, err := sftp.NewClient(sshClient) if err != nil { return } defer client.Close() // create all parent dirs of dest err = s.MkDir(dest) if err != nil { return } // open source, create dest sourceFile, err := os.Open(source) if err != nil { return } defer sourceFile.Close() destFile, err := client.Create(dest) if err != nil { return } // copy the file content over _, err = io.Copy(destFile, sourceFile) return }
// CreateFile creates a new file with the given content on the server. func (s *Server) CreateFile(content string, dest string) (err error) { sshClient, err := s.SSHClient() if err != nil { return } client, err := sftp.NewClient(sshClient) if err != nil { return } defer client.Close() // create all parent dirs of dest err = s.MkDir(dest) if err != nil { return } // create dest destFile, err := client.Create(dest) if err != nil { return } // write the content _, err = io.WriteString(destFile, content) return }
func (p *SSHPath) newClient() (*sftp.Client, error) { if !p.sudo { sftpClient, err := sftp.NewClient(p.client) if err != nil { return nil, fmt.Errorf("error creating sftp client: %v", err) } return sftpClient, nil } else { s, err := p.client.NewSession() if err != nil { return nil, fmt.Errorf("error creating sftp client (in new-session): %v", err) } //if err := s.R("sftp"); err != nil { // return nil, fmt.Errorf("error creating sftp client (in new-session): %v", err) //} stdin, err := s.StdinPipe() if err != nil { return nil, fmt.Errorf("error creating sftp client (at stdin pipe): %v", err) } stdout, err := s.StdoutPipe() if err != nil { return nil, fmt.Errorf("error creating sftp client (at stdout pipe): %v", err) } err = s.Start("sudo /usr/lib/openssh/sftp-server") if err != nil { return nil, fmt.Errorf("error creating sftp client (executing 'sudo /usr/lib/openssh/sftp-server'): %v", err) } return sftp.NewClientPipe(stdout, stdin) } }
func Rollback(rollback int, deploy_config setup.DeployConfiguration, commands setup.CommandConfig) { /* remove the current directory, and switch back to version current - number */ connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath) if err != nil { log.Panic(err) } sftp, err := sftp.NewClient(connection) if err != nil { log.Fatal(err) } defer sftp.Close() var build_list []string w, err := sftp.ReadDir(deploy_config.TargetDirectory) for _, folder := range w { if folder.Name() == "current" || !folder.IsDir() { continue } build_list = append(build_list, folder.Name()) } sort.Sort(ByDate(build_list)) rollback_version := build_list[len(build_list)-rollback] //list is ordered old to new, so we want to index in relation to the end of the list. //Create a symbolic link to the new directory. cmd := fmt.Sprintf("unlink %s", deploy_config.TargetDirectory+"/current") ExecuteCmd(connection, cmd, nil, nil) cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+rollback_version, deploy_config.TargetDirectory+"/current") ExecuteCmd(connection, cmd, nil, nil) }
func Example(conn *ssh.ClientConn) { // open an SFTP sesison over an existing ssh connection. sftp, err := sftp.NewClient(conn) if err != nil { log.Fatal(err) } defer sftp.Close() // walk a directory w := sftp.Walk("/home/user") for w.Step() { if w.Err() != nil { continue } log.Println(w.Path()) } // leave your mark f, err := sftp.Create("hello.txt") if err != nil { log.Fatal(err) } if _, err := f.Write([]byte("Hello world!")); err != nil { log.Fatal(err) } // check it's there fi, err := sftp.Lstat("hello.txt") if err != nil { log.Fatal(err) } log.Println(fi) }
func DownLoadDirectoryRecurrsively(hostAndPort string, username string, password string, remoteSourceDirectory string, localTargetDirectory string) error { remoteSourceDirectoryLength := len(remoteSourceDirectory) authMethodSlice := make([]ssh.AuthMethod, 0) authMethodSlice = append(authMethodSlice, ssh.Password(password)) clientConfig := ssh.ClientConfig{ User: username, Auth: authMethodSlice, } connection, err := ssh.Dial("tcp", hostAndPort, &clientConfig) if err != nil { return err } defer connection.Close() // open an SFTP session over an existing ssh connection. client, err := sftp.NewClient(connection) if err != nil { return err } defer client.Close() // walk a directory walk := client.Walk(remoteSourceDirectory) for walk.Step() { if err := walk.Err(); err != nil { return err } if walk.Stat().IsDir() { directoryPath := localTargetDirectory + walk.Path()[remoteSourceDirectoryLength:] if err := os.MkdirAll(directoryPath, os.ModePerm); err != nil { return err } } else { filePath := localTargetDirectory + walk.Path()[remoteSourceDirectoryLength:] file, err := client.Open(walk.Path()) if err != nil { return err } defer file.Close() outputFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm) if err != nil { return err } defer outputFile.Close() _, err = file.WriteTo(outputFile) if err != nil { return err } } } return nil }
func (i *blueBoxInstance) UploadScript(ctx gocontext.Context, script []byte) error { client, err := i.sshClient(ctx) if err != nil { return err } defer client.Close() sftp, err := sftp.NewClient(client) if err != nil { return err } defer sftp.Close() _, err = sftp.Lstat("build.sh") if err == nil { return ErrStaleVM } f, err := sftp.Create("build.sh") if err != nil { return err } _, err = f.Write(script) if err != nil { return err } return err }
func (c *Communicator) Put(local string, remote string) error { client, err := c.Client() if err != nil { return err } sc, err := sftp.NewClient(client) if err != nil { return err } defer sc.Close() b, err := ioutil.ReadFile(local) if err != nil { return err } fo, err := sc.Create(remote) if err != nil { return err } defer fo.Close() _, err = fo.Write(b) if err != nil { return err } return nil }
func (c *Communicator) Get(remote string, local string) error { client, err := c.Client() if err != nil { return err } sc, err := sftp.NewClient(client) if err != nil { return err } defer sc.Close() fi, err := sc.Open(remote) if err != nil { return err } defer fi.Close() fo, err := os.Create(local) if err != nil { return err } defer fo.Close() _, err = io.Copy(fo, fi) if err != nil { return err } return nil }
func NewSFTPClient(addr string, config *ssh.ClientConfig) (*sftp.Client, error) { conn, err := ssh.Dial("tcp", addr, config) if err != nil { return nil, err } return sftp.NewClient(conn) }
func (c *sshClientImpl) UploadFile(username string, password string, ip string, srcFile string, destFile string) error { config := &myssh.ClientConfig{ User: username, Auth: []myssh.AuthMethod{ myssh.Password(password), }, } if !IsIP(ip) { return errors.New("invalid IP address") } if IsDir(srcFile) || IsDir(destFile) { return errors.New("Is a directory") } client, err := myssh.Dial("tcp", ip+":22", config) if err != nil { log.Fatal(err) return err } defer client.Close() sftp, err := sftp.NewClient(client) if err != nil { log.Fatal(err) return err } defer sftp.Close() data, err := ioutil.ReadFile(srcFile) if err != nil { log.Fatal(err) return err } f, err := sftp.Create(destFile) if err != nil { log.Fatal(err) return err } defer f.Close() _, err = f.Write([]byte(data)) if err != nil { log.Fatal(err) return err } _, err = sftp.Lstat(destFile) if err != nil { log.Fatal(err) return err } return nil }
func UploadFile(state multistep.StateBag, localFilename string, remoteFilename string, allowExecute bool) error { ui := state.Get("ui").(packer.Ui) config := state.Get("commonconfig").(CommonConfig) // Setup connection config sshConfig := &gossh.ClientConfig{ User: config.Username, Auth: []gossh.AuthMethod{ gossh.Password(config.Password), }, } sshClient, err := gossh.Dial("tcp", config.HostIp+":22", sshConfig) if err != nil { ui.Error(fmt.Sprintf("Error connecting to host. '%s'.", err)) return err } defer sshClient.Close() sftpClient, err := sftp.NewClient(sshClient) if err != nil { ui.Error(fmt.Sprintf("Error obtaining sftp client. '%s'.", err)) return err } defer sftpClient.Close() sourceScript, err := os.Open(localFilename) if err != nil { ui.Error(fmt.Sprintf("Error opening local file '%s'. '%s'.", localFilename, err)) return err } defer sourceScript.Close() destScript, err := sftpClient.Create(remoteFilename) if err != nil { ui.Error(fmt.Sprintf("Error creating file '%s' on remote host. '%s'.", remoteFilename, config.HostIp, err)) return err } _, err = io.Copy(destScript, sourceScript) if err != nil { ui.Error(fmt.Sprintf("Error copying file '%s' to remote host. '%s'.", localFilename, config.HostIp, err)) return err } if allowExecute { err := sftpClient.Chmod(remoteFilename, 555) if err != nil { ui.Error(fmt.Sprintf("Error setting permissions on file '%s' on remote host. '%s'.", remoteFilename, config.HostIp, err)) return err } } return nil }
func (s *SFTPConnnection) init(ss *SSHConnection) error { s.ssh = ss client, err := sftp.NewClient(ss.client) if err != nil { log.Fatal(err) return err } s.client = client return nil }
func upload(addr, dir string, long bool, config *ssh.ClientConfig, files []string) (string, error) { if len(files) == 0 { return "", errors.New("nothing to upload") } for _, name := range files { if fi, err := os.Stat(name); err != nil { return "", err } else if fi.IsDir() { return "", errors.New("upload of directories not yet implemented") } } conn, err := ssh.Dial("tcp", addr, config) if err != nil { return "", err } defer conn.Close() sc, err := sftp.NewClient(conn) if err != nil { return "", err } defer sc.Close() if di, err := sc.Stat(dir); err != nil { return "", err } else if !di.IsDir() { return "", errors.New("destination path is not a directory") } var dst string if long { for i := 0; i < 3; i++ { dst = path.Join(dir, fmt.Sprintf("%x", randBytes())) if err = sc.Mkdir(dst); err == nil { break } } } else { for b, i := randBytes(), 1; i < len(b); i++ { dst = path.Join(dir, fmt.Sprintf("%x", b[:i])) if err = sc.Mkdir(dst); err == nil { break } } } if err != nil { return "", errors.New("failed to create new random-named directory, probably too many already exist") } for _, name := range files { if err := uploadFile(name, dst, sc); err != nil { return dst, err } } return dst, nil }
/* Create a new version, remove oldest if there are more than *Rollback return the new version number so that the deploy knows where to place shit. */ func create_version(deploy_config setup.DeployConfiguration) string { connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath) if err != nil { log.Panic(err) } sftp, err := sftp.NewClient(connection) if err != nil { log.Fatal(err) } defer sftp.Close() new_version := time.Now().Format("20060102150405") var build_list []string w, err := sftp.ReadDir(deploy_config.TargetDirectory) for _, folder := range w { if folder.Name() == "current" || !folder.IsDir() { continue } build_list = append(build_list, folder.Name()) } sort.Sort(ByDate(build_list)) //Remove oldest in build_list if len(build_list)+1 > deploy_config.Rollbacks+1 { remove_list := build_list[:len(build_list)-deploy_config.Rollbacks] //remove any old that exceeds Rollbaks, which are in the start of the list for _, version := range remove_list { log.Printf("Removing version: %s\n", deploy_config.TargetDirectory+"/"+version) //err :=sftp.Remove(deploy_config.TargetDirectory + "/" + version) cmd := fmt.Sprintf("rm -r %s", deploy_config.TargetDirectory+"/"+version) err := ExecuteCmd(connection, cmd, nil, nil) if err != nil { log.Println(err) } } } log.Printf("Creating folder: %s\n", deploy_config.TargetDirectory+"/"+new_version) sftp.Mkdir(deploy_config.TargetDirectory + "/" + new_version) //Create a symbolic link to the new directory. cmd := fmt.Sprintf("unlink %s", deploy_config.TargetDirectory+"/current") //log.Printf("Removing old symlink: %s", cmd) ExecuteCmd(connection, cmd, nil, nil) cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+new_version, deploy_config.TargetDirectory+"/current") //log.Printf("Creating symlink: %s", cmd) ExecuteCmd(connection, cmd, nil, nil) return new_version }
func main() { var auths []ssh.AuthMethod if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil { auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers)) } if *PASS != "" { auths = append(auths, ssh.Password(*PASS)) } config := ssh.ClientConfig{ User: *USER, Auth: auths, } addr := fmt.Sprintf("%s:%d", *HOST, *PORT) conn, err := ssh.Dial("tcp", addr, &config) if err != nil { log.Fatalf("unable to connect to [%s]: %v", addr, err) } defer conn.Close() c, err := sftp.NewClient(conn, sftp.MaxPacket(*SIZE)) if err != nil { log.Fatalf("unable to start sftp subsytem: %v", err) } defer c.Close() w, err := c.OpenFile("/dev/null", syscall.O_WRONLY) if err != nil { log.Fatal(err) } defer w.Close() f, err := os.Open("/dev/zero") if err != nil { log.Fatal(err) } defer f.Close() const size int64 = 1e9 log.Printf("writing %v bytes", size) t1 := time.Now() n, err := io.Copy(w, io.LimitReader(f, size)) if err != nil { log.Fatal(err) } if n != size { log.Fatalf("copy: expected %v bytes, got %d", size, n) } log.Printf("wrote %v bytes in %s", size, time.Since(t1)) }
func (conn *SshConfig) transport() (*sftp.Client, error) { c, err := conn.Client() if err != nil { return nil, err } sftp, err := sftp.NewClient(c) if err != nil { return nil, err } return sftp, nil }
func (c *sshClientImpl) DownloadFile(username string, password string, ip string, srcFile string, destFile string) error { config := &myssh.ClientConfig{ User: username, Auth: []myssh.AuthMethod{ myssh.Password(password), }, } if !IsIP(ip) { return errors.New("invalid IP address") } if IsDir(srcFile) || IsDir(destFile) { return errors.New("Is a directory") } client, err := myssh.Dial("tcp", ip+":22", config) if err != nil { log.Fatal(err) return err } defer client.Close() sftp, err := sftp.NewClient(client) if err != nil { log.Fatal(err) return err } defer sftp.Close() pFile, err := sftp.Open(srcFile) if err != nil { log.Fatal(err) return err } defer pFile.Close() data, err := ioutil.ReadAll(pFile) if err != nil { log.Fatal(err) return err } err = ioutil.WriteFile(destFile, data, 0755) if err != nil { log.Fatal(err) return err } return nil }
func main() { if err := resolveSettings(pars); err != nil { log.Fatal(err) } conn, err := initSSH(pars) if err != nil { log.Fatal(err) } sftp, err := sftp.NewClient(conn) if err != nil { log.Fatal(err) } defer sftp.Close() finStat, err := os.Stat(inPath) if err != nil { log.Fatal(err) } origSize := finStat.Size() fin, err := os.Open(inPath) if err != nil { log.Fatal(err) } defer fin.Close() outPath := path.Clean(path.Join(pars.DstFolder, filepath.Base(inPath))) fout, err := sftp.Create(outPath) if err != nil { log.Fatal(err) } if _, err := io.Copy(fout, fin); err != nil { log.Fatal(err) } foutStat, err := sftp.Lstat(outPath) if err != nil { log.Fatal(err) } if finalSize := foutStat.Size(); origSize != finalSize { if err := sftp.Remove(outPath); err != nil { log.Printf("[ERROR] Unable to remove target file: %v", err) } log.Fatalf("Failed to upload %s to %s: expected %d bytes, got %d (missing %d)", inPath, outPath, origSize, finalSize, origSize-finalSize) } fmt.Println("Successfully uploaded", outPath) }
// connect to sftp, get ssh connection and sftp client func connectSftp(opt *SftpOption) (*ssh.Client, *sftp.Client, error) { conf := &ssh.ClientConfig{ User: opt.User, Auth: []ssh.AuthMethod{ ssh.Password(opt.Password), }, } client, err := ssh.Dial("tcp", opt.url.Host, conf) if err != nil { return nil, nil, err } s, err := sftp.NewClient(client) return client, s, err }
// Constructor func NewSender(username string, ip string, keyFile string, localPath string, remotePath string, logger *logrus.Logger) (*Sender, SenderError) { privateKeyDatas, _ := ioutil.ReadFile(keyFile) signer, error := ssh.ParsePrivateKey([]byte(privateKeyDatas)) if error != nil { senderError := SenderError{ code: FAILED_TO_PARSE_PRIVATE_KEY, } logger.WithFields(logrus.Fields{ "code": senderError.CodeInteger(), }).Error(senderError.CodeString()) return &Sender{}, senderError } config := &ssh.ClientConfig{ User: username, Auth: []ssh.AuthMethod{ ssh.PublicKeys(signer), }, } var sftpClient *sftp.Client if client, err := ssh.Dial("tcp", ip+":22", config); err == nil { if sftpClientTmp, err := sftp.NewClient(client); err == nil { sftpClient = sftpClientTmp } else { senderError := SenderError{ code: FAILED_TO_CONNECT_TO_REMOTE_HOST, } logger.WithFields(logrus.Fields{ "code": senderError.CodeInteger(), }).Error(senderError.CodeString()) return &Sender{}, senderError } } return &Sender{ sftpClient: sftpClient, localPath: localPath, remotePath: remotePath, logger: logger, }, SenderError{} }
func (ssc *SSHConfig) CopyFile(local_path, remote_path string) (err tree_lib.TreeError) { var ( sft *sftp.Client f *sftp.File file_data []byte ) err.From = tree_lib.FROM_SSH_COPY_FILE sft, err.Err = sftp.NewClient(ssc.conn) if !err.IsNull() { return } defer sft.Close() file_data, err.Err = ioutil.ReadFile(local_path) if !err.IsNull() { return } f, err.Err = sft.Create(remote_path) if !err.IsNull() { return } _, err.Err = f.Write(file_data) f.Close() sft.Close() return }