Guest User

Untitled

a guest
Mar 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "net"
  7. "os"
  8.  
  9. "github.com/go-sql-driver/mysql"
  10. "golang.org/x/crypto/ssh"
  11. "golang.org/x/crypto/ssh/agent"
  12. )
  13.  
  14. type ViaSSHDialer struct {
  15. client *ssh.Client
  16. }
  17.  
  18. func (self *ViaSSHDialer) Dial(addr string) (net.Conn, error) {
  19. return self.client.Dial("tcp", addr)
  20. }
  21.  
  22. func main() {
  23.  
  24. sshHost := "example.com" // SSH Server Hostname/IP
  25. sshPort := 22 // SSH Port
  26. sshUser := "ssh-user" // SSH Username
  27. sshPass := "ssh-pass" // Empty string for no password
  28. dbUser := "dbuser" // DB username
  29. dbPass := "dbpass" // DB Password
  30. dbHost := "localhost:3306" // DB Hostname/IP
  31. dbName := "database" // Database name
  32.  
  33. var agentClient agent.Agent
  34. // Establish a connection to the local ssh-agent
  35. if conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
  36. defer conn.Close()
  37.  
  38. // Create a new instance of the ssh agent
  39. agentClient = agent.NewClient(conn)
  40. }
  41.  
  42. // The client configuration with configuration option to use the ssh-agent
  43. sshConfig := &ssh.ClientConfig{
  44. User: sshUser,
  45. Auth: []ssh.AuthMethod{},
  46. }
  47.  
  48. // When the agentClient connection succeeded, add them as AuthMethod
  49. if agentClient != nil {
  50. sshConfig.Auth = append(sshConfig.Auth, ssh.PublicKeysCallback(agentClient.Signers))
  51. }
  52. // When there's a non empty password add the password AuthMethod
  53. if sshPass != "" {
  54. sshConfig.Auth = append(sshConfig.Auth, ssh.PasswordCallback(func() (string, error) {
  55. return sshPass, nil
  56. }))
  57. }
  58.  
  59. // Connect to the SSH Server
  60. if sshcon, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", sshHost, sshPort), sshConfig); err == nil {
  61. defer sshcon.Close()
  62.  
  63. // Now we register the ViaSSHDialer with the ssh connection as a parameter
  64. mysql.RegisterDial("mysql+tcp", (&ViaSSHDialer{sshcon}).Dial)
  65.  
  66. // And now we can use our new driver with the regular mysql connection string tunneled through the SSH connection
  67. if db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@mysql+tcp(%s)/%s", dbUser, dbPass, dbHost, dbName)); err == nil {
  68.  
  69. fmt.Printf("Successfully connected to the db\n")
  70.  
  71. if rows, err := db.Query("SELECT id, name FROM table ORDER BY id"); err == nil {
  72. for rows.Next() {
  73. var id int64
  74. var name string
  75. rows.Scan(&id, &name)
  76. fmt.Printf("ID: %d Name: %s\n", id, name)
  77. }
  78. rows.Close()
  79. } else {
  80. fmt.Printf("Failure: %s", err.Error())
  81. }
  82.  
  83. db.Close()
  84.  
  85. } else {
  86.  
  87. fmt.Printf("Failed to connect to the db: %s\n", err.Error())
  88. }
  89.  
  90. }
  91. }
Add Comment
Please, Sign In to add comment