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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| type SSHTerminal struct { Session *ssh.Session exitMsg string stdout io.Reader stdin io.Writer stderr io.Reader }
func main() { sshConfig := &ssh.ClientConfig{ User: "root", Auth: []ssh.AuthMethod{ ssh.Password("password"), }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), }
client, err := ssh.Dial("tcp", "192.168.1.20:22", sshConfig) if err != nil { fmt.Println(err) } defer client.Close() err = New(client) if err != nil { fmt.Println(err) } }
func (t *SSHTerminal) updateTerminalSize() {
go func() { sigwinchCh := make(chan os.Signal, 1) signal.Notify(sigwinchCh, syscall.SIGWINCH)
fd := int(os.Stdin.Fd()) termWidth, termHeight, err := terminal.GetSize(fd) if err != nil { fmt.Println(err) }
for { select { case sigwinch := <-sigwinchCh: if sigwinch == nil { return } currTermWidth, currTermHeight, err := terminal.GetSize(fd)
if currTermHeight == termHeight && currTermWidth == termWidth { continue }
t.Session.WindowChange(currTermHeight, currTermWidth) if err != nil { fmt.Printf("Unable to send window-change reqest: %s.", err) continue }
termWidth, termHeight = currTermWidth, currTermHeight
} } }()
}
func (t *SSHTerminal) interactiveSession() error {
defer func() { if t.exitMsg == "" { fmt.Fprintln(os.Stdout, "the connection was closed on the remote side on ", time.Now().Format(time.RFC822)) } else { fmt.Fprintln(os.Stdout, t.exitMsg) } }()
fd := int(os.Stdin.Fd()) state, err := terminal.MakeRaw(fd) if err != nil { return err } defer terminal.Restore(fd, state)
termWidth, termHeight, err := terminal.GetSize(fd) if err != nil { return err }
termType := os.Getenv("TERM") if termType == "" { termType = "xterm-256color" }
err = t.Session.RequestPty(termType, termHeight, termWidth, ssh.TerminalModes{}) if err != nil { return err }
t.updateTerminalSize()
t.stdin, err = t.Session.StdinPipe() if err != nil { return err } t.stdout, err = t.Session.StdoutPipe() if err != nil { return err } t.stderr, err = t.Session.StderrPipe()
go io.Copy(os.Stderr, t.stderr) go io.Copy(os.Stdout, t.stdout) go func() { buf := make([]byte, 128) for { n, err := os.Stdin.Read(buf) if err != nil { fmt.Println(err) return } if n > 0 { _, err = t.stdin.Write(buf[:n]) if err != nil { fmt.Println(err) t.exitMsg = err.Error() return } } } }()
err = t.Session.Shell() if err != nil { return err } err = t.Session.Wait() if err != nil { return err } return nil }
func New(client *ssh.Client) error {
session, err := client.NewSession() if err != nil { return err } defer session.Close()
s := SSHTerminal{ Session: session, }
return s.interactiveSession() }
|