blob: 8a67fd8e9ab9ca6c6860e7b74be19731e2276fbd [file] [log] [blame]
Max8a02e362017-12-21 14:37:23 +01001#!/usr/bin/env python3
Maxe732c2c2017-11-23 16:42:59 +01002# -*- mode: python-mode; py-indent-tabs-mode: nil -*-
3"""
4/*
Max8a02e362017-12-21 14:37:23 +01005 * Copyright (C) 2016-2017 sysmocom s.f.m.c. GmbH
Maxe732c2c2017-11-23 16:42:59 +01006 *
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"""
Max8a02e362017-12-21 14:37:23 +010024
Maxe732c2c2017-11-23 16:42:59 +010025from optparse import OptionParser
26from osmopy.osmo_ipa import Ctrl
27import socket
28
29verbose = False
30
31def connect(host, port):
32 if verbose:
Max6ccd0782017-12-15 12:15:39 +010033 print("Connecting to host %s:%i" % (host, port))
Maxe732c2c2017-11-23 16:42:59 +010034
35 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36 sck.setblocking(1)
37 sck.connect((host, port))
38 return sck
39
40def do_set_get(sck, var, value = None):
Max217837c2018-11-26 12:18:03 +010041 _leftovers(sck, socket.MSG_DONTWAIT)
Maxe732c2c2017-11-23 16:42:59 +010042 (r, c) = Ctrl().cmd(var, value)
43 sck.send(c)
Max2a1d8932018-11-23 23:02:27 +010044 while True:
45 ret = sck.recv(4096)
46 # handle multiple messages, ignore TRAPs
47 ret = Ctrl().skip_traps(ret)
48 if ret != None:
49 (i, k, v) = Ctrl().parse(ret)
50 break;
Max566f2a72017-12-21 14:38:39 +010051 return (Ctrl().rem_header(ret),) + Ctrl().verify(ret, r, var, value)
Maxe732c2c2017-11-23 16:42:59 +010052
53def set_var(sck, var, val):
54 (a, _, _) = do_set_get(sck, var, val)
55 return a
56
Maxe732c2c2017-11-23 16:42:59 +010057def _leftovers(sck, fl):
58 """
59 Read outstanding data if any according to flags
60 """
61 try:
62 data = sck.recv(1024, fl)
Max6ccd0782017-12-15 12:15:39 +010063 except socket.error as _:
Maxe732c2c2017-11-23 16:42:59 +010064 return False
65 if len(data) != 0:
66 tail = data
67 while True:
68 (head, tail) = Ctrl().split_combined(tail)
Max6ccd0782017-12-15 12:15:39 +010069 print("Got message:", Ctrl().rem_header(head))
Maxe732c2c2017-11-23 16:42:59 +010070 if len(tail) == 0:
71 break
72 return True
73 return False
74
75if __name__ == '__main__':
76 parser = OptionParser("Usage: %prog [options] var [value]")
77 parser.add_option("-d", "--host", dest="host",
78 help="connect to HOST", metavar="HOST")
79 parser.add_option("-p", "--port", dest="port", type="int",
80 help="use PORT", metavar="PORT", default=4249)
81 parser.add_option("-g", "--get", action="store_true",
82 dest="cmd_get", help="perform GET operation")
83 parser.add_option("-s", "--set", action="store_true",
84 dest="cmd_set", help="perform SET operation")
85 parser.add_option("-v", "--verbose", action="store_true",
86 dest="verbose", help="be verbose", default=False)
87 parser.add_option("-m", "--monitor", action="store_true",
88 dest="monitor", help="monitor the connection for traps", default=False)
89
90 (options, args) = parser.parse_args()
91
92 verbose = options.verbose
93
94 if options.cmd_set and options.cmd_get:
95 parser.error("Get and set options are mutually exclusive!")
96
97 if not (options.cmd_get or options.cmd_set or options.monitor):
98 parser.error("One of -m, -g, or -s must be set")
99
100 if not (options.host):
101 parser.error("Destination host and port required!")
102
103 sock = connect(options.host, options.port)
104
105 if options.cmd_set:
106 if len(args) < 2:
107 parser.error("Set requires var and value arguments")
Max6ccd0782017-12-15 12:15:39 +0100108 print("Got message:", set_var(sock, args[0], ' '.join(args[1:])))
Maxe732c2c2017-11-23 16:42:59 +0100109
110 if options.cmd_get:
111 if len(args) != 1:
112 parser.error("Get requires the var argument")
Maxe732c2c2017-11-23 16:42:59 +0100113 (a, _, _) = do_set_get(sock, args[0])
Max6ccd0782017-12-15 12:15:39 +0100114 print("Got message:", a)
Maxe732c2c2017-11-23 16:42:59 +0100115
116 if options.monitor:
117 while True:
118 if not _leftovers(sock, 0):
Max6ccd0782017-12-15 12:15:39 +0100119 print("Connection is gone.")
Maxe732c2c2017-11-23 16:42:59 +0100120 break
121 sock.close()