Documentation
¶
Overview ¶
Package scp provides functions to copy a single file or to copy files and directories under a directory recursively between the localhost and a remote server.
Example ¶
generateTestSshdKey := func() ([]byte, error) { key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return nil, err } pemdata := pem.EncodeToMemory( &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key), }, ) return pemdata, nil } newTestSshdServer := func() (*sshd.Server, net.Listener, error) { config := &ssh.ServerConfig{ PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) { if c.User() == testSshdUser && string(pass) == testSshdPassword { return nil, nil } return nil, fmt.Errorf("password rejected for %q", c.User()) }, } testSshdKey, err := generateTestSshdKey() if err != nil { return nil, nil, err } private, err := ssh.ParsePrivateKey(testSshdKey) if err != nil { return nil, nil, err } config.AddHostKey(private) server := sshd.NewServer(testSshdShell, config, nil) l, err := net.Listen("tcp", ":0") if err != nil { return nil, nil, err } return server, l, err } s, l, err := newTestSshdServer() if err != nil { log.Fatalf("fail to create test sshd server; %s", err) } defer s.Close() go s.Serve(l) config := &ssh.ClientConfig{ User: testSshdUser, Auth: []ssh.AuthMethod{ssh.Password(testSshdPassword)}, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } client, err := ssh.Dial("tcp", l.Addr().String(), config) if err != nil { log.Fatalf("failed to dial to ssh server: %s", err) } defer client.Close() localDir, err := ioutil.TempDir("", "go-scp-ExampleSendFile-local") if err != nil { log.Fatalf("fail to get tempdir; %s", err) } defer os.RemoveAll(localDir) remoteDir, err := ioutil.TempDir("", "go-scp-ExampleSendFile-remote") if err != nil { log.Fatalf("fail to get tempdir; %s", err) } defer os.RemoveAll(remoteDir) localName := "test1.dat" remoteName := "dest.dat" localPath := filepath.Join(localDir, localName) remotePath := filepath.Join(remoteDir, remoteName) content := []byte("Hello, SCP\n") err = ioutil.WriteFile(localPath, content, 0644) if err != nil { log.Fatalf("fail to write file; %s", err) } err = scp.NewSCP(client).SendFile(localPath, remotePath) if err != nil { log.Fatalf("fail to send file; %s", err) } data, err := ioutil.ReadFile(remotePath) if err != nil { log.Fatalf("fail to read file; %s", err) } fmt.Printf("%s", data)
Output: Hello, SCP
Index ¶
- type AcceptFunc
- type FileInfo
- type SCP
- func (s *SCP) Receive(srcFile string, dest io.Writer) (*FileInfo, error)
- func (s *SCP) ReceiveDir(srcDir, destDir string, acceptFn AcceptFunc) error
- func (s *SCP) ReceiveFile(srcFile, destFile string) error
- func (s *SCP) Send(info *FileInfo, r io.ReadCloser, destFile string) error
- func (s *SCP) SendDir(srcDir, destDir string, acceptFn AcceptFunc) error
- func (s *SCP) SendFile(srcFile, destFile string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AcceptFunc ¶
AcceptFunc is the type of the function called for each file or directory to determine whether is should be copied or not. In SendDir, parentDir will be a directory under srcDir. In ReceiveDir, parentDir will be a directory under destDir.
type FileInfo ¶
type FileInfo struct {
// contains filtered or unexported fields
}
FileInfo represents a file or a directory information.
func NewFileInfo ¶
func NewFileInfo(name string, size int64, mode os.FileMode, modTime, accessTime time.Time) *FileInfo
NewFileInfo creates a file information. The filepath.Base(name) is used as the name and the mode is masked with (os.ModePerm | os.ModeDir).
func (*FileInfo) AccessTime ¶
AccessTime returns access time.
type SCP ¶
type SCP struct { // Alternate scp command. If not set, scp is used. This can be used // to call scp via sudo by setting it to "sudo scp" SCPCommand string // contains filtered or unexported fields }
SCP is the type for the SCP client.
func NewSCP ¶
NewSCP creates the SCP client. It is caller's responsibility to call Dial for ssh.Client before calling NewSCP and call Close for ssh.Client after using SCP.
func (*SCP) Receive ¶
Receive copies a single remote file to the specified writer and returns the file information. The actual type of the file information is scp.FileInfo, and you can get the access time with fileInfo.(*scp.FileInfo).AccessTime().
func (*SCP) ReceiveDir ¶
func (s *SCP) ReceiveDir(srcDir, destDir string, acceptFn AcceptFunc) error
ReceiveDir copies files and directories under a remote srcDir to to the destDir on the local machine. You can filter the files and directories to be copied with acceptFn. If acceptFn is nil, all files and directories will be copied. The time and permission will be set to the same value of the source file or directory.
func (*SCP) ReceiveFile ¶
ReceiveFile copies a single remote file to the local machine with the specified name. The time and permission will be set to the same value of the source file.
func (*SCP) Send ¶
Send reads a single local file content from the r, and copies it to the remote file with the name info.Name() under the directory filepath.Dir(destFile). The time and permission will be set with the value of info. The r will be closed after copying. If you don't want for r to be closed, you can pass the result of ioutil.NopCloser(r).
func (*SCP) SendDir ¶
func (s *SCP) SendDir(srcDir, destDir string, acceptFn AcceptFunc) error
SendDir copies files and directories under the local srcDir to to the remote destDir. You can filter the files and directories to be copied with acceptFn. However this filtering is done at the receiver side, so all file bodies are transferred over the network even if some files are filtered out. If you need more efficiency, it is better to use another method like the tar command. If acceptFn is nil, all files and directories will be copied. The time and permission will be set to the same value of the source file or directory.