Guest User

Untitled

a guest
Oct 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. // Grabs screen previews from the bmc
  2. // DEBUGLOGIN=1 go run main.go -u bar -p foo -ip <>
  3.  
  4. package main
  5.  
  6. import (
  7. "flag"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "strings"
  12. "time"
  13.  
  14. "github.com/bmc-toolbox/bmclib/devices"
  15. "github.com/bmc-toolbox/bmclogin"
  16. )
  17.  
  18. var (
  19. user string
  20. pass string
  21. ip string
  22. )
  23.  
  24. func main() {
  25.  
  26. flag.StringVar(&user, "u", "", "Username")
  27. flag.StringVar(&pass, "p", "", "Password")
  28. flag.StringVar(&ip, "ip", "", "IPAddress")
  29. flag.Parse()
  30.  
  31. if user == "" {
  32. fmt.Println("-u required (username)")
  33. os.Exit(1)
  34. }
  35.  
  36. if pass == "" {
  37. fmt.Println("-p required (password)")
  38. os.Exit(1)
  39. }
  40.  
  41. if ip == "" {
  42. fmt.Println("-ip required (IPAddress)")
  43. os.Exit(1)
  44. }
  45.  
  46. fmt.Printf("Connecting to %s with user: %s\n", ip, user)
  47.  
  48. credentials := []map[string]string{
  49. map[string]string{user: pass},
  50. }
  51.  
  52. c := bmclogin.Params{
  53. IpAddresses: []string{ip},
  54. Credentials: credentials,
  55. CheckCredential: true,
  56. Retries: 2,
  57. }
  58.  
  59. connection, _, err := c.Login()
  60. if err == nil {
  61.  
  62. switch (connection).(type) {
  63. case devices.Bmc:
  64.  
  65. bmc := connection.(devices.Bmc)
  66. state, err := bmc.PowerState()
  67. if err != nil {
  68. fmt.Printf("Error: Unable to determine asset power state, %+v", err)
  69. os.Exit(1)
  70. }
  71.  
  72. if strings.Contains(state, "off") {
  73. fmt.Printf("Asset is currently powered off.")
  74. os.Exit(1)
  75. } else {
  76. fmt.Printf("Server power status is %s\n", state)
  77. }
  78.  
  79. resp, extension, err := bmc.Screenshot()
  80. if err != nil {
  81. fmt.Printf("Error: %+v", err)
  82. os.Exit(1)
  83. }
  84.  
  85. if resp == nil {
  86. fmt.Println("Error: empty response recieved")
  87. os.Exit(1)
  88. }
  89.  
  90. oFname := "/tmp/"
  91.  
  92. t := time.Now()
  93. oFname += fmt.Sprintf("%s_%d", bmc.BmcType(), t.Unix())
  94. oFname += fmt.Sprintf(".%s", extension)
  95.  
  96. err = ioutil.WriteFile(oFname, resp, 0644)
  97. if err != nil {
  98. fmt.Printf("Error: unable to to write to file %+v", err)
  99. os.Exit(1)
  100. }
  101.  
  102. fmt.Printf("Wrote file: %s", oFname)
  103.  
  104. bmc.Close()
  105. case devices.BmcChassis:
  106. connection.(devices.BmcChassis).Close()
  107. }
  108.  
  109. //fmt.Println("Successful login")
  110. } else {
  111. fmt.Println("login failed")
  112. }
  113. }
Add Comment
Please, Sign In to add comment