Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 2 | # -*- mode: python-mode; py-indent-tabs-mode: nil -*- |
Philipp Maier | 2459f9f | 2017-02-24 00:07:26 +0100 | [diff] [blame] | 3 | """ |
| 4 | /* |
| 5 | * Copyright (C) 2016 sysmocom s.f.m.c. GmbH |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | """ |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 24 | |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 25 | from optparse import OptionParser |
Max | bd33f54 | 2016-11-17 17:29:45 +0100 | [diff] [blame] | 26 | from ipa import Ctrl |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 27 | import socket |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 28 | |
| 29 | verbose = False |
| 30 | |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 31 | def connect(host, port): |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 32 | if verbose: |
| 33 | print "Connecting to host %s:%i" % (host, port) |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 34 | |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 35 | sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 36 | sck.setblocking(1) |
| 37 | sck.connect((host, port)) |
| 38 | return sck |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 39 | |
Max | dbb6392 | 2016-08-01 18:54:17 +0200 | [diff] [blame] | 40 | def do_set_get(sck, var, value = None): |
Max | bd33f54 | 2016-11-17 17:29:45 +0100 | [diff] [blame] | 41 | (r, c) = Ctrl().cmd(var, value) |
| 42 | sck.send(c) |
| 43 | answer = Ctrl().rem_header(sck.recv(4096)) |
| 44 | return (answer,) + Ctrl().verify(answer, r, var, value) |
Max | dbb6392 | 2016-08-01 18:54:17 +0200 | [diff] [blame] | 45 | |
| 46 | def set_var(sck, var, val): |
Max | bd33f54 | 2016-11-17 17:29:45 +0100 | [diff] [blame] | 47 | (a, _, _) = do_set_get(sck, var, val) |
| 48 | return a |
Max | dbb6392 | 2016-08-01 18:54:17 +0200 | [diff] [blame] | 49 | |
| 50 | def get_var(sck, var): |
Max | bd33f54 | 2016-11-17 17:29:45 +0100 | [diff] [blame] | 51 | (_, _, v) = do_set_get(sck, var) |
| 52 | return v |
| 53 | |
Max | 47e1781 | 2017-01-11 15:10:16 +0100 | [diff] [blame] | 54 | def _leftovers(sck, fl): |
| 55 | """ |
| 56 | Read outstanding data if any according to flags |
| 57 | """ |
| 58 | try: |
| 59 | data = sck.recv(1024, fl) |
| 60 | except socket.error as (s_errno, strerror): |
| 61 | return False |
Max | bd33f54 | 2016-11-17 17:29:45 +0100 | [diff] [blame] | 62 | if len(data) != 0: |
| 63 | tail = data |
| 64 | while True: |
| 65 | (head, tail) = Ctrl().split_combined(tail) |
| 66 | print "Got message:", Ctrl().rem_header(head) |
| 67 | if len(tail) == 0: |
| 68 | break |
| 69 | return True |
| 70 | return False |
Max | dbb6392 | 2016-08-01 18:54:17 +0200 | [diff] [blame] | 71 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 72 | if __name__ == '__main__': |
| 73 | parser = OptionParser("Usage: %prog [options] var [value]") |
| 74 | parser.add_option("-d", "--host", dest="host", |
| 75 | help="connect to HOST", metavar="HOST") |
| 76 | parser.add_option("-p", "--port", dest="port", type="int", |
| 77 | help="use PORT", metavar="PORT", default=4249) |
| 78 | parser.add_option("-g", "--get", action="store_true", |
| 79 | dest="cmd_get", help="perform GET operation") |
| 80 | parser.add_option("-s", "--set", action="store_true", |
| 81 | dest="cmd_set", help="perform SET operation") |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 82 | parser.add_option("-v", "--verbose", action="store_true", |
| 83 | dest="verbose", help="be verbose", default=False) |
| 84 | parser.add_option("-m", "--monitor", action="store_true", |
| 85 | dest="monitor", help="monitor the connection for traps", default=False) |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 86 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 87 | (options, args) = parser.parse_args() |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 88 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 89 | verbose = options.verbose |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 90 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 91 | if options.cmd_set and options.cmd_get: |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 92 | parser.error("Get and set options are mutually exclusive!") |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 93 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 94 | if not (options.cmd_get or options.cmd_set or options.monitor): |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 95 | parser.error("One of -m, -g, or -s must be set") |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 96 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 97 | if not (options.host): |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 98 | parser.error("Destination host and port required!") |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 99 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 100 | sock = connect(options.host, options.port) |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 101 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 102 | if options.cmd_set: |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 103 | if len(args) < 2: |
| 104 | parser.error("Set requires var and value arguments") |
Max | 47e1781 | 2017-01-11 15:10:16 +0100 | [diff] [blame] | 105 | _leftovers(sock, socket.MSG_DONTWAIT) |
Max | bd33f54 | 2016-11-17 17:29:45 +0100 | [diff] [blame] | 106 | print "Got message:", set_var(sock, args[0], ' '.join(args[1:])) |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 107 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 108 | if options.cmd_get: |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 109 | if len(args) != 1: |
| 110 | parser.error("Get requires the var argument") |
Max | 47e1781 | 2017-01-11 15:10:16 +0100 | [diff] [blame] | 111 | _leftovers(sock, socket.MSG_DONTWAIT) |
Max | bd33f54 | 2016-11-17 17:29:45 +0100 | [diff] [blame] | 112 | (a, _, _) = do_set_get(sock, args[0]) |
| 113 | print "Got message:", a |
Daniel Willmann | 203d865 | 2011-04-29 18:40:29 +0200 | [diff] [blame] | 114 | |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 115 | if options.monitor: |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 116 | while True: |
Max | 47e1781 | 2017-01-11 15:10:16 +0100 | [diff] [blame] | 117 | if not _leftovers(sock, 0): |
Max | 82caa3e | 2016-11-17 17:24:53 +0100 | [diff] [blame] | 118 | print "Connection is gone." |
| 119 | break |
Max | 3ed214c | 2016-07-29 18:19:25 +0200 | [diff] [blame] | 120 | sock.close() |