-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9d8993
commit be275a4
Showing
5 changed files
with
119 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
) | ||
|
||
// Type for results | ||
type Result struct { | ||
Target string | ||
Hostname string | ||
Stdout string | ||
Stderr string | ||
Error error | ||
} | ||
|
||
func (result *Result) toString(format string) string { | ||
tmpl, err := template.New("result").Parse(format) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var buf bytes.Buffer | ||
err = tmpl.Execute(&buf, result) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/appleboy/easyssh-proxy" | ||
"github.com/kevinburke/ssh_config" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func getConfigForHost(target string) *easyssh.MakeConfig { | ||
proxy := ssh_config.Get(target, "ProxyJump") | ||
user := ssh_config.Get(target, "User") | ||
if user == "" { | ||
user = os.Getenv("USER") | ||
} | ||
port := ssh_config.Get(target, "Port") | ||
if port == "" { | ||
port = "22" | ||
} | ||
hostname := fillSSHConfigHostname(target, ssh_config.Get(target, "HostName")) | ||
if hostname == "" { | ||
hostname = target | ||
} | ||
|
||
log.Debug("Creating SSH config for target ", target, " with hostname ", hostname, " and proxy ", proxy) | ||
|
||
if proxy != "" { | ||
log.Debug("Using proxy ", proxy, " for target ", target) | ||
return &easyssh.MakeConfig{ | ||
User: user, | ||
Server: hostname, | ||
Port: port, | ||
Proxy: *makeConfigToDefaultConfig(getConfigForHost(proxy)), | ||
} | ||
} else { | ||
log.Debug("No proxy found for target ", target) | ||
return &easyssh.MakeConfig{ | ||
User: user, | ||
Server: hostname, | ||
Port: port, | ||
} | ||
} | ||
} | ||
|
||
func fillSSHConfigHostname(target string, configHostName string) string { | ||
// Check if "%h" is in the config name | ||
if strings.Contains(configHostName, "%h") { | ||
// Replace %h with the target | ||
return strings.ReplaceAll(configHostName, "%h", target) | ||
} | ||
|
||
// Nothing to replace | ||
return configHostName | ||
} | ||
|
||
// Note: this is necessary for generating proxy configs | ||
func makeConfigToDefaultConfig(makeConfig *easyssh.MakeConfig) *easyssh.DefaultConfig { | ||
return &easyssh.DefaultConfig{ | ||
User: makeConfig.User, | ||
Server: makeConfig.Server, | ||
Port: makeConfig.Port, | ||
Password: makeConfig.Password, | ||
KeyPath: makeConfig.KeyPath, | ||
Timeout: makeConfig.Timeout, | ||
} | ||
} |