2016-03-23 23:40:38 +08:00
|
|
|
package ipmi_sensor
|
2016-03-17 23:45:29 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-25 18:45:08 +08:00
|
|
|
// connection properties for a Client
|
|
|
|
|
type connection struct {
|
|
|
|
|
hostname string
|
|
|
|
|
username string
|
|
|
|
|
password string
|
|
|
|
|
port int
|
|
|
|
|
intf string
|
|
|
|
|
privilege string
|
|
|
|
|
hexKey string
|
2016-03-17 23:45:29 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-25 18:45:08 +08:00
|
|
|
func newConnection(server, privilege, hexKey string) *connection {
|
|
|
|
|
conn := &connection{
|
|
|
|
|
privilege: privilege,
|
|
|
|
|
hexKey: hexKey,
|
2020-12-22 00:45:58 +08:00
|
|
|
}
|
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 {
|
2024-10-25 18:45:08 +08:00
|
|
|
conn.username = up[0]
|
|
|
|
|
conn.password = up[1]
|
2021-03-23 22:09:51 +08:00
|
|
|
}
|
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
|
|
|
|
2024-10-25 18:45:08 +08:00
|
|
|
conn.intf = connstr[0:inx2]
|
|
|
|
|
conn.hostname = connstr[inx2+1 : inx3]
|
2016-03-17 23:45:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conn
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 18:45:08 +08:00
|
|
|
func (c *connection) options() []string {
|
|
|
|
|
intf := c.intf
|
2016-03-17 23:45:29 +08:00
|
|
|
if intf == "" {
|
|
|
|
|
intf = "lan"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options := []string{
|
2024-10-25 18:45:08 +08:00
|
|
|
"-H", c.hostname,
|
|
|
|
|
"-U", c.username,
|
|
|
|
|
"-P", c.password,
|
2016-03-17 23:45:29 +08:00
|
|
|
"-I", intf,
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 18:45:08 +08:00
|
|
|
if c.hexKey != "" {
|
|
|
|
|
options = append(options, "-y", c.hexKey)
|
2020-12-22 00:45:58 +08:00
|
|
|
}
|
2024-10-25 18:45:08 +08:00
|
|
|
if c.port != 0 {
|
|
|
|
|
options = append(options, "-p", strconv.Itoa(c.port))
|
2016-03-17 23:45:29 +08:00
|
|
|
}
|
2024-10-25 18:45:08 +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
|
|
|
|
|
}
|