blob: fdcdcb33c5c68b9bc7044ffb613fd52befb18371 [file] [log] [blame]
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +01001#!/usr/bin/python
2
3import socket
4import optparse
5
6if __name__ == '__main__':
7 usage = "Usage: %prog [-d] deamonhostname [-h] pduhostname -[p] pduportnum [-c] pducommand"
8 description = "LAVA PDU daemon client"
Neil Williams16579dc2014-02-13 11:20:04 +00009 commands = ["reboot", "on", "off", "delayed"]
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010010 parser = optparse.OptionParser(usage=usage, description=description)
Neil Williams16579dc2014-02-13 11:20:04 +000011 parser.add_option("--daemon", dest="pdudaemonhostname", action="store", type="string", help="LAVAPDU Daemon (ex: control)")
12 parser.add_option("--hostname", dest="pduhostname", action="store", type="string", help="PDU Hostname (ex: pdu05)")
13 parser.add_option("--port", dest="pduportnum", action="store", type="string", help="PDU Portnumber (ex: 04)")
14 parser.add_option("--command", dest="pducommand", action="store", type="string", help="PDU command (ex: reboot|on|off|delayed)")
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010015 (options, args) = parser.parse_args()
16 if (not (options.pdudaemonhostname) or not(options.pduhostname) or not (options.pduportnum) or not (options.pducommand)):
17 print("Missing option, try -h for help")
18 exit(1)
19 if not (options.pducommand in commands):
20 print("Unknown pdu command: %s" % options.pducommand)
21 exit(1)
22 #print(options)
23 string = ("%s %s %s" % (options.pduhostname, options.pduportnum, options.pducommand))
24 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25 #sock.setblocking(0) # optional non-blocking
Neil Williams6877b1f2014-02-13 11:16:51 +000026 reply = ""
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010027 try:
28 sock.connect((options.pdudaemonhostname, 16421))
29 sock.send(string)
30 reply = sock.recv(16384).strip() # limit reply to 16K
31 sock.close()
32 except Exception:
33 print ("Error sending command, wrong hostname?")
34 exit(1)
35 if reply == "ack":
36 print("Command sent successfully.")
37 exit(0)
38 else:
Neil Williams6877b1f2014-02-13 11:16:51 +000039 print("Unknown error sending command! %s replied: %s" % (options.pdudaemonhostname, reply))
40 exit(127)