Skip to content

Commit

Permalink
add input hostname from files option '-f'
Browse files Browse the repository at this point in the history
  • Loading branch information
u5surf committed Jun 26, 2018
1 parent 21d86dd commit 57cee53
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -26,6 +26,7 @@ Usage of nssh:
-i=false: handle stdin
-p=false: add hostname to line prefix
-t=[]: target hostname
-f=[]: target hostname from files
-v=false: show version
```

Expand All @@ -43,4 +44,16 @@ Pass nssh's stdin to remote command. Use `-i` option.
$ echo foo | nssh -i -t host-web-001 -t host-web-002 cat
foo
foo
```

Target hosts from file. Use `-f` option.

```
$ cat hosts.txt
host-web-001
host-web-002
$ nssh -f hosts.txt cat /var/log/nginx/access.log
12.34.567.890 - - [06/Oct/2015:15:52:42 +0900] "-" 400 0 "-" "-"
12.34.567.890 - - [06/Oct/2015:15:52:43 +0900] "GET / HTTP/1.1" 200 133 "-" "Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"
```
23 changes: 23 additions & 0 deletions nssh.go
Expand Up @@ -30,8 +30,10 @@ func (s *strslice) Set(v string) error {

func main() {
var targets strslice
var filenames strslice
var showVersion bool
flag.Var(&targets, "t", "target hostname")
flag.Var(&filenames, "f", "target hostname from file")
flag.BoolVar(&addPrefix, "p", false, "add hostname to line prefix")
flag.BoolVar(&showVersion, "v", false, "show version")
flag.BoolVar(&handleStdin, "i", false, "handle STDIN")
Expand All @@ -40,6 +42,9 @@ func main() {
fmt.Println("version:", version)
return
}
if len(filenames) > 0 {
getHostFromFiles(&targets, &filenames)
}

command := flag.Args()
if len(command) < 1 {
Expand Down Expand Up @@ -148,3 +153,21 @@ func writeInput(src chan []byte, dest io.WriteCloser, host string) {
}
}
}

func getHostFromFiles(targets *strslice, filenames *strslice) {
for _, filename := range *filenames {
f, err := os.Open(filename)
if err != nil {
fmt.Fprintln(os.Stderr, "[error]", filename, err)
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
targets.Set(scanner.Text())
}
if err = scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "[error]", err)
}
}
}

0 comments on commit 57cee53

Please sign in to comment.