1#!/usr/bin/env python3

2

3 import sys

4 import shlex

5 from subprocess import check_output, CalledProcessError

6

7

8 def main():

9 # First, we need to get output

10 cmd = "ipmitool mc info"

11 try:

12 result = check_output(shlex.split(cmd), universal_newlines=True)

13 except FileNotFoundError:

14 print(

15 "ipmitool was not found! Please install it and try again.",

16 file=sys.stderr,

17 )

18 return 1

19 except CalledProcessError as e:

20 print("Problem running %s. Error was %s" % (cmd, e), file=sys.stderr)

21 return 1

22 result = result.split("\n")

23

24 # We need some bits that are formatted oddly so we need to do some parsing

25 data = {}

26 for line in result:

27 if ":" in line:

28 key = line.split(":")[0].strip()

29 value = line.split(":")[1].strip()

30 data[key] = value

31 last = (key, [value])

32 else:

33 # since the last line we matched had a ':', it's key is likely the

34 # key for the next few lines that don't have a ':'

35 # This should keep adding items to our last key's list until we hit

36 # another line with a :, and we start the cycle over again.

37 last[1].append(line.strip())

38 data[last[0]] = last[1]

39

40 # Now print out what we care about:

41 we_care_about = [

42 "Manufacturer Name",

43 "Manufacturer ID",

44 "Product Name",

45 "Product ID",

46 "Firmware Revision",

47 "IPMI Version",

48 "Additional Device Support",

49 ]

50

51 for field in we_care_about:

52 if type(data[field]) is list:

53 # Sometimes the first item in the list is ''. This will remove it

54 data[field].remove("")

55 print(field.ljust(30), ":", data[field].pop(0))

56 for item in data[field]:

57 print(" ".ljust(32), item)

58 else:

59 print(field.ljust(30), ":", data[field])

60

61 return 0

62

63

64 if __name__ == "__main__":

65 sys.exit(main())