Golang lsof server

2014-11-24 00:04:11 · 作者: · 浏览: 19

01 package main
02
03 import (
04 "net/http"
05 "fmt"
06 "os/exec"
07 "flag"
08 )
09
10 func main(){
11 http.HandleFunc("/", readHandle)
12 port := flag.Int("port", 2012, "Listen Port")
13 flag.Parse()
14 http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
15
16 }
17

18
19 func readHandle(w http.ResponseWriter, r *http.Request){
20 /* Command */
21 lsof := exec.Command("lsof", r.FormValue("path"))
22 wc := exec.Command("wc", "-l")
23 /* Pipe */
24 lsofOut,_ := lsof.StdoutPipe()
25 /* Command Start */
26 lsof.Start()
27 /* Stdin */
28 wc.Stdin = lsofOut
29 out,_ := wc.Output()
30
31 fmt.Fprintf(w, "%v", r.FormValue("path"))
32 fmt.Fprintf(w, "%s", out)
33 fmt.Fprintf(w, "%s", r.URL.Path[1:])
34 }

作者:ioser