File: pduclient

package info (click to toggle)
lavapdu 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 248 kB
  • ctags: 145
  • sloc: python: 687; xml: 458; sh: 190; makefile: 29
file content (61 lines) | stat: -rwxr-xr-x 3,013 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python

#  Copyright 2013 Linaro Limited
#  Author Matt Hart <matthew.hart@linaro.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

import socket
import optparse

if __name__ == '__main__':
    usage = "Usage: %prog --daemon deamonhostname --hostname pduhostname --port pduportnum --command pducommand"
    description = "LAVA PDU daemon client"
    commands = ["reboot", "on", "off"]
    parser = optparse.OptionParser(usage=usage, description=description)
    parser.add_option("--daemon", dest="pdudaemonhostname", action="store", type="string", help="LAVAPDU Daemon hostname (ex: localhost)")
    parser.add_option("--hostname", dest="pduhostname", action="store", type="string", help="PDU Hostname (ex: pdu05)")
    parser.add_option("--port", dest="pduportnum", action="store", type="string", help="PDU Portnumber (ex: 04)")
    parser.add_option("--command", dest="pducommand", action="store", type="string", help="PDU command (ex: reboot|on|off)")
    parser.add_option("--delay", dest="pdudelay", action="store", type="int", help="Delay before command runs, or between off/on when rebooting (ex: 5)")
    (options, args) = parser.parse_args()
    if (not (options.pdudaemonhostname) or not(options.pduhostname) or not (options.pduportnum) or not (options.pducommand)):
        print("Missing option, try -h for help")
        exit(1)
    if not (options.pducommand in commands):
        print("Unknown pdu command: %s" % options.pducommand)
        exit(1)
    if options.pdudelay:
        string = ("%s %s %s %s" % (options.pduhostname, options.pduportnum, options.pducommand, options.pdudelay))
    else:
        string = ("%s %s %s" % (options.pduhostname, options.pduportnum, options.pducommand))
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #sock.setblocking(0)  # optional non-blocking
    reply = ""
    try:
        sock.connect((options.pdudaemonhostname, 16421))
        sock.send(string)
        reply = sock.recv(16384).strip()  # limit reply to 16K
        sock.close()
    except Exception:
        print ("Error sending command, wrong daemon hostname?")
        exit(1)
    if reply == "ack":
        print("Command sent successfully.")
        exit(0)
    else:
        print("Unknown error sending command! %s replied: %s" % (options.pdudaemonhostname, reply))
        exit(127)