github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/ssh.go (about) 1 package util 2 3 import ( 4 "regexp" 5 6 "github.com/pkg/errors" 7 ) 8 9 const ( 10 DefaultSSHPort = "22" 11 ) 12 13 // StaticHostInfo stores the connection parameters for connecting to an SSH host. 14 type StaticHostInfo struct { 15 User string 16 Hostname string 17 Port string 18 } 19 20 var userHostPortRegex = regexp.MustCompile(`(?:([\w\-_]+)@)?@?([\w\-_\.]+)(?::(\d+))?`) 21 22 // ParseSSHInfo reads in a hostname definition and reads the relevant 23 // SSH connection information from it. For example, 24 // "admin@myhostaddress:24" 25 // will return 26 // StaticHostInfo{User: "admin", Hostname:"myhostaddress", Port: 24} 27 func ParseSSHInfo(fullHostname string) (*StaticHostInfo, error) { 28 matches := userHostPortRegex.FindStringSubmatch(fullHostname) 29 if len(matches) == 0 { 30 return nil, errors.Errorf("Invalid hostname format: %v", fullHostname) 31 } else { 32 returnVal := &StaticHostInfo{ 33 User: matches[1], 34 Hostname: matches[2], 35 Port: matches[3], 36 } 37 if returnVal.Port == "" { 38 returnVal.Port = DefaultSSHPort 39 } 40 return returnVal, nil 41 } 42 }