ftpclient

package module
v0.0.0-...-e49771e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2017 License: MIT Imports: 14 Imported by: 0

README

ftpclient-go

Golang Ftp Client Library

Installing

go get -u github.com/tsujimic/ftpclient-go

Using the ftpclient-go

package main

import (
    "fmt"
    "flag"
    "log"
    "net"
    "strconv"
    "time"
    "github.com/tsujimic/ftpclient-go"
)

func main() {
    var host, user, pass, remote, local string
    var port int
    
    flag.StringVar(&host, "host", "", "target host name")
    flag.IntVar(&port, "port", 21, "tcp/ip port number")
    flag.StringVar(&user, "user", "", "login username")
    flag.StringVar(&pass, "pass", "", "login password")
    flag.StringVar(&remote, "remote", "", "remote file path")
    flag.StringVar(&local, "local", "", "local file path")
    flag.Parse()

    log.Println("Start")

    logger := NewDefaultLogger()
    cfg := NewConfig().WithLogger(logger)
    client := New(cfg)
    addr := net.JoinHostPort(host, strconv.Itoa(port))
    err := client.DialTimeout(addr, 30*time.Second)
    if err != nil {
        panic(err)
    }
    defer client.Quit()

    err = client.Login(user, pass)
    if err != nil {
        panic(err)
    }

    err = client.Type("I")
    if err != nil {
        panic(err)
    }

    client.SetPasv(false)
    start := time.Now()
    err = client.StorFile(local, remote)
    if err != nil {
        panic(err)
    }

    sec := (time.Now().Sub(start)).Seconds()    
    msg := fmt.Sprintf("Stopwatch : %f seconds\n", sec)
    log.Println(msg)
}

Documentation

Overview

Package ftpclient implements a FTP client as described in RFC 959.

Index

Constants

View Source
const (
	DataConnectionAlreadyOpen = 125
	FileStatusOK              = 150
	CommandOkay               = 200
	SystemStatus              = 211
	DirectoryStatus           = 212
	FileStatus                = 213
	ConnectionClosing         = 221
	SystemType                = 215
	ServiceReadyForNewUser    = 220
	ClosingDataConnection     = 226
	UserLoggedIn              = 230
	ActionOK                  = 250
	PathCreated               = 257
	UserNameOK                = 331
	ActionPending             = 350
	NotLoggedIn               = 530
)

FTP Status Code, defined in RFC 959

Variables

This section is empty.

Functions

func NewTLSConfig

func NewTLSConfig() *tls.Config

NewTLSConfig ...

func NewTLSConfigWithX509KeyPair

func NewTLSConfigWithX509KeyPair(certFile, keyFile string) (*tls.Config, error)

NewTLSConfigWithX509KeyPair ...

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config ...

func NewConfig

func NewConfig() *Config

NewConfig ...

func (*Config) WithLogger

func (c *Config) WithLogger(logger Logger) *Config

WithLogger sets a config Logger value returning a Config pointer for chaining.

func (*Config) WithReadWriteTimeout

func (c *Config) WithReadWriteTimeout(time time.Duration) *Config

WithReadWriteTimeout sets a config ReadWriteTimeout value returning a Config pointer for chaining.

func (*Config) WithTLSConfig

func (c *Config) WithTLSConfig(config *tls.Config) *Config

WithTLSConfig sets a config tlsConfig value returning a Config pointer for chaining.

func (*Config) WithTLSImplicit

func (c *Config) WithTLSImplicit(implicit bool) *Config

WithTLSImplicit sets a config tlsImplicit value returning a Config pointer for chaining.

type FtpDataConn

type FtpDataConn struct {
	// contains filtered or unexported fields
}

FtpDataConn represent a data-connection

func (*FtpDataConn) Close

func (d *FtpDataConn) Close() error

Close implements the io.Closer interface on a FTP data connection.

func (*FtpDataConn) Read

func (d *FtpDataConn) Read(buf []byte) (int, error)

Read implements the io.Reader interface on a FTP data connection.

func (*FtpDataConn) Write

func (d *FtpDataConn) Write(buf []byte) (int, error)

Write implements the io.Writer interface on a FTP data connection.

type FtpServerConn

type FtpServerConn struct {
	*Config
	// contains filtered or unexported fields
}

FtpServerConn represents the connection to a remote FTP server.

func New

func New(cfg *Config) *FtpServerConn

New ...

func (*FtpServerConn) Abort

func (c *FtpServerConn) Abort() error

Abort a file transfer that is in progress.

func (*FtpServerConn) Auth

func (c *FtpServerConn) Auth(param string) error

Auth issues a AUTH FTP command

func (*FtpServerConn) Cdup

func (c *FtpServerConn) Cdup() error

Cdup issues a CDUP FTP command, which changes the current directory to the parent directory. This is similar to a call to ChangeDir with a path set to "..".

func (*FtpServerConn) Cwd

func (c *FtpServerConn) Cwd(path string) error

Cwd issues a CWD FTP command, which changes the current directory to the specified path.

func (*FtpServerConn) Delete

func (c *FtpServerConn) Delete(path string) error

Delete issues a DELE FTP command to delete the specified file from the remote FTP server.

func (*FtpServerConn) Dial

func (c *FtpServerConn) Dial(addr string) error

Dial ...

func (*FtpServerConn) DialTimeout

func (c *FtpServerConn) DialTimeout(addr string, timeout time.Duration) error

DialTimeout ...

func (*FtpServerConn) Dir

func (c *FtpServerConn) Dir(args ...string) (infos []os.FileInfo, err error)

Dir issues a LIST FTP command.

func (*FtpServerConn) Eprt

func (c *FtpServerConn) Eprt(host string, port int) error

Eprt issues a EPRT FTP command

func (*FtpServerConn) Epsv

func (c *FtpServerConn) Epsv() (port int, err error)

Epsv issues a "EPSV" command to get a port number for a data connection.

func (*FtpServerConn) Feat

func (c *FtpServerConn) Feat() error

Feat issues a FEAT FTP command

func (*FtpServerConn) GetResponse

func (c *FtpServerConn) GetResponse(expectCode int, timeout time.Duration) (int, string, error)

GetResponse issues a FTP command response

func (*FtpServerConn) List

func (c *FtpServerConn) List(args ...string) (lines []string, err error)

List issues a LIST FTP command.

func (*FtpServerConn) ListRequest

func (c *FtpServerConn) ListRequest(args ...string) (io.ReadCloser, error)

ListRequest issues a LIST FTP command.

func (*FtpServerConn) Login

func (c *FtpServerConn) Login(user, password string) error

Login as the given user.

func (*FtpServerConn) Mkd

func (c *FtpServerConn) Mkd(path string) (string, error)

Mkd issues a MKD FTP command to create the specified directory on the remote FTP server.

func (*FtpServerConn) Nlst

func (c *FtpServerConn) Nlst(args ...string) (lines []string, err error)

Nlst issues an NLST FTP command.

func (*FtpServerConn) NlstRequest

func (c *FtpServerConn) NlstRequest(args ...string) (io.ReadCloser, error)

NlstRequest issues an NLST FTP command.

func (*FtpServerConn) Noop

func (c *FtpServerConn) Noop() error

Noop has no effects and is usually used to prevent the remote FTP server to close the otherwise idle connection.

func (*FtpServerConn) Opts

func (c *FtpServerConn) Opts(param string) error

Opts issues a OPTS FTP command

func (*FtpServerConn) Pasv

func (c *FtpServerConn) Pasv() (host string, port int, err error)

Pasv issues a "PASV" command to get a port number for a data connection.

func (*FtpServerConn) Pbsz

func (c *FtpServerConn) Pbsz(param string) error

Pbsz issues a PBSZ FTP command

func (*FtpServerConn) Port

func (c *FtpServerConn) Port(host string, port int) error

Port issues a PORT FTP command

func (*FtpServerConn) Prot

func (c *FtpServerConn) Prot(param string) error

Prot issues a PROT FTP command

func (*FtpServerConn) Pwd

func (c *FtpServerConn) Pwd() (string, error)

Pwd issues a PWD FTP command, which Returns the path of the current directory.

func (*FtpServerConn) Quit

func (c *FtpServerConn) Quit() error

Quit issues a QUIT FTP command to properly close the connection from the remote FTP server.

func (*FtpServerConn) Rein

func (c *FtpServerConn) Rein() error

Rein issues a REIN FTP command to logout the current user. ftp server optional command.

func (*FtpServerConn) Rename

func (c *FtpServerConn) Rename(from, to string) error

Rename renames a file on the remote FTP server.

func (*FtpServerConn) Rest

func (c *FtpServerConn) Rest(offset uint64) error

Rest issues a REST FTP command.

func (*FtpServerConn) Retr

func (c *FtpServerConn) Retr(path string) error

Retr issues a RETR FTP command to fetch the specified file from the remote FTP server

func (*FtpServerConn) RetrFile

func (c *FtpServerConn) RetrFile(remote, local string) error

RetrFile issues a RETR FTP command to fetch the specified file from the remote FTP server

func (*FtpServerConn) RetrRequest

func (c *FtpServerConn) RetrRequest(path string) (io.ReadCloser, error)

RetrRequest issues a RETR FTP command to fetch the specified file from the remote FTP server The returned ReadCloser must be closed to cleanup the FTP data connection.

func (*FtpServerConn) Rmd

func (c *FtpServerConn) Rmd(path string) error

Rmd issues a RMD FTP command to remove the specified directory from the remote FTP server.

func (*FtpServerConn) SendCmd

func (c *FtpServerConn) SendCmd(expectCode int, format string, args ...interface{}) (int, string, error)

SendCmd Send a simple command string to the server and return the code and response string.

func (*FtpServerConn) SetPasv

func (c *FtpServerConn) SetPasv(ispassive bool)

SetPasv sets the mode to passive or active for data transfers.

func (*FtpServerConn) Size

func (c *FtpServerConn) Size(filename string) (int, error)

Size Request the size of the file named filename on the server. On success, the size of the file is returned as an integer. ftp server extention command.

func (*FtpServerConn) Stor

func (c *FtpServerConn) Stor(path string) error

Stor issues a STOR FTP command to store a file to the remote FTP server.

func (*FtpServerConn) StorFile

func (c *FtpServerConn) StorFile(local, remote string) error

StorFile issues a STOR FTP command to store a file to the remote FTP server.

func (*FtpServerConn) StorRequest

func (c *FtpServerConn) StorRequest(path string) (io.WriteCloser, error)

StorRequest issues a STOR FTP command to store a file to the remote FTP server. The returned WriteCloser must be closed to cleanup the FTP data connection.

func (*FtpServerConn) Syst

func (c *FtpServerConn) Syst() (string, error)

Syst issues a SYST FTP command

func (*FtpServerConn) TransferRequest

func (c *FtpServerConn) TransferRequest(format string, args ...interface{}) (io.ReadCloser, error)

TransferRequest issues a FTP command to fetch the specified file from the remote FTP server The returned ReadCloser must be closed to cleanup the FTP data connection.

func (*FtpServerConn) Type

func (c *FtpServerConn) Type(param string) error

Type issues a TYPE FTP command

type Logger

type Logger interface {
	Log(v ...interface{})
	Logf(format string, v ...interface{})
}

Logger ...

func NewDefaultLogger

func NewDefaultLogger() Logger

NewDefaultLogger ...

Directories

Path Synopsis
examples
dir
fxp
get
put

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL