Skip to content

Added one line to allow for interactive ssh #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion easyssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout ...time.Duration) (<-
stderrChan := make(chan string)
doneChan := make(chan bool)
errChan := make(chan error)

// connect to remote host
session, err := ssh_conf.Connect()
if err != nil {
return stdoutChan, stderrChan, doneChan, errChan, err
}
session.Stdin = os.Stdin
// defer session.Close()
// connect to both outputs (they are of type io.Reader)
outReader, err := session.StdoutPipe()
Expand Down
6 changes: 6 additions & 0 deletions example/interactive/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
for i in {1..5};do
echo "Hello, who are you?"
read varname
echo "Hi $varname `hostname -f`"
sleep 1
done
64 changes: 64 additions & 0 deletions example/interactive/ssh-cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/appleboy/easyssh-proxy"
"github.com/ogier/pflag"
)

func main() {
// Create MakeConfig instance with remote username, server address and path to private key.
server := pflag.StringP("server", "s", "localhost", "Server to SSH To.")
scriptFile := pflag.StringP("script", "S", "", "Script to run on remote server")
username := pflag.StringP("user", "u", os.Getenv("USER"), "Username")
keyfile := pflag.StringP("key", "k", os.Getenv("HOME")+"/.ssh/id_rsa", "SSH Key")
help := pflag.BoolP("help", "h", false, "Help")
pflag.Parse()
if *help {
pflag.PrintDefaults()
os.Exit(1)
}
ssh := &easyssh.MakeConfig{
Server: *server,
User: *username,
KeyPath: *keyfile,
Port: "22",
Timeout: 60 * time.Second,
}
content, err := ioutil.ReadFile(*scriptFile)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SA4006: this value of err is never used (from staticcheck)

// Call Run method with command you want to run on remote server.
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(string(content), 60*time.Second)
// Handle errors
if err != nil {
panic("Can't run remote command: " + err.Error())
} else {
// read from the output channel until the done signal is passed
isTimeout := true
loop:
for {
select {
case isTimeout = <-doneChan:
break loop
case outline := <-stdoutChan:
fmt.Println("out:", outline)
case errline := <-stderrChan:
fmt.Println("err:", errline)
case err = <-errChan:
}
}

// get exit code or command error.
if err != nil {
fmt.Println("err: " + err.Error())
}

// command time out
if !isTimeout {
fmt.Println("Error: command timeout")
}
}
}