


About using golang to encapsulate ssh to execute commands on the remote host and upload or download files
The following tutorial column of golang will introduce to you how to use golang to encapsulate ssh to execute commands on the remote host and upload or download files. I hope it will be helpful to friends who need it. Helped!
In python, paramiko can be used to execute commands, upload and download files on the remote host. You can also encapsulate one with go. In go, you can use ssh and sftp packages to achieve this. The following functions are implemented
- Execute the command on the remote host to return the result, return value
- Upload and download files on the remote host, and how many bytes are transferred
Authentication method
- If a password is specified, the user password is used for authentication, otherwise the user secret key is used for authentication
- If not specified User, the current user will be used by default
- If no password is specified, the user key method will be used, and the ~/.ssh/id_rsa file private key file will be obtained by default to obtain the secret key
Similar to paramik
Directly upload the code
package mainimport ( "errors" "fmt" "github.com/pkg/sftp" "golang.org/x/crypto/ssh" "io" "io/ioutil" "log" "os" "os/user" "time")var ( DefaultSShTcpTimeout = 15 * time.Second // 与ssh建立连接的默认时间,自己设置一个就行)// 错误定义var ( InvalidHostName = errors.New("invalid parameters: hostname is empty") InvalidPort = errors.New("invalid parameters: port must be range 0 ~ 65535"))// 返回当前用户名func getCurrentUser() string { user, _ := user.Current() return user.Username}// 存放上传或下载的信息type TransferInfo struct { Kind string // upload或download Local string // 本地路径 Dst string // 目标路径 TransferByte int64 // 传输的字节数(byte)}func (t *TransferInfo) String() string { return fmt.Sprintf(`TransforInfo(Kind:"%s", Local: "%s", Dst: "%s", TransferByte: %d)`, t.Kind, t.Local, t.Dst, t.TransferByte)}// 存放执行结果的结构体信息type ExecInfo struct { Cmd string Output []byte ExitCode int}func (e *ExecInfo) OutputString() string { return string(e.Output)}func (e *ExecInfo) String() string { return fmt.Sprintf(`ExecInfo(cmd: "%s", exitcode: %d)`, e.Cmd, e.ExitCode)}type AuthConfig struct { *ssh.ClientConfig User string Password string KeyFile string Timeout time.Duration}func (a *AuthConfig) setDefault() { if a.User == "" { a.User = getCurrentUser() } if a.KeyFile == "" { userHome, _ := os.UserHomeDir() a.KeyFile = fmt.Sprintf("%s/.ssh/id_rsa", userHome) } if a.Timeout == 0 { a.Timeout = DefaultSShTcpTimeout }}func (a *AuthConfig) SetAuthMethod() (ssh.AuthMethod, error) { a.setDefault() if a.Password != "" { return ssh.Password(a.Password), nil } data, err := ioutil.ReadFile(a.KeyFile) if err != nil { return nil, err } singer, err := ssh.ParsePrivateKey(data) if err != nil { return nil, err } return ssh.PublicKeys(singer), nil}func (a *AuthConfig) ApplyConfig() error { authMethod, err := a.SetAuthMethod() if err != nil { return err } a.ClientConfig = &ssh.ClientConfig{ User: a.User, Auth: []ssh.AuthMethod{authMethod}, HostKeyCallback: ssh.InsecureIgnoreHostKey(), Timeout: a.Timeout, } return nil}// 存放连接的结构体type conn struct { client *ssh.Client sftpClient *sftp.Client}func (c *conn) Close() { if c.sftpClient != nil { c.sftpClient.Close() c.sftpClient = nil } if c.client != nil { c.client.Close() c.client = nil }}// SSHClient结构体type SSHClient struct { conn HostName string Port int AuthConfig AuthConfig}// 设置默认端口信息func (s *SSHClient) setDefaultValue() { if s.Port == 0 { s.Port = 22 }}// 与远程主机连接func (s *SSHClient) Connect() error { if s.client != nil { log.Println("Already Login") return nil } if err := s.AuthConfig.ApplyConfig(); err != nil { return err } s.setDefaultValue() addr := fmt.Sprintf("%s:%d", s.HostName, s.Port) var err error s.client, err = ssh.Dial("tcp", addr, s.AuthConfig.ClientConfig) if err != nil { return err } return nil}// 一个session只能执行一次命令,也就是说不能在同一个session执行多次s.session.CombinedOutput// 如果想执行多次,需要每条为每个命令创建一个session(这里是这样做)func (s *SSHClient) Exec(cmd string) (*ExecInfo, error) { session, err := s.client.NewSession() if err != nil { return nil, err } defer session.Close() output, err := session.CombinedOutput(cmd) var exitcode int if err != nil { // 断言转成具体实现类型,获取返回值 exitcode = err.(*ssh.ExitError).ExitStatus() } return &ExecInfo{ Cmd: cmd, Output: output, ExitCode: exitcode, }, nil}// 将本地文件上传到远程主机上func (s *SSHClient) Upload(localPath string, dstPath string) (*TransferInfo, error) { transferInfo := &TransferInfo{Kind: "upload", Local: localPath, Dst: dstPath, TransferByte: 0} var err error // 如果sftp客户端没有打开,就打开,为了复用 if s.sftpClient == nil { if s.sftpClient, err = sftp.NewClient(s.client); err != nil { return transferInfo, err } } localFileObj, err := os.Open(localPath) if err != nil { return transferInfo, err } defer localFileObj.Close() dstFileObj, err := s.sftpClient.Create(dstPath) if err != nil { return transferInfo, err } defer dstFileObj.Close() written, err := io.Copy(dstFileObj, localFileObj) if err != nil { return transferInfo, err } transferInfo.TransferByte = written return transferInfo, nil}// 从远程主机上下载文件到本地func (s *SSHClient) Download(dstPath string, localPath string) (*TransferInfo, error) { transferInfo := &TransferInfo{Kind: "download", Local: localPath, Dst: dstPath, TransferByte: 0} var err error if s.sftpClient == nil { if s.sftpClient, err = sftp.NewClient(s.client); err != nil { return transferInfo, err } } //defer s.sftpClient.Close() localFileObj, err := os.Create(localPath) if err != nil { return transferInfo, err } defer localFileObj.Close() dstFileObj, err := s.sftpClient.Open(dstPath) if err != nil { return transferInfo, err } defer dstFileObj.Close() written, err := io.Copy(localFileObj, dstFileObj) if err != nil { return transferInfo, err } transferInfo.TransferByte = written return transferInfo, nil}// SSHclient的构造方法func NewSSHClient(hostname string, port int, authConfig AuthConfig) (*SSHClient, error) { switch { case hostname == "": return nil, InvalidHostName case port > 65535 || port
The above is the detailed content of About using golang to encapsulate ssh to execute commands on the remote host and upload or download files. For more information, please follow other related articles on the PHP Chinese website!

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

Golang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang excels in practical applications and is known for its simplicity, efficiency and concurrency. 1) Concurrent programming is implemented through Goroutines and Channels, 2) Flexible code is written using interfaces and polymorphisms, 3) Simplify network programming with net/http packages, 4) Build efficient concurrent crawlers, 5) Debugging and optimizing through tools and best practices.

The core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Confused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.