Skip to content

Commit

Permalink
client: add GetHostUser for u@host and .ssh/config (u-root#280)
Browse files Browse the repository at this point in the history
Since 2017 cpu used $USER as the user. Now that it supports
ssh, it needs better processing.

Add GetHostUser, which takes a string in [user@]host form,
and returns a host and user name as follows:
if user@host form, return the host and user.

Look up the host in .ssh/config; if found, and
there is a UserName record, return that UserName

Otherwise return host and $USER.

If a host entry is found in .ssh/config, the
HostName from that entry is used.

This means that code that used GetHost should start using
GetHostUser.

Signed-off-by: Ron Minnich <[email protected]>
Co-authored-by: Ron Minnich <[email protected]>
  • Loading branch information
rminnich and rminnichcodeu committed Jul 20, 2024
1 parent f44d394 commit 6092ed5
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions client/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,14 @@ func GetHostName(host string) string {
// can be replaced as needed, but that's for the future.
func GetHostUser(n string) (host, user string) {
if u, h, ok := strings.Cut(n, "@"); ok {
host, user = GetHostName(h), u
return
return GetHostName(h), u
}
// Perhaps a user name is found in .ssh/config
if cp := config.Get(n, "User"); len(cp) != 0 {
host, user = GetHostName(n), cp
return
return GetHostName(n), cp
}
// Last try: the environment.
host, user = GetHostName(n), os.Getenv("USER")
return
return GetHostName(n), os.Getenv("USER")
}

// GetPort gets a port. It verifies that the port fits in 16-bit space.
Expand Down

0 comments on commit 6092ed5

Please sign in to comment.