Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "os"
- "fmt"
- "flag"
- "golang.org/x/crypto/ssh"
- "io"
- "log"
- "strings"
- "time"
- "encoding/xml"
- "bufio"
- "net"
- )
- // SSH connection struct
- type SSHConnections struct {
- Host string
- User string
- Password string
- CLILogfile string
- SSHConn ssh.Client
- SSHConfig ssh.ClientConfig
- }
- func main() {
- //parse list of addresses, get usernames and passwords
- var ipaddr = []string{"a.b.c.d","e.f.g.h"} //hard-coded for now, will receive list from external source later
- //build out SSHConnections struct
- for i := 0; i < len(ipaddr); i++ {
- tempsshConfig := &ssh.ClientConfig{
- User: "administrator",
- Auth: []ssh.AuthMethod{
- ssh.Password("Password!"),
- },
- HostKeyCallback: ssh.InsecureIgnoreHostKey(), //FUTURECHECK allows connections to all machines, reconsider for security's sake
- }
- tempsshConfig.Config.Ciphers = append(tempsshConfig.Config.Ciphers, "aes128-cbc")
- var newitem = SSHConnections{
- Host: ipaddr[i],
- User: "administrator",
- Password: "Password!",
- CLILogfile: ipaddr[1]+".txt",
- SSHConn: ssh.Client,
- SSHConfig: *tempsshConfig,
- }
- SSHConnections = append(SSHConnections, newitem)
- }
- type ssh.Client is not an expression
- type SSHConnections is not an expression
- //extra SSH parameters required before connecting
- sshConfig.Config.Ciphers = append(sshConfig.Config.Ciphers, "aes128-cbc")
- 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
- }
- //prepare logfiles
- f, ferr := os.OpenFile("outputfile.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
- if ferr != nil {
- panic(ferr)
- }
- defer f.Close()
- //SSH connection procedure
- connection, err := ssh.Dial("tcp", hostname+":22", sshConfig)
- if err != nil {
- log.Fatalf("Failed to dial: %s", err)
- }
- session, err := connection.NewSession()
- handleError(err, true, "Failed to create session: %s")
- sshOut, err := session.StdoutPipe()
- handleError(err, true, "Unable to setup stdin for session: %v")
- sshIn, err := session.StdinPipe()
- handleError(err, true, "Unable to setup stdout for session: %v")
- if err := session.RequestPty("xterm", 0, 200, modes); err != nil {
- session.Close()
- handleError(err, true, "request for pseudo terminal failed: %s")
- }
- if err := session.Shell(); err != nil {
- session.Close()
- handleError(err, true, "request for shell failed: %s")
- }
Add Comment
Please, Sign In to add comment