Skip to content

Commit

Permalink
add password flag to ssh-copy to make it usable programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
yakuter committed Apr 14, 2022
1 parent 4dad7b9 commit 84f6e15
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ gossl key --bits 2048 --out ./id_rsa

```bash
gossl ssh-copy --help
gossl ssh-copy --pubkey /home/user/.ssh/id_rsa.pub remoteUser@remoteIP

// This command will ask for password to connect SSH server
gossl ssh-copy --pubkey /home/user/.ssh/id_rsa.pub remoteUser@remoteIP

gossl ssh-copy --pubkey /home/user/.ssh/id_rsa.pub --password passw@rd123 remoteUser@remoteIP

```

### TODO
Expand Down
26 changes: 18 additions & 8 deletions commands/ssh_copy/ssh_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
const (
CmdSSHCopy = "ssh-copy"

flagPubkey = "pubkey"
flagPort = "port"
flagPubkey = "pubkey"
flagPort = "port"
flagPassword = "password"
)

func Command(reader passwordReader) *cli.Command {
Expand Down Expand Up @@ -49,6 +50,11 @@ func Flags() []cli.Flag {
DefaultText: "eg, 22",
Value: 22,
},
&cli.StringFlag{
Name: flagPassword,
Usage: "SSH server password",
Required: false,
},
}
}

Expand All @@ -70,12 +76,16 @@ func Action(reader passwordReader) func(*cli.Context) error {
return err
}

// Get password from user
fmt.Printf("Password: ")
pwd, err := reader.ReadPassword()
if err != nil {
log.Printf("failed to read inputs %v", err)
return err
var pwd string
if c.IsSet(flagPassword) {
pwd = c.String(flagPassword)
} else {
fmt.Printf("Password: ")
pwd, err = reader.ReadPassword()
if err != nil {
log.Printf("failed to read inputs %v", err)
return err
}
}

// Connect to remote SSH server with SFTP
Expand Down
17 changes: 16 additions & 1 deletion commands/ssh_copy/ssh_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,23 @@ func TestSSHCopy(t *testing.T) {
user: testUser,
shouldErr: false,
},
{
name: "valid sftp connection and write with password flag",
user: testUser,
pass: testPass,
shouldErr: false,
},
{
name: "credentials error",
user: "wrongUser",
shouldErr: true,
},
{
name: "credentials error with password flag",
user: "wrongUser",
pass: "wrongpass",
shouldErr: true,
},
}

for _, tC := range testCases {
Expand All @@ -93,8 +105,11 @@ func TestSSHCopy(t *testing.T) {
execName, CmdSSHCopy,
"--pubkey", pubFile.Name(),
"--port", port,
fmt.Sprintf("%s@localhost", tC.user),
}
if tC.pass != "" {
testArgs = append(testArgs, "--password", tC.pass)
}
testArgs = append(testArgs, fmt.Sprintf("%s@localhost", tC.user))

app := &cli.App{
Commands: []*cli.Command{
Expand Down

0 comments on commit 84f6e15

Please sign in to comment.