Max | 8a02e36 | 2017-12-21 14:37:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 2 | # -*- mode: python-mode; py-indent-tabs-mode: nil -*- |
| 3 | """ |
| 4 | /* |
Max | 8a02e36 | 2017-12-21 14:37:23 +0100 | [diff] [blame] | 5 | * Copyright (C) 2016-2017 sysmocom s.f.m.c. GmbH |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 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 | """ |
Max | 8a02e36 | 2017-12-21 14:37:23 +0100 | [diff] [blame] | 24 | |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 25 | from optparse import OptionParser |
| 26 | from osmopy.osmo_ipa import Ctrl |
| 27 | import socket |
| 28 | |
| 29 | verbose = False |
| 30 | |
| 31 | def connect(host, port): |
| 32 | if verbose: |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 33 | print("Connecting to host %s:%i" % (host, port)) |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 34 | |
| 35 | sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 36 | sck.setblocking(1) |
| 37 | sck.connect((host, port)) |
| 38 | return sck |
| 39 | |
| 40 | def do_set_get(sck, var, value = None): |
| 41 | (r, c) = Ctrl().cmd(var, value) |
| 42 | sck.send(c) |
Max | 566f2a7 | 2017-12-21 14:38:39 +0100 | [diff] [blame^] | 43 | ret = sck.recv(4096) |
| 44 | return (Ctrl().rem_header(ret),) + Ctrl().verify(ret, r, var, value) |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 45 | |
| 46 | def set_var(sck, var, val): |
| 47 | (a, _, _) = do_set_get(sck, var, val) |
| 48 | return a |
| 49 | |
| 50 | def get_var(sck, var): |
| 51 | (_, _, v) = do_set_get(sck, var) |
| 52 | return v |
| 53 | |
| 54 | def _leftovers(sck, fl): |
| 55 | """ |
| 56 | Read outstanding data if any according to flags |
| 57 | """ |
| 58 | try: |
| 59 | data = sck.recv(1024, fl) |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 60 | except socket.error as _: |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 61 | return False |
| 62 | if len(data) != 0: |
| 63 | tail = data |
| 64 | while True: |
| 65 | (head, tail) = Ctrl().split_combined(tail) |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 66 | print("Got message:", Ctrl().rem_header(head)) |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 67 | if len(tail) == 0: |
| 68 | break |
| 69 | return True |
| 70 | return False |
| 71 | |
| 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") |
| 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) |
| 86 | |
| 87 | (options, args) = parser.parse_args() |
| 88 | |
| 89 | verbose = options.verbose |
| 90 | |
| 91 | if options.cmd_set and options.cmd_get: |
| 92 | parser.error("Get and set options are mutually exclusive!") |
| 93 | |
| 94 | if not (options.cmd_get or options.cmd_set or options.monitor): |
| 95 | parser.error("One of -m, -g, or -s must be set") |
| 96 | |
| 97 | if not (options.host): |
| 98 | parser.error("Destination host and port required!") |
| 99 | |
| 100 | sock = connect(options.host, options.port) |
| 101 | |
| 102 | if options.cmd_set: |
| 103 | if len(args) < 2: |
| 104 | parser.error("Set requires var and value arguments") |
| 105 | _leftovers(sock, socket.MSG_DONTWAIT) |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 106 | print("Got message:", set_var(sock, args[0], ' '.join(args[1:]))) |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 107 | |
| 108 | if options.cmd_get: |
| 109 | if len(args) != 1: |
| 110 | parser.error("Get requires the var argument") |
| 111 | _leftovers(sock, socket.MSG_DONTWAIT) |
| 112 | (a, _, _) = do_set_get(sock, args[0]) |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 113 | print("Got message:", a) |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 114 | |
| 115 | if options.monitor: |
| 116 | while True: |
| 117 | if not _leftovers(sock, 0): |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 118 | print("Connection is gone.") |
Max | e732c2c | 2017-11-23 16:42:59 +0100 | [diff] [blame] | 119 | break |
| 120 | sock.close() |