Updated to connect to another host after ssh exit.
This commit is contained in:
46
main.go
46
main.go
@@ -75,18 +75,7 @@ func readHosts(lines []string) []host {
|
||||
return hosts
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
log.Fatalf("Usage: %s [configfile]", os.Args[0])
|
||||
}
|
||||
fileName := args[0]
|
||||
lines := strings.Split(readFile(fileName), "\n")
|
||||
for i, line := range lines {
|
||||
lines[i] = strings.Trim(line, "\r\t ")
|
||||
}
|
||||
hosts := readHosts(lines)
|
||||
|
||||
func promptHost(hosts []host) (host, error) {
|
||||
searcher := func(input string, index int) bool {
|
||||
host := hosts[index]
|
||||
name := strings.Replace(strings.ToLower(host.Name+host.User+host.HostName), " ", "", -1)
|
||||
@@ -94,7 +83,6 @@ func main() {
|
||||
|
||||
return strings.Contains(name, input)
|
||||
}
|
||||
|
||||
hostTemplate := "{{\"[\" | bold}}{{.Name | cyan | bold }}{{\"]\" | bold}} {{ .User |bold }} @ {{ .HostName | bold }}"
|
||||
templates := &promptui.SelectTemplates{
|
||||
Label: "{{ . | bold }}",
|
||||
@@ -107,20 +95,44 @@ func main() {
|
||||
Label: "Select SSH Host",
|
||||
Items: hosts,
|
||||
Templates: templates,
|
||||
Size: 4,
|
||||
Size: len(hosts),
|
||||
Searcher: searcher,
|
||||
}
|
||||
|
||||
i, _, promptErr := prompt.Run()
|
||||
|
||||
if promptErr != nil {
|
||||
fmt.Println("No option selected")
|
||||
return
|
||||
return host{}, promptErr
|
||||
}
|
||||
|
||||
cmd := exec.Command("ssh", hosts[i].Name)
|
||||
return hosts[i], nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
log.Fatalf("Usage: %s [configfile]", os.Args[0])
|
||||
}
|
||||
fileName := args[0]
|
||||
lines := strings.Split(readFile(fileName), "\n")
|
||||
for i, line := range lines {
|
||||
lines[i] = strings.Trim(line, "\r\t ")
|
||||
}
|
||||
hosts := readHosts(lines)
|
||||
|
||||
var selected host
|
||||
var promptErr error
|
||||
|
||||
for {
|
||||
selected, promptErr = promptHost(hosts)
|
||||
if promptErr != nil {
|
||||
break
|
||||
}
|
||||
cmd := exec.Command("ssh", selected.Name)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Run()
|
||||
fmt.Println("\n\tSSH Session Closed\n")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user