Skip to content

Commit

Permalink
add logic of ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
AshutoshPatole committed Jun 27, 2024
1 parent ec56a89 commit 879a7f7
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Package cmd /*
package cmd

import (
"errors"
"github.com/TwiN/go-color"
"github.com/spf13/cobra"
"log"
"ssm-v2/internal/ssh"
)

var (
username string
group string
alias string
environment string
allowedEnvironmentValues = []string{"dev", "staging", "prod"}
)

// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "A brief description of your command",
Long: ``,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New(color.InRed("Requires hostname of the machine"))
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
validEnvironment := false
for _, env := range allowedEnvironmentValues {
if env == environment {
validEnvironment = true
}
}
if !validEnvironment {
log.Fatal(color.InRed("Invalid environment value"))
}
addServer(args[0])
},
}

func addServer(host string) {
password, err := ssh.AskPassword()
if err != nil {
log.Fatal(color.InRed("Error reading password"))
}
ssh.InitSSHConnection(username, password, host)
}

func init() {
rootCmd.AddCommand(addCmd)
addCmd.Flags().StringVarP(&username, "username", "u", "root", "Username to use")
addCmd.Flags().StringVarP(&group, "group", "g", "", "Group to use")
addCmd.Flags().StringVarP(&alias, "alias", "a", "", "Alias to use")
addCmd.Flags().StringVarP(&environment, "environment", "e", "dev", "Environment to use")
_ = addCmd.MarkFlagRequired("group")
_ = addCmd.MarkFlagRequired("alias")
}
11 changes: 2 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
Copyright © 2024 Ashutosh Patole <apatole479@gmail.com>
*/
// Package cmd /*
package cmd

import (
Expand All @@ -17,12 +15,7 @@ var cfgFile string
var rootCmd = &cobra.Command{
Use: "ssm-v2",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Long: ``,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module ssm-v2
go 1.22

require (
github.com/TwiN/go-color v1.4.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
golang.org/x/crypto v0.24.0
)

require (
Expand All @@ -24,8 +26,9 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/TwiN/go-color v1.4.1 h1:mqG0P/KBgHKVqmtL5ye7K0/Gr4l6hTksPgTgMk3mUzc=
github.com/TwiN/go-color v1.4.1/go.mod h1:WcPf/jtiW95WBIsEeY1Lc/b8aaWoiqQpu5cf8WFxu+s=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -61,12 +63,16 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
17 changes: 17 additions & 0 deletions internal/ssh/askpass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ssh

import (
"fmt"
"golang.org/x/crypto/ssh/terminal"
"os"
)

func AskPassword() (string, error) {
fmt.Print("Password: ")
password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
fmt.Print("\n")
return string(password), nil
}
50 changes: 50 additions & 0 deletions internal/ssh/ssh-connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ssh

import (
"fmt"
"github.com/TwiN/go-color"
"golang.org/x/crypto/ssh"
"log"
"os"
"time"
)

const SshPort = 22

func InitSSHConnection(user, password, host string) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
HostKeyAlgorithms: []string{ssh.KeyAlgoRSA, ssh.KeyAlgoDSA, ssh.KeyAlgoED25519, ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521},
Timeout: 5 * time.Second,
}
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, SshPort), config)
if err != nil {
log.Fatal(color.InRed(err.Error()))
}
defer func(client *ssh.Client) {
err := client.Close()
if err != nil {
log.Fatalf(color.InRed("Failed to close SSH connection"))
}
}(client)

session, err := client.NewSession()
if err != nil {
log.Fatal(color.InRed(err.Error()))
}

defer func(session *ssh.Session) {
err := session.Close()
if err != nil {
log.Fatalf(color.InRed("Failed to close SSH connection"))
}
}(session)

session.Stdout = os.Stdout
session.Stderr = os.Stderr

}

0 comments on commit 879a7f7

Please sign in to comment.