Last active
August 29, 2015 14:01
-
-
Save smeggingsmegger/4fa50da01db17d1deb92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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