sshw

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: MIT Imports: 24 Imported by: 0

README

sshw

GitHub GitHub tag (latest by date)

ssh client wrapper for automatic login ( with scp support now 🔥)

sshw demo

usage

sshw scp demo

usage sshw scp

说明

该项目 fork 自 sshw , 这是一个非常好用的 ssh 工具,我使用也有很长一段时间了,真的对工作效率有很大提升,在此感恩作者们。

在使用的场景中,我有对文件拷贝的需求,以前常用 scp / sftp 这类工具,不过和原生的 ssh 命令一样,这些工具在使用的时候也需要记 ip (或名字),由于用惯了 sshw 这个工具,因此希望将 scp 的功能也集成到该工具上。

于是,对该项目做了些微的改造,参考 go-scp 项目,将 scp 适配到了 sshw 上。

目前为止,仅支持文件的拷贝 (暂未支持目录递归拷贝),之后的计划有两个方面:

  • 增加目录递归拷贝
  • 增加模式匹配拷贝
  • 增加拷贝进度 (2022-11-08)
    • 进度条已完成,使用的 progressbar,很顺畅,感恩作者
  • 增加系统 history (似乎不好搞,拿不到history文件地址,目前仅测试了 zsh 和 bash 和 sh)
  • 增加 tab 键补全
  • 当 target path 为空时,改为相对路径 (2022-11-11)
  • 更加智能的地址分析
  • 支持 scp 中转
  • 增加 update 自动更新
  • scp 选择多个组
  • sshw 穿透

使用上,和 scp 类似:

sshw scp xx.txt xxx:~/
# 或
sshw scp xxx:~/xx.txt ./

install

use go install

go install github.com/iamlongalong/sshw/cmd/sshw

remeber to add GOPATH/bin to the $PATH

or download binary from releases.

config

config file load in following order:

  • ~/.sshw
  • ~/.sshw.yml
  • ~/.sshw.yaml
  • ./.sshw
  • ./.sshw.yml
  • ./.sshw.yaml

config example:

- { name: dev server fully configured, user: appuser, host: 192.168.8.35, port: 22, password: 123456 }
- { name: dev server with key path, user: appuser, host: 192.168.8.35, port: 22, keypath: /root/.ssh/id_rsa }
- { name: dev server with passphrase key, user: appuser, host: 192.168.8.35, port: 22, keypath: /root/.ssh/id_rsa, passphrase: abcdefghijklmn}
- { name: dev server without port, user: appuser, host: 192.168.8.35 }
- { name: dev server without user, host: 192.168.8.35 }
- { name: dev server without password, host: 192.168.8.35 }
- { name: ⚡️ server with emoji name, host: 192.168.8.35 }
- { name: server with alias, alias: dev, host: 192.168.8.35 }
- name: server with jump
  user: appuser
  host: 192.168.8.35
  port: 22
  password: 123456
  jump:
  - user: appuser
    host: 192.168.8.36
    port: 2222


# server group 1
- name: server group 1
  children:
  - { name: server 1, user: root, host: 192.168.1.2 }
  - { name: server 2, user: root, host: 192.168.1.3 }
  - { name: server 3, user: root, host: 192.168.1.4 }

# server group 2
- name: server group 2
  children:
  - { name: server 1, user: root, host: 192.168.2.2 }
  - { name: server 2, user: root, host: 192.168.3.3 }
  - { name: server 3, user: root, host: 192.168.4.4 }

callback

- name: dev server fully configured
  user: appuser
  host: 192.168.8.35
  port: 22
  password: 123456
  callback-shells:
    - { cmd: 2 }
    - { delay: 1500, cmd: 0 }
    - { cmd: "echo 1" }

ps

  • 如果在看代码的时候,无法理解 scp -t 这个参数的,可以参考 这篇文章

  • 上面的 demo gif 图,是用 terminalizer 录制的,挺好用。不过这个工具渲染出来的 gif 会相对较大,使用 gifcompressor 还挺不错,上面的 gif 图从原有的 4.6Mb 压缩到了 1.9Mb。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultCiphers = []string{
		"aes128-ctr",
		"aes192-ctr",
		"aes256-ctr",
		"aes128-gcm@openssh.com",
		"chacha20-poly1305@openssh.com",
		"arcfour256",
		"arcfour128",
		"arcfour",
		"aes128-cbc",
		"3des-cbc",
		"blowfish-cbc",
		"cast128-cbc",
		"aes192-cbc",
		"aes256-cbc",
	}
)

Functions

func Ack

func Ack(writer io.Writer) error

Ack writes an `Ack` message to the remote, does not await its response, a seperate call to ParseResponse is therefore required to check if the acknowledgement succeeded.

func CopyFromLocal

func CopyFromLocal(ctx context.Context, s *ssh.Session, localPath string, remotePath string) error

func CopyFromRemote

func CopyFromRemote(ctx context.Context, s *ssh.Session, remotePath string, localPath string) error

func CopyN

func CopyN(writer io.Writer, src io.Reader, size int64) (int64, error)

func LoadConfig

func LoadConfig() error

func LoadConfigBytes

func LoadConfigBytes(names ...string) ([]byte, error)

func LoadSshConfig

func LoadSshConfig() error

func ParseHostFile

func ParseHostFile(s string) (host string, filePath string, err error)

func RecordHistory

func RecordHistory(cmd string) error

func SetLogger

func SetLogger(logger Logger)

Types

type CallbackShell

type CallbackShell struct {
	Cmd   string        `yaml:"cmd"`
	Delay time.Duration `yaml:"delay"`
}

type Client

type Client interface {
	Login()
	Scp(ScpOption)
}

func NewClient

func NewClient(node *Node) Client

type FileInfos

type FileInfos struct {
	Message     string
	Filename    string
	Permissions string
	Size        int64
}

type Logger

type Logger interface {
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
}

func GetLogger

func GetLogger() Logger

type Node

type Node struct {
	Name           string           `yaml:"name"`
	Alias          string           `yaml:"alias"`
	Host           string           `yaml:"host"`
	User           string           `yaml:"user"`
	Port           int              `yaml:"port"`
	KeyPath        string           `yaml:"keypath"`
	Passphrase     string           `yaml:"passphrase"`
	Password       string           `yaml:"password"`
	CallbackShells []*CallbackShell `yaml:"callback-shells"`
	Children       []*Node          `yaml:"children"`
	Jump           []*Node          `yaml:"jump"`
}

func GetConfig

func GetConfig() []*Node

func (*Node) String

func (n *Node) String() string

type Response

type Response struct {
	Type    ResponseType
	Message string
}

Response represent a response from the SCP command. There are tree types of responses that the remote can send back: ok, warning and error

The difference between warning and error is that the connection is not closed by the remote, however, a warning can indicate a file transfer failure (such as invalid destination directory) and such be handled as such.

All responses except for the `Ok` type always have a message (although these can be empty)

The remote sends a confirmation after every SCP command, because a failure can occur after every command, the response should be read and checked after sending them.

func ParseResponse

func ParseResponse(reader io.Reader) (Response, error)

ParseResponse reads from the given reader (assuming it is the output of the remote) and parses it into a Response structure.

func (*Response) GetMessage

func (r *Response) GetMessage() string

GetMessage returns the message the remote sent back.

func (*Response) IsError

func (r *Response) IsError() bool

IsError returns true when the remote responded with an error.

func (*Response) IsFailure

func (r *Response) IsFailure() bool

IsFailure returns true when the remote answered with a warning or an error.

func (*Response) IsOk

func (r *Response) IsOk() bool

func (*Response) IsWarning

func (r *Response) IsWarning() bool

func (*Response) ParseFileInfos

func (r *Response) ParseFileInfos() (*FileInfos, error)

type ResponseType

type ResponseType = uint8
const (
	Ok      ResponseType = 0
	Warning ResponseType = 1
	Error   ResponseType = 2
)

type ScpOption

type ScpOption struct {
	SrcFilePath string
	SrcHost     string

	TarFilePath string
	TarHost     string
}

func ParseScpOption

func ParseScpOption(s string) (ScpOption, error)

func (*ScpOption) Valid

func (o *ScpOption) Valid() error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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