matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import socket |
| 4 | import optparse |
| 5 | |
| 6 | if __name__ == '__main__': |
| 7 | usage = "Usage: %prog [-d] deamonhostname [-h] pduhostname -[p] pduportnum [-c] pducommand" |
| 8 | description = "LAVA PDU daemon client" |
| 9 | commands = ["reboot","on","off","delayed"] |
| 10 | parser = optparse.OptionParser(usage=usage, description=description) |
| 11 | 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)") |
| 15 | (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 Williams | 6877b1f | 2014-02-13 11:16:51 +0000 | [diff] [blame] | 26 | reply = "" |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 27 | 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 Williams | 6877b1f | 2014-02-13 11:16:51 +0000 | [diff] [blame] | 39 | print("Unknown error sending command! %s replied: %s" % (options.pdudaemonhostname, reply)) |
| 40 | exit(127) |
| 41 | |