Last active
August 29, 2015 14:26
-
-
Save fujiwara/9224b202f946fbb89bb5 to your computer and use it in GitHub Desktop.
nssh
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
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
"os/exec" | |
"sync" | |
) | |
var ( | |
addPrefix bool | |
source string | |
) | |
type strslice []string | |
func (s *strslice) String() string { | |
return fmt.Sprintf("%v", *s) | |
} | |
func (s *strslice) Set(v string) error { | |
*s = append(*s, v) | |
return nil | |
} | |
func main() { | |
var targets strslice | |
var showSource bool | |
flag.Var(&targets, "t", "target hostname") | |
flag.BoolVar(&addPrefix, "p", false, "add hostname to line prefix") | |
flag.BoolVar(&showSource, "v", false, "show version") | |
flag.Parse() | |
if showSource { | |
fmt.Println("Source URL:", source) | |
return | |
} | |
command := flag.Args() | |
if len(command) < 1 { | |
flag.PrintDefaults() | |
return | |
} | |
wg := &sync.WaitGroup{} | |
for _, host := range targets { | |
wg.Add(1) | |
go func(h string) { | |
remoteCommand(h, command) | |
wg.Done() | |
}(host) | |
} | |
wg.Wait() | |
} | |
func remoteCommand(host string, command []string) { | |
args := []string{"-t", "-t"} // man ssh(1): Multiple -t options force tty allocation, even if ssh has no local tty. | |
args = append(args, host) | |
args = append(args, command...) | |
cmd := exec.Command("ssh", args...) | |
stdout, err := cmd.StdoutPipe() | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "[error]", host, err) | |
return | |
} | |
stderr, err := cmd.StderrPipe() | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "[error]", host, err) | |
return | |
} | |
cmd.Start() | |
go scanLines(stderr, os.Stderr, host) | |
go scanLines(stdout, os.Stdout, host) | |
cmd.Wait() | |
} | |
func scanLines(src io.ReadCloser, dest io.Writer, prefix string) { | |
scanner := bufio.NewScanner(src) | |
for scanner.Scan() { | |
if addPrefix { | |
fmt.Fprintln(dest, prefix, scanner.Text()) | |
} else { | |
fmt.Fprintln(dest, scanner.Text()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment