Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add scp for up & download #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cmd/sshman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
Name: "sshman",
Description: "SSH connection management tool.",
Author: "@mikeunge",
Version: "1.3.3",
Version: "1.4.0",
Github: "https://github.com/mikeunge/sshman",
}

Expand Down Expand Up @@ -70,6 +70,21 @@ func main() {
case "update":
additionalArg := getAdditionalArg(args, argsFound)
err = profileService.UpdateProfile(additionalArg)
break
case "upload":
files := *args["upload"].(*[]string)
if len(files) < 2 {
err = fmt.Errorf("Upload needs 2 paths. sshman --upload <from> <to>")
break
}

from := files[0]
to := files[1]
err = profileService.UploadFile(from, to)
break
case "download":
err = profileService.ProfilesList()
break
default:
os.Exit(0)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/bramvdbogaerde/go-scp v1.4.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 // indirect
github.com/gookit/color v1.5.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYew
github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi+zB4=
github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/bramvdbogaerde/go-scp v1.4.0 h1:jKMwpwCbcX1KyvDbm/PDJuXcMuNVlLGi0Q0reuzjyKY=
github.com/bramvdbogaerde/go-scp v1.4.0/go.mod h1:on2aH5AxaFb2G0N5Vsdy6B0Ml7k9HuHSwfo1y0QzAbQ=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (app *App) New() (map[string]interface{}, map[string]*bool, error) {
args["delete"], argsFound["delete"] = parser.Flag("-d", "--delete", &argparser.Options{Required: false, Help: "Delete SSH profiles."})
args["export"], argsFound["export"] = parser.Flag("-e", "--export", &argparser.Options{Required: false, Help: "Export profiles (for eg. sharing)."})

args["upload"], argsFound["upload"] = parser.MultiString("", "--upload", &argparser.Options{Required: false, Help: "Upload a file to a remote host."})
args["download"], argsFound["download"] = parser.MultiString("", "--download", &argparser.Options{Required: false, Help: "Download a file from a remote host."})

args["alias"], argsFound["alias"] = parser.String("-a", "--alias", &argparser.Options{Required: false, Help: "Provide an alias to directly access."})
args["id"], argsFound["id"] = parser.Number("-i", "--id", &argparser.Options{Required: false, Help: "Provide an id for directly accessing."})

Expand Down
5 changes: 5 additions & 0 deletions internal/profiles/profileService.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ func (s *ProfileService) DeleteProfile(p string) error {
return nil
}

func (s *ProfileService) UploadFile(from string, to string) error {
fmt.Println(from, to)
return nil
}

func (s *ProfileService) ConnectToServer(p string) error {
var (
profile database.SSHProfile
Expand Down
53 changes: 53 additions & 0 deletions pkg/ssh/scp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ssh

import (
"context"
"fmt"
"os"

scp "github.com/bramvdbogaerde/go-scp"
"github.com/bramvdbogaerde/go-scp/auth"
"golang.org/x/crypto/ssh"
)

func NewSCPClient() {
return SSHServer{User: user, Host: host, SecureConnection: false}
}

func NewSCP() error {
// Use SSH key authentication from the auth package
// we ignore the host key in this example, please change this if you use this library
clientConfig, _ := auth.PrivateKey("username", "/path/to/rsa/key", ssh.InsecureIgnoreHostKey())

// For other authentication methods see ssh.ClientConfig and ssh.AuthMethod

// Create a new SCP client
client := scp.NewClient("example.com:22", &clientConfig)

// Connect to the remote server
err := client.Connect()
if err != nil {
fmt.Println("Couldn't establish a connection to the remote server ", err)
return
}

// Open a file
f, _ := os.Open("/path/to/local/file")

// Close client connection after the file has been copied
defer client.Close()

// Close the file after it has been copied
defer f.Close()

// Finally, copy the file over
// Usage: CopyFromFile(context, file, remotePath, permission)

// the context can be adjusted to provide time-outs or inherit from other contexts if this is embedded in a larger application.
err = client.CopyFromFile(context.Background(), *f, "/home/server/test.txt", "0655")

if err != nil {
fmt.Println("Error while copying file ", err)
}
return nil
}
4 changes: 4 additions & 0 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type SSHServer struct {
Client *goph.Client
}

func NewSSHServer(user string, host string) SSHServer {
return SSHServer{User: user, Host: host, SecureConnection: false}
}

func (s SSHServer) generateSSHClient(auth goph.Auth) (*goph.Client, error) {
if s.SecureConnection {
client, err := goph.New(s.User, s.Host, auth)
Expand Down