Jump to content
JIHAD

Go Lang - Execute SSH command on server

Recommended Posts

Posted

package main

import (
"bytes"
"fmt"
"code.google.com/p/go.crypto/ssh"
"os"
)

func main() {
config := &ssh.ClientConfig{
User: os.Args[3],
Auth: []ssh.AuthMethod{
ssh.Password(os.Args[4]),
},
}
client, err := ssh.Dial("tcp", os.Args[1] + ":" + os.Args[2], config)
if err != nil {
panic("Failed to dial: " + err.Error())
}

session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
var b bytes.Buffer
session.Stdout = &b
if err := session.Run(os.Args[5]); err != nil {
panic("Failed to run: " + err.Error())
}
fmt.Println(b.String())
}

compile and execute: ssh.exe ip port user password "command"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...