func proxyCommand(host *config.Host, command string) error { command = host.ExpandString(command) args, err := shlex.Split(command) Logger.Debugf("ProxyCommand: %s", command) if err != nil { return err } spawn := exec.Command(args[0], args[1:]...) spawn.Stdout = os.Stdout spawn.Stdin = os.Stdin spawn.Stderr = os.Stderr return spawn.Run() }
func proxyCommand(host *config.Host, command string, dryRun bool) error { command = host.ExpandString(command) Logger.Debugf("ProxyCommand: %s", command) args, err := shlex.Split(command) if err != nil { return err } if dryRun { return fmt.Errorf("dry-run: Execute %s", args) } spawn := exec.Command(args[0], args[1:]...) spawn.Stdout = os.Stdout spawn.Stdin = os.Stdin spawn.Stderr = os.Stderr return spawn.Run() }
func proxy(host *config.Host, conf *config.Config) error { if len(host.Gateways) > 0 { Logger.Debugf("Trying gateways: %s", host.Gateways) for _, gateway := range host.Gateways { if gateway == "direct" { err := proxyDirect(host) if err != nil { Logger.Errorf("Failed to use 'direct' connection") } } else { gatewayHost := conf.GetGatewaySafe(gateway) err := prepareHostControlPath(host, gatewayHost) if err != nil { return err } if host.ProxyCommand == "" { host.ProxyCommand = "nc %h %p" } // FIXME: dynamically add "-v" flags if err = hostPrepare(host); err != nil { return err } command := "ssh %name -- " + host.ExpandString(host.ProxyCommand) Logger.Debugf("Using gateway '%s': %s", gateway, command) err = proxyCommand(gatewayHost, command) if err == nil { return nil } Logger.Errorf("Cannot use gateway '%s': %v", gateway, err) } } return fmt.Errorf("No such available gateway") } Logger.Debugf("Connecting without gateway") return proxyDirect(host) }
func hostPrepare(host *config.Host) error { if host.HostName == "" { host.HostName = host.Name() } if len(host.ResolveNameservers) > 0 { Logger.Debugf("Resolving host: '%s' using nameservers %s", host.HostName, host.ResolveNameservers) // FIXME: resolve using custom dns server results, err := net.LookupAddr(host.HostName) if err != nil { return err } if len(results) > 0 { host.HostName = results[0] } Logger.Debugf("Resolved host is: %s", host.HostName) } if host.ResolveCommand != "" { command := host.ExpandString(host.ResolveCommand) Logger.Debugf("Resolving host: %q using command: %q", host.HostName, command) args, err := shlex.Split(command) if err != nil { return err } cmd := exec.Command(args[0], args[1:]...) var stdout bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil { Logger.Errorf("ResolveCommand failed: %s", stderr.String()) return err } host.HostName = strings.TrimSpace(fmt.Sprintf("%s", stdout.String())) Logger.Debugf("Resolved host is: %s", host.HostName) } return nil }
func hostPrepare(host *config.Host) error { if host.HostName == "" { host.HostName = host.Name() } if len(host.ResolveNameservers) > 0 { Logger.Debugf("Resolving host: '%s' using nameservers %s", host.HostName, host.ResolveNameservers) // FIXME: resolve using custom dns server results, err := net.LookupAddr(host.HostName) if err != nil { return err } if len(results) > 0 { host.HostName = results[0] } Logger.Debugf("Resolved host is: %s", host.HostName) } if host.ResolveCommand != "" { command := host.ExpandString(host.ResolveCommand) Logger.Debugf("Resolving host: %q using command: %q", host.HostName, command) args, err := shlex.Split(command) if err != nil { return err } out, err := exec.Command(args[0], args[1:]...).Output() if err != nil { return err } host.HostName = strings.TrimSpace(fmt.Sprintf("%s", out)) Logger.Debugf("Resolved host is: %s", host.HostName) } return nil }