2016-03-23 23:40:38 +08:00
|
|
|
package ipmi_sensor
|
2016-03-17 23:45:29 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Connection properties for a Client
|
|
|
|
|
type Connection struct {
|
|
|
|
|
Hostname string
|
|
|
|
|
Username string
|
|
|
|
|
Password string
|
|
|
|
|
Port int
|
|
|
|
|
Interface string
|
2018-01-06 07:59:25 +08:00
|
|
|
Privilege string
|
2020-12-22 00:45:58 +08:00
|
|
|
HexKey string
|
2016-03-17 23:45:29 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-22 00:45:58 +08:00
|
|
|
func NewConnection(server, privilege, hexKey string) *Connection {
|
|
|
|
|
conn := &Connection{
|
|
|
|
|
Privilege: privilege,
|
|
|
|
|
HexKey: hexKey,
|
|
|
|
|
}
|
2017-04-07 05:40:34 +08:00
|
|
|
inx1 := strings.LastIndex(server, "@")
|
2016-03-17 23:45:29 +08:00
|
|
|
inx2 := strings.Index(server, "(")
|
|
|
|
|
|
|
|
|
|
connstr := server
|
|
|
|
|
|
|
|
|
|
if inx1 > 0 {
|
|
|
|
|
security := server[0:inx1]
|
2018-10-20 04:32:54 +08:00
|
|
|
connstr = server[inx1+1:]
|
2016-04-05 21:24:24 +08:00
|
|
|
up := strings.SplitN(security, ":", 2)
|
2021-03-23 22:09:51 +08:00
|
|
|
if len(up) == 2 {
|
|
|
|
|
conn.Username = up[0]
|
|
|
|
|
conn.Password = up[1]
|
|
|
|
|
}
|
2016-03-17 23:45:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inx2 > 0 {
|
|
|
|
|
inx2 = strings.Index(connstr, "(")
|
2020-10-08 23:20:35 +08:00
|
|
|
inx3 := strings.Index(connstr, ")")
|
2016-03-17 23:45:29 +08:00
|
|
|
|
|
|
|
|
conn.Interface = connstr[0:inx2]
|
|
|
|
|
conn.Hostname = connstr[inx2+1 : inx3]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conn
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
func (c *Connection) options() []string {
|
|
|
|
|
intf := c.Interface
|
2016-03-17 23:45:29 +08:00
|
|
|
if intf == "" {
|
|
|
|
|
intf = "lan"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options := []string{
|
2021-02-17 07:19:50 +08:00
|
|
|
"-H", c.Hostname,
|
|
|
|
|
"-U", c.Username,
|
|
|
|
|
"-P", c.Password,
|
2016-03-17 23:45:29 +08:00
|
|
|
"-I", intf,
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
if c.HexKey != "" {
|
|
|
|
|
options = append(options, "-y", c.HexKey)
|
2020-12-22 00:45:58 +08:00
|
|
|
}
|
2021-02-17 07:19:50 +08:00
|
|
|
if c.Port != 0 {
|
|
|
|
|
options = append(options, "-p", strconv.Itoa(c.Port))
|
2016-03-17 23:45:29 +08:00
|
|
|
}
|
2021-02-17 07:19:50 +08:00
|
|
|
if c.Privilege != "" {
|
|
|
|
|
options = append(options, "-L", c.Privilege)
|
2018-01-06 07:59:25 +08:00
|
|
|
}
|
2016-03-17 23:45:29 +08:00
|
|
|
return options
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemoteIP returns the remote (bmc) IP address of the Connection
|
|
|
|
|
func (c *Connection) RemoteIP() string {
|
|
|
|
|
if net.ParseIP(c.Hostname) == nil {
|
|
|
|
|
addrs, err := net.LookupHost(c.Hostname)
|
|
|
|
|
if err != nil && len(addrs) > 0 {
|
|
|
|
|
return addrs[0]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return c.Hostname
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LocalIP returns the local (client) IP address of the Connection
|
|
|
|
|
func (c *Connection) LocalIP() string {
|
|
|
|
|
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", c.Hostname, c.Port))
|
|
|
|
|
if err != nil {
|
|
|
|
|
// don't bother returning an error, since this value will never
|
|
|
|
|
// make it to the bmc if we can't connect to it.
|
|
|
|
|
return c.Hostname
|
|
|
|
|
}
|
|
|
|
|
_ = conn.Close()
|
2024-07-10 18:51:25 +08:00
|
|
|
//nolint:errcheck // unable to propagate
|
2016-03-17 23:45:29 +08:00
|
|
|
host, _, _ := net.SplitHostPort(conn.LocalAddr().String())
|
|
|
|
|
return host
|
|
|
|
|
}
|