gosshmfa

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-ssh-mfa

A wrapper package for the ssh package which allows you to ssh into machines/servers which requires MFA (duo-app/yubikey/google authenticator).

install

go get -u github.com/vinaykulk621/go-ssh-mfa
This package wraps all the methods offered by ssh package's clent and session type (i hope, i did not miss any) along with two new methods Run and RunCmds.

Example usage

configuring credentials
type SSHConfig struct {
	User       string // username through which ssh connection is made
	Protocol   string // takes one of the two values "tcp" or "udp"
	RemoteAddr string // IP address or server name(FQDN)
	Port       int    // port to which ssh connection should be made
}
running a single a command using Run() will execute your command something like this: /bin/bash; <yourCmd>; exit.

A Session only accepts one call to Run, Start or Shell. If you want to run multiple commands at once, checkout RunCmds().

package main

import (
    "fmt"
	"log"

	gosshmfa "github.com/vinaykulk621/go-ssh-mfa"
)

func main() {
	config := &gosshmfa.SSHConfig{
		User:       "user",
		Protocol:   "tcp",
		RemoteAddr: "my-server-name",
		Port:       22,
	}
	client, err := gosshmfa.MakeSSHConnection(config)
	if err != nil {
		log.Fatalf("failed to ssh\n\t\terr: %s", err.Error())
	}
	defer client.Close()

	session, err := client.MakeNewSession()
	if err != nil {
		log.Fatalf("failed to create a session\n\t\terr: %s", err.Error())
	}
	defer session.Close()

	stdout, stdErr := session.Run("ls")
	fmt.Println(stdout.String(), stdErr.String())
}
running multiple commands or chaining commands can be achieved through RunCmds().
  • RunCmds() will loop through all the commands, and for the last command it will execute: /bin/bash; <yourCmd>; exit.
  • RunCmds() will internally create new sessions for each command, as a session only accepts one call to Run.
  • RunCmds() returns 2 maps with commands as keys:
    • stdOutMap contains the outputs (if any) or empty string for each command.
    • stdErrMap contains the errors (if any) or empty string for each command.
// takes slice of commands as input
//
// returns two maps. one for standard output of
// the commands executed and one if any errors occured
// if no error or output, val will be an empty string
//   - key -- commad
//   - val -- output/error
func main(){
	...

	session, err := ...
	...

	stdOutMap, stdErrMap := session.RunCmds(cmds)
	...
}

main.go

func main() {
	cmds := []string{"ls", "cat ab.txt", "echo 'hi'"}
	config := &gosshmfa.SSHConfig{...}

	client, err := gosshmfa.MakeSSHConnection(config)
	if err != nil { ...	}
	defer client.Close()

	session, err := client.MakeNewSession()
	if err != nil { ...	}
	defer session.Close()

	stdOutMap, stdErrMap := session.RunCmds(cmds)
	fmt.Println(stdOutMap, stdErrMap)
}

Documentation

Overview

package gosshmfa is a wrapper for ssh package of the golang which allows you to ssh into mchines which requires MFA

author: Vinay Kulkarni<kulkarnivinay621@gmail.com>

This package implements everything (almost (i hope so)) you can with ssh package of go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeSSHConnection

func MakeSSHConnection(sshConf *SSHConfig) (*client, error)

create an SSH client

Types

type SSHConfig

type SSHConfig struct {
	User       string // username through which ssh connection is made
	Protocol   string // takes one of the two values "tcp" or "udp"
	RemoteAddr string // IP address or server name(FQDN)
	Port       int    // port to which ssh connection should be made
}

user should provide all these details to make the ssh connection

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL