Skip to content

Instantly share code, notes, and snippets.

@smeggingsmegger
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smeggingsmegger/4fa50da01db17d1deb92 to your computer and use it in GitHub Desktop.
Save smeggingsmegger/4fa50da01db17d1deb92 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os/exec"
)
type TunnelParams struct {
pem_key string
ssh_host string
ssh_user string
local_port string
mysql_host string
mysql_port string
}
func build_tunnel_command(p TunnelParams) string {
command := "ssh -N "
if p.pem_key != "" {
command += "-i " + p.pem_key + " "
}
if p.ssh_user != "" {
command += p.ssh_user + "@"
}
if p.ssh_host != "" {
command += p.ssh_host + " "
}
if p.local_port != "" {
command += "-L " + p.local_port + ":"
}
if p.mysql_host != "" {
command += p.mysql_host + ":"
}
if p.mysql_port != "" {
command += p.mysql_port
}
return command
}
// USAGE:
// cmd := create_tunnel(command)
// defer cmd.Process.Kill()
func create_tunnel(command string) *exec.Cmd {
fmt.Println("Executing command: " + command)
cmd := exec.Command("bash", "-c", command)
stdout, err := cmd.StdoutPipe()
cmd.Start()
if err != nil {
fmt.Println(err)
fmt.Println(stdout)
}
return cmd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment