【博客427】通过redfish协议操控服务器
https://github.com/stmcginnis/gofishChange Loginexample2:Query Chassisexample3:Query Sessionexample4:Reboot
·
通过redfish协议操控服务器
开源库链接
https://github.com/stmcginnis/gofish
方式:通过gofish开源库,底层使用redfish协议操控服务器
example1:
Change Login
//
// SPDX-License-Identifier: BSD-3-Clause
//
package main
import (
"github.com/stmcginnis/gofish"
)
func main() {
// Create a new instance of gofish client, ignoring self-signed certs
username := "my-username"
config := gofish.ClientConfig{
Endpoint: "https://bmc-ip",
Username: username,
Password: "my-password",
Insecure: true,
}
c, err := gofish.Connect(config)
if err != nil {
panic(err)
}
defer c.Logout()
// Retrieve the service root
service := c.Service
// Query the AccountService using the session token
accountService, err := service.AccountService()
if err != nil {
panic(err)
}
// Get list of accounts
accounts, err := accountService.Accounts()
if err != nil {
panic(err)
}
// Iterate over accounts to find the current user
for _, account := range accounts {
if account.UserName == username {
account.UserName = "new-username"
// New password must follow the rules set in AccountService :
// MinPasswordLength and MaxPasswordLength
account.Password = "new-password"
err := account.Update()
if err != nil {
panic(err)
}
}
}
}
example2:
Query Chassis
//
// SPDX-License-Identifier: BSD-3-Clause
//
package main
import (
"fmt"
"github.com/stmcginnis/gofish"
)
func main() {
// Create a new instance of gofish client, ignoring self-signed certs
config := gofish.ClientConfig{
Endpoint: "https://bmc-ip",
Username: "my-username",
Password: "my-password",
Insecure: true,
}
c, err := gofish.Connect(config)
if err != nil {
panic(err)
}
defer c.Logout()
// Retrieve the service root
service := c.Service
// Query the chassis data using the session token
chassis, err := service.Chassis()
if err != nil {
panic(err)
}
for _, chass := range chassis {
fmt.Printf("Chassis: %#v\n\n", chass)
}
}
example3:
Query Session
//
// SPDX-License-Identifier: BSD-3-Clause
//
package main
import (
"fmt"
"github.com/stmcginnis/gofish"
)
func main() {
// Create a new instance of gofish client, ignoring self-signed certs
config := gofish.ClientConfig{
Endpoint: "https://bmc-ip",
Username: "my-username",
Password: "my-password",
Insecure: true,
}
c, err := gofish.Connect(config)
if err != nil {
panic(err)
}
defer c.Logout()
// Retrieve the service root
service := c.Service
// Query the active sessions using the session token
sessions, err := service.Sessions()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", sessions)
for _, session := range sessions {
fmt.Printf("Sessions: %#v\n\n", session)
}
}
example4:
Reboot
//
// SPDX-License-Identifier: BSD-3-Clause
//
package main
import (
"fmt"
"github.com/stmcginnis/gofish"
"github.com/stmcginnis/gofish/redfish"
)
func main() {
// Create a new instance of gofish client, ignoring self-signed certs
config := gofish.ClientConfig{
Endpoint: "https://bmc-ip",
Username: "my-username",
Password: "my-password",
Insecure: true,
}
c, err := gofish.Connect(config)
if err != nil {
panic(err)
}
defer c.Logout()
// Attached the client to service root
service := c.Service
// Query the computer systems
ss, err := service.Systems()
if err != nil {
panic(err)
}
// Creates a boot override to pxe once
bootOverride := redfish.Boot{
BootSourceOverrideTarget: redfish.PxeBootSourceOverrideTarget,
BootSourceOverrideEnabled: redfish.OnceBootSourceOverrideEnabled,
}
for _, system := range ss {
fmt.Printf("System: %#v\n\n", system)
err := system.SetBoot(bootOverride)
if err != nil {
panic(err)
}
err = system.Reset(redfish.ForceRestartResetType)
if err != nil {
panic(err)
}
}
}
更多推荐
所有评论(0)