Skip to content

Commit

Permalink
set default user based on current user (same default as most ssh clie…
Browse files Browse the repository at this point in the history
…nts)

use filepath.Join rather than strings.Join for better os support
  • Loading branch information
stensonb committed Feb 20, 2023
1 parent 724d025 commit afcffac
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/goph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"log"
"net"
"os"
osuser "os/user"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -53,11 +55,16 @@ var (
)

func init() {
usr, err := osuser.Current()
if err != nil {
fmt.Println("couldn't determine current user. defaulting to 'root'")
usr.Username = "root"
}

flag.StringVar(&addr, "ip", "127.0.0.1", "machine ip address.")
flag.StringVar(&user, "user", "root", "ssh user.")
flag.StringVar(&user, "user", usr.Username, "ssh user.")
flag.UintVar(&port, "port", 22, "ssh port number.")
flag.StringVar(&key, "key", strings.Join([]string{os.Getenv("HOME"), ".ssh", "id_rsa"}, "/"), "private key path.")
flag.StringVar(&key, "key", filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa"), "private key path.")
flag.StringVar(&cmd, "cmd", "", "command to run.")
flag.BoolVar(&pass, "pass", false, "ask for ssh password instead of private key.")
flag.BoolVar(&agent, "agent", false, "use ssh agent for authentication (unix systems only).")
Expand Down

0 comments on commit afcffac

Please sign in to comment.