-
Notifications
You must be signed in to change notification settings - Fork 4
/
sshcmd.go
134 lines (123 loc) · 2.23 KB
/
sshcmd.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bufio"
"flag"
"fmt"
"github.com/scottkiss/gosshtool"
"golang.org/x/crypto/ssh"
"io"
"net"
"os"
"strings"
"time"
)
var quit bool
const BUFFER_SIZE = 1024 * 4
var buffer = make([]byte, BUFFER_SIZE)
var (
host string
passwd string
user string
)
func main() {
flag.StringVar(&host, "h", "", "host")
flag.StringVar(&passwd, "p", "", "password")
flag.Parse()
hostsp := strings.Split(host, "@")
user = hostsp[0]
host = hostsp[1]
server := &server{
Host: ":9999",
}
go server.Start()
time.Sleep(time.Second)
conn, err := net.Dial("tcp", ":9999")
if err != nil {
fmt.Println(err)
}
defer conn.Close()
fmt.Println("connected to ssh server!")
go onMessageRecived(conn)
for {
if quit {
break
}
inputReader := bufio.NewReader(os.Stdin)
input, err := inputReader.ReadString('\n')
if err != nil {
fmt.Println("There ware errors reading.")
return
}
b := []byte(input)
conn.Write(b)
}
}
func onMessageRecived(conn net.Conn) {
buffer = make([]byte, BUFFER_SIZE)
for {
n, err := conn.Read(buffer)
if err == io.EOF {
fmt.Printf("The RemoteAddr: %s is closed!\n", conn.RemoteAddr().String())
return
}
if err != nil {
break
}
if n > 0 {
str := string(buffer[:n])
fmt.Printf("%s", str)
if strings.Contains(str, "logout") {
quit = true
}
} else {
break
}
}
}
type server struct {
Host string
}
func handleConn(conn net.Conn) {
config := &gosshtool.SSHClientConfig{
User: user,
Password: passwd,
Host: host,
}
sshclient := gosshtool.NewSSHClient(config)
_, err := sshclient.Connect()
if err == nil {
fmt.Println("ssh connect success")
} else {
fmt.Println("ssh connect failed")
}
modes := ssh.TerminalModes{
ssh.ECHO: 0,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
pty := &gosshtool.PtyInfo{
Term: "xterm-256color",
H: 80,
W: 40,
Modes: modes,
}
session, err := sshclient.Pipe(conn, pty, nil, 30)
if err != nil {
fmt.Println(err)
}
defer session.Close()
}
func (s *server) Start() {
ln, err := net.Listen("tcp", s.Host)
if err != nil {
fmt.Println(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
}
go handleConn(conn)
}
}