matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 3 | # Copyright 2013 Linaro Limited |
| 4 | # Author Matt Hart <matthew.hart@linaro.org> |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 21 | import socket |
| 22 | import optparse |
| 23 | |
| 24 | if __name__ == '__main__': |
Neil Williams | 424fbbb | 2014-05-16 20:34:00 +0100 | [diff] [blame] | 25 | usage = "Usage: %prog --daemon deamonhostname --hostname pduhostname --port pduportnum --command pducommand" |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 26 | description = "LAVA PDU daemon client" |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 27 | commands = ["reboot", "on", "off"] |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 28 | parser = optparse.OptionParser(usage=usage, description=description) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 29 | parser.add_option("--daemon", dest="pdudaemonhostname", action="store", type="string", help="LAVAPDU Daemon hostname (ex: localhost)") |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 30 | parser.add_option("--hostname", dest="pduhostname", action="store", type="string", help="PDU Hostname (ex: pdu05)") |
| 31 | parser.add_option("--port", dest="pduportnum", action="store", type="string", help="PDU Portnumber (ex: 04)") |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 32 | parser.add_option("--command", dest="pducommand", action="store", type="string", help="PDU command (ex: reboot|on|off)") |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 33 | parser.add_option("--delay", dest="pdudelay", action="store", type="int", help="Delay before command runs, or between off/on when rebooting (ex: 5)") |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 34 | (options, args) = parser.parse_args() |
| 35 | if (not (options.pdudaemonhostname) or not(options.pduhostname) or not (options.pduportnum) or not (options.pducommand)): |
| 36 | print("Missing option, try -h for help") |
| 37 | exit(1) |
| 38 | if not (options.pducommand in commands): |
| 39 | print("Unknown pdu command: %s" % options.pducommand) |
| 40 | exit(1) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 41 | if options.pdudelay: |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 42 | string = ("%s %s %s %s" % (options.pduhostname, options.pduportnum, options.pducommand, options.pdudelay)) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 43 | else: |
| 44 | string = ("%s %s %s" % (options.pduhostname, options.pduportnum, options.pducommand)) |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 45 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 46 | #sock.setblocking(0) # optional non-blocking |
Neil Williams | 6877b1f | 2014-02-13 11:16:51 +0000 | [diff] [blame] | 47 | reply = "" |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 48 | try: |
| 49 | sock.connect((options.pdudaemonhostname, 16421)) |
| 50 | sock.send(string) |
| 51 | reply = sock.recv(16384).strip() # limit reply to 16K |
| 52 | sock.close() |
| 53 | except Exception: |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 54 | print ("Error sending command, wrong daemon hostname?") |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 55 | exit(1) |
| 56 | if reply == "ack": |
| 57 | print("Command sent successfully.") |
| 58 | exit(0) |
| 59 | else: |
Neil Williams | 6877b1f | 2014-02-13 11:16:51 +0000 | [diff] [blame] | 60 | print("Unknown error sending command! %s replied: %s" % (options.pdudaemonhostname, reply)) |
| 61 | exit(127) |