Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Lang exec/spawn a ssh session

Tags:

fork

ssh

go

lang

I'm trying to work out the mechanism to fork/start a ssh terminal session i.e I want to be logged into remote server (my keys are on server) if I execute this program.

Right now it just executes but nothing happens.

package main

import (
 "os/exec"   
 "os"
)

func main() {
  cmd := exec.Command("ssh","root@SERVER-IP")
  cmd.Stdout = os.Stdout
  //cmd.Stderr = os.Stderr
  cmd.Run()

}
like image 815
user914584 Avatar asked Dec 28 '25 16:12

user914584


1 Answers

cmd.Run waits for the command to complete. Your ssh session should (normally) not exit without user interaction. Therefore your program blocks, since it waits for the ssh process to finish.

You may want to either

  • also redirect Stdin, so you can interact with the ssh session
  • execute ssh me@server somecommand. In the last form a specific command gets executed and the output of this command gets redirected.
  • take a look at the ssh package
like image 64
tike Avatar answered Dec 31 '25 17:12

tike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!