sftp_util

package module
v0.0.0-...-593099a Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README

sftp_util

Example Go-lang command-line utility for SFTP

How to use the package

Confugre the settings for your file transfer, as well as server and credentials, in the SftpUtil struct. The following comments explain the fields to configure:

type SftpUtil struct {
	Rdir      string // Remote directory
	Ldir      string // Local directory
	Filename  string // File to transfer
	Type      string // GET or PUT
	User      string // Username
	Pass      string // Password
	Host      string // Hostname or IP Address
	Port      string // TCP port
	Client    *sftp.Client
}

The routine GetCmdLine() reads comand-line flags to specify the file details and type of transfer. These parameters set the first four fields in the above struct.

Sample Code

A sample main() routine for making a stand-alone sftp utilty is provided in sftp-cmd/sftp-cmd.go

package main

import (
	"fmt"
	"github.com/DavidSantia/sftp_util"
)

func main() {
	sftp := &sftp_util.SftpSettings{}
	err := sftp.GetCmdLine()
	if err != nil {
		fmt.Printf("Command-line error: %v\n", err)
		return
	}
	sftp.User = "USER"
	sftp.Pass = "PASSWORD"
	sftp.Host = "HOST"
	sftp.Port = "22"

	// Open SSH session
	err = sftp.Connect()
	if err != nil {
		fmt.Printf("Connect error: %v\n", err)
		return
	}
	defer sftp.Client.Close()

	if sftp.Type == "GET" {
		err = sftp.GetFile()
		if err != nil {
			fmt.Printf("GET error: %v\n", err)
			return
		}
	} else if sftp.Type == "PUT" {
		err = sftp.PutFile()
		if err != nil {
			fmt.Printf("PUT error: %v\n", err)
			return
		}
	} else if sftp.Type == "RM" {
		err = sftp.RmFile()
		if err != nil {
			fmt.Printf("RM error: %v\n", err)
			return
		}
	} else {
		err = sftp.LsDir()
		if err != nil {
			fmt.Printf("LS error: %v\n", err)
			return
		}
	}
}
Building the project

You can buid directly:

$ cd sftp-cmd
$ go build

Alternatively, a build script is provided that builds for linux using a Docker container:

$ ./build.sh
Running the utility

The utility takes the following flags:

Usage of ./sftp-cmd:
  -file string
    	File to transfer
  -ldir string
    	Local directory (default ".")
  -nochmod
    	Don't attempt to set remote file permissions
  -rdir string
    	Remote directory (default ".")
  -type string
    	GET, PUT, RM or LS (required)

Documentation

Index

Constants

View Source
const SFTP_BUFSIZE = 131072

Byte buffer for file I/O (128K)

Variables

This section is empty.

Functions

func DoNothing

func DoNothing(hostname string, remote net.Addr, key ssh.PublicKey) (err error)

Types

type SftpSettings

type SftpSettings struct {
	NoChmod  bool
	Rdir     string // Remote directory
	Ldir     string // Local directory
	Filename string // File to transfer
	Type     string // GET or PUT
	User     string // Username
	Pass     string // Password
	KeyFile  string // RSA Key file
	Host     string // Hostname or IP Address
	Port     string // TCP port
	Client   *sftp.Client
}

func (*SftpSettings) Connect

func (sftpSettings *SftpSettings) Connect() (err error)

func (*SftpSettings) GetCmdLine

func (util *SftpSettings) GetCmdLine() (err error)

func (*SftpSettings) GetFile

func (sftpSettings *SftpSettings) GetFile() (err error)

func (*SftpSettings) LsDir

func (sftpSettings *SftpSettings) LsDir() (err error)

func (*SftpSettings) PutFile

func (sftpSettings *SftpSettings) PutFile() (err error)

func (*SftpSettings) RmFile

func (sftpSettings *SftpSettings) RmFile() (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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