Skip to content

Instantly share code, notes, and snippets.

@joelrebel
Last active October 24, 2018 09:34
Show Gist options
  • Save joelrebel/e08b0a1ea02f9be7e041accaa8a57bb7 to your computer and use it in GitHub Desktop.
Save joelrebel/e08b0a1ea02f9be7e041accaa8a57bb7 to your computer and use it in GitHub Desktop.
// Grabs screen previews from the bmc
// DEBUGLOGIN=1 go run main.go -u bar -p foo -ip <>
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/bmc-toolbox/bmclib/devices"
"github.com/bmc-toolbox/bmclogin"
)
var (
user string
pass string
ip string
)
func main() {
flag.StringVar(&user, "u", "", "Username")
flag.StringVar(&pass, "p", "", "Password")
flag.StringVar(&ip, "ip", "", "IPAddress")
flag.Parse()
if user == "" {
fmt.Println("-u required (username)")
os.Exit(1)
}
if pass == "" {
fmt.Println("-p required (password)")
os.Exit(1)
}
if ip == "" {
fmt.Println("-ip required (IPAddress)")
os.Exit(1)
}
fmt.Printf("Connecting to %s with user: %s\n", ip, user)
credentials := []map[string]string{
map[string]string{user: pass},
}
c := bmclogin.Params{
IpAddresses: []string{ip},
Credentials: credentials,
CheckCredential: true,
Retries: 2,
}
connection, _, err := c.Login()
if err == nil {
switch (connection).(type) {
case devices.Bmc:
bmc := connection.(devices.Bmc)
state, err := bmc.PowerState()
if err != nil {
fmt.Printf("Error: Unable to determine asset power state, %+v", err)
os.Exit(1)
}
if strings.Contains(state, "off") {
fmt.Printf("Asset is currently powered off.")
os.Exit(1)
} else {
fmt.Printf("Server power status is %s\n", state)
}
resp, extension, err := bmc.Screenshot()
if err != nil {
fmt.Printf("Error: %+v", err)
os.Exit(1)
}
if resp == nil {
fmt.Println("Error: empty response recieved")
os.Exit(1)
}
oFname := "/tmp/"
t := time.Now()
oFname += fmt.Sprintf("%s_%d", bmc.BmcType(), t.Unix())
oFname += fmt.Sprintf(".%s", extension)
err = ioutil.WriteFile(oFname, resp, 0644)
if err != nil {
fmt.Printf("Error: unable to to write to file %+v", err)
os.Exit(1)
}
fmt.Printf("Wrote file: %s", oFname)
bmc.Close()
case devices.BmcChassis:
connection.(devices.BmcChassis).Close()
}
//fmt.Println("Successful login")
} else {
fmt.Println("login failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment