Documentation ¶
Overview ¶
Package scplib is a library for exchanging data with scp in golang.
Example ¶
// Read Private key key, err := ioutil.ReadFile(os.Getenv("HOME") + "/.ssh/id_rsa") if err != nil { fmt.Fprintf(os.Stderr, "Unable to read private key: %v\n", err) os.Exit(1) } // Parse Private key signer, err := ssh.ParsePrivateKey(key) if err != nil { fmt.Fprintf(os.Stderr, "Unable to parse private key: %v\n", err) os.Exit(1) } // Create ssh client config config := &ssh.ClientConfig{ User: "user", Auth: []ssh.AuthMethod{ ssh.PublicKeys(signer), }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), Timeout: 60 * time.Second, } // Create ssh connection connection, err := ssh.Dial("tcp", "test-node:22", config) if err != nil { fmt.Fprintf(os.Stderr, "Failed to dial: %s\n", err) os.Exit(1) } defer connection.Close() // Create scp client scp := new(scplib.SCPClient) scp.Permission = false // copy permission with scp flag scp.Connection = connection // scp get file // scp.GetFile("/From/Remote/Path","/To/Local/Path") err = scp.GetFile([]string{"/etc/passwd"}, "./passwd") if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp get: %s\n", err) os.Exit(1) } // // Dir pattern snip // this sample comment out // err := scp.GetFile("/path/from/remote/dir", "./path/to/local/dir") // if err != nil { // fmt.Fprintf(os.Stderr, "Failed to create session: %s\n", err) // os.Exit(1) // } // scp put file // scp.PutFile("/From/Local/Path","/To/Remote/Path") err = scp.PutFile([]string{"./passwd"}, "./passwd_scp") if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err) os.Exit(1) } // scp get file (to scp format data) // scp.GetData("/path/remote/path") getData, err := scp.GetData([]string{"/etc/passwd"}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err) os.Exit(1) } fmt.Println(getData) // scp put file (to scp format data) // scp.GetData(Data,"/path/remote/path") err = scp.PutData(getData, "./passwd_data") if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err) os.Exit(1) }
Output:
Index ¶
- type SCPClient
- func (s *SCPClient) GetData(fromPaths []string) (data *bytes.Buffer, err error)
- func (s *SCPClient) GetFile(fromPaths []string, toPath string) (err error)
- func (s *SCPClient) PutData(fromData *bytes.Buffer, toPath string) (err error)
- func (s *SCPClient) PutFile(fromPaths []string, toPath string) (err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SCPClient ¶
SCPClient save credentials and use scp from method.
func (*SCPClient) GetData ¶
GetData get and return scp format data(remote to local).
example:
scp.GetData("/path/remote/path")
Example ¶
var connection *ssh.Client // Create scp client scp := new(scplib.SCPClient) scp.Permission = false // copy permission with scp flag scp.Connection = connection // *ssh.Client // Get /etc/passwd from remote machine getData, err := scp.GetData([]string{"/etc/passwd"}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err) os.Exit(1) } // println getData fmt.Println(getData)
Output: C0644 1561 passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin ...
func (*SCPClient) GetFile ¶
GetFile get file data to file (remote to Local).
example:
scp.GetFile("/From/Remote/Path","/To/Local/Path")
Example ¶
var connection *ssh.Client // Create scp client scp := new(scplib.SCPClient) scp.Permission = false // copy permission with scp flag scp.Connection = connection // *ssh.Client // Get /etc/passwd from remote machine, and copy to ./passwd err := scp.GetFile([]string{"/etc/passwd"}, "./passwd") if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp get: %s\n", err) os.Exit(1) }
Output:
func (*SCPClient) PutData ¶
PutData put data of scp format as a file(local to remote).
example:
scp.PutData(buffer(scp format data),"/path/remote/path")
Example ¶
var connection *ssh.Client // Create scp client scp := new(scplib.SCPClient) scp.Permission = false // copy permission with scp flag scp.Connection = connection // *ssh.Client // Get /etc/passwd from remote machine getData, err := scp.GetData([]string{"/etc/passwd"}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err) os.Exit(1) } // getData Value... // C0644 1561 passwd // root:x:0:0:root:/root:/bin/bash // daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin // bin:x:2:2:bin:/bin:/usr/sbin/nologin // sys:x:3:3:sys:/dev:/usr/sbin/nologin // ... // Put getData err = scp.PutData(getData, "./passwd_data") if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err) os.Exit(1) }
Output:
func (*SCPClient) PutFile ¶
PutFile is put file to remote path.
example:
scp.PutFile("/From/Local/Path","/To/Remote/Path")
Example ¶
var connection *ssh.Client // Create scp client scp := new(scplib.SCPClient) scp.Permission = false // copy permission with scp flag scp.Connection = connection // *ssh.Client // Put ./passwd to remote machine `./passwd_scp` err := scp.PutFile([]string{"./passwd"}, "./passwd_scp") if err != nil { fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err) os.Exit(1) }
Output:
Click to show internal directories.
Click to hide internal directories.