File: pyghmiutil.py

package info (click to toggle)
python-pyghmi 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,376 kB
  • sloc: python: 21,736; sh: 35; makefile: 18
file content (92 lines) | stat: -rwxr-xr-x 2,917 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2013 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This is an example of using the library in a synchronous fashion. For now,
it isn't conceived as a general utility to actually use, just help developers
understand how the ipmi_command class workes.
"""

import functools
import os
import sys

from pyghmi.ipmi import command


def docommand(args, result, ipmisession):
    command = args[0]
    args = args[1:]
    print("Logged into %s" % ipmisession.bmc)
    if 'error' in result:
        print(result['error'])
        return
    if command == 'power':
        if args:
            print(ipmisession.set_power(args[0], wait=True))
        else:
            value = ipmisession.get_power()
            print("%s: %s" % (ipmisession.bmc, value['powerstate']))
    elif command == 'bootdev':
        if args:
            print(ipmisession.set_bootdev(args[0]))
        else:
            print(ipmisession.get_bootdev())
    elif command == 'sensors':
        for reading in ipmisession.get_sensor_data():
            print(reading)
    elif command == 'health':
        print(ipmisession.get_health())
    elif command == 'inventory':
        for item in ipmisession.get_inventory():
            print(item)
    elif command == 'leds':
        for led in ipmisession.get_leds():
            print(led)
    elif command == 'graphical':
        print(ipmisession.get_graphical_console())
    elif command == 'net':
        print(ipmisession.get_net_configuration())
    elif command == 'raw':
        print(ipmisession.raw_command(
              netfn=int(args[0]),
              command=int(args[1]),
              data=map(lambda x: int(x, 16), args[2:])))


def main():
    if (len(sys.argv) < 3) or 'IPMIPASSWORD' not in os.environ:
        print("Usage:")
        print(" IPMIPASSWORD=password %s bmc username <cmd> <optarg>" %
              sys.argv[0])
        return 1

    password = os.environ['IPMIPASSWORD']
    os.environ['IPMIPASSWORD'] = ""
    bmc = sys.argv[1]
    userid = sys.argv[2]

    bmcs = bmc.split(',')
    ipmicmd = None
    for bmc in bmcs:
        # NOTE(etingof): is it right to have `ipmicmd` overridden?
        ipmicmd = command.Command(
            bmc=bmc, userid=userid, password=password,
            onlogon=functools.partial(docommand, sys.argv[3:]))

    if ipmicmd:
        ipmicmd.eventloop()


if __name__ == '__main__':
    sys.exit(main())