Last active
August 29, 2015 14:00
-
-
Save anuaimi/096c1031a3b95430298b to your computer and use it in GitHub Desktop.
ssh into a server and run command. code is written in go and shows: password based login, key based login and running on multiple servers
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
// example code to use ssh to run command on a remote server | |
// includes password based and key based login | |
package main | |
import ( | |
"log" | |
"fmt" | |
"io/ioutil" | |
"bytes" | |
"code.google.com/p/go.crypto/ssh" | |
"github.com/anuaimi/mssh" | |
) | |
const ( | |
VINTR = 1 | |
VQUIT = 2 | |
VERASE = 3 | |
VKILL = 4 | |
VEOF = 5 | |
VEOL = 6 | |
VEOL2 = 7 | |
VSTART = 8 | |
VSTOP = 9 | |
VSUSP = 10 | |
VDSUSP = 11 | |
VREPRINT = 12 | |
VWERASE = 13 | |
VLNEXT = 14 | |
VFLUSH = 15 | |
VSWTCH = 16 | |
VSTATUS = 17 | |
VDISCARD = 18 | |
IGNPAR = 30 | |
PARMRK = 31 | |
INPCK = 32 | |
ISTRIP = 33 | |
INLCR = 34 | |
IGNCR = 35 | |
ICRNL = 36 | |
IUCLC = 37 | |
IXON = 38 | |
IXANY = 39 | |
IXOFF = 40 | |
IMAXBEL = 41 | |
ISIG = 50 | |
ICANON = 51 | |
XCASE = 52 | |
ECHO = 53 | |
ECHOE = 54 | |
ECHOK = 55 | |
ECHONL = 56 | |
NOFLSH = 57 | |
TOSTOP = 58 | |
IEXTEN = 59 | |
ECHOCTL = 60 | |
ECHOKE = 61 | |
PENDIN = 62 | |
OPOST = 70 | |
OLCUC = 71 | |
ONLCR = 72 | |
OCRNL = 73 | |
ONOCR = 74 | |
ONLRET = 75 | |
CS7 = 90 | |
CS8 = 91 | |
PARENB = 92 | |
PARODD = 93 | |
TTY_OP_ISPEED = 128 | |
TTY_OP_OSPEED = 129 | |
) | |
// create a ssh config that uses password based login | |
func createPasswordConfig() (*ssh.ClientConfig) { | |
return &ssh.ClientConfig{ | |
User: "vagrant", | |
Auth: []ssh.AuthMethod{ | |
ssh.Password("vagrant"), | |
}, | |
} | |
} | |
// create a ssh config that uses keypair to login | |
func createKeyConfig() (*ssh.ClientConfig) { | |
// read private key | |
var sshPrivateKey = "/Users/anuaimi/.ssh/id_rsa" | |
privateKey, err := ioutil.ReadFile(sshPrivateKey) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// parse private key | |
signer, err := ssh.ParsePrivateKey(privateKey) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// details of where we want to connect to | |
return &ssh.ClientConfig{ | |
User: "anuaimi", | |
Auth: []ssh.AuthMethod{ | |
// ssh.Password("vagrant"), | |
ssh.PublicKeys(signer), | |
}, | |
} | |
} | |
func runCommandOnServer() { | |
// details of where we want to connect to | |
// use a password | |
// config := createPasswordConfig() | |
// use a keypair | |
config := createKeyConfig() | |
// connect to server | |
client, err := ssh.Dial( "tcp", "127.0.0.1:2222", config) | |
if err != nil { | |
panic("Failed to dial: " + err.Error()) | |
} | |
defer client.Close() | |
// start session | |
session, err := client.NewSession() | |
if err != nil { | |
log.Fatalf("unable to create session: " + err.Error()) | |
} | |
defer session.Close() | |
// get a psuedo terminal | |
modes := ssh.TerminalModes { | |
ECHO: 0, // disable echo | |
TTY_OP_ISPEED: 14400, // input speed 14.4k | |
TTY_OP_OSPEED: 14400, // output speed 14.4K | |
} | |
if err := session.RequestPty("xterm", 80, 40, modes); err != nil { | |
log.Fatalf("request for psuedo terminal failed: %s", err) | |
} | |
// run command | |
var b bytes.Buffer | |
session.Stdout = &b | |
if err := session.Run("cat /etc/debian_version"); err != nil { | |
panic("Failed to run: " + err.Error()) | |
} | |
fmt.Println(b.String()) | |
} | |
func main() { | |
// | |
// run on a single server | |
// | |
runCommandOnServer() | |
// | |
// now show how we can run on multiple servers | |
// | |
// get list of hosts we want to run command on | |
hosts := mssh.GetHostsByFile("hostlist") | |
// setup options | |
var conf mssh.Conf | |
conf.User = "anuaimi" | |
conf.Key = "/Users/anuaimi/.ssh/id_rsa" | |
conf.Sudo = false | |
conf.Debug = false | |
// command to run | |
conf.Command = "cat /etc/debian_version" | |
// actually run the command on all the hosts | |
mssh.Ssh(hosts, conf) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment