Created
January 30, 2015 17:29
-
-
Save michaljemala/c5f5a5586f528dffa3f3 to your computer and use it in GitHub Desktop.
SSH Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
// "github.com/cloudfoundry/cli/plugin" | |
"golang.org/x/crypto/ssh" | |
"golang.org/x/net/websocket" | |
) | |
func main() { | |
appUrl := "ssh-server.cfapps.io" | |
origin := fmt.Sprintf("http://%s", appUrl) | |
addr := fmt.Sprintf("%s:4443", appUrl) | |
url := fmt.Sprintf("wss://%s/ssh", addr) | |
log.Println("Connecting...") | |
conn, err := websocket.Dial(url, "", origin) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println("OK") | |
log.Println("SSH...") | |
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, &ssh.ClientConfig{ | |
User: "foo", | |
Auth: []ssh.AuthMethod{ssh.Password("bar")}, | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println("OK") | |
client := ssh.NewClient(sshConn, chans, reqs) | |
defer client.Close() | |
log.Println("SSH Session...") | |
session, err := client.NewSession() | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer session.Close() | |
log.Println("OK") | |
modes := ssh.TerminalModes{ | |
ssh.ECHO: 0, // disable echoing | |
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud | |
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud | |
} | |
if err := session.RequestPty("xterm", 80, 40, modes); err != nil { | |
log.Fatal(err) | |
} | |
if err := session.Shell(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment