blob: c1b09ce748270c6a3a634a71f6ec55b065071aa1 [file] [log] [blame]
Daniel Willmann203d8652011-04-29 18:40:29 +02001#!/usr/bin/python
Max82caa3e2016-11-17 17:24:53 +01002# -*- mode: python-mode; py-indent-tabs-mode: nil -*-
Philipp Maier2459f9f2017-02-24 00:07:26 +01003"""
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 Willmann203d8652011-04-29 18:40:29 +020024
Daniel Willmann203d8652011-04-29 18:40:29 +020025from optparse import OptionParser
Maxbd33f542016-11-17 17:29:45 +010026from ipa import Ctrl
Daniel Willmann203d8652011-04-29 18:40:29 +020027import socket
Daniel Willmann203d8652011-04-29 18:40:29 +020028
29verbose = False
30
Daniel Willmann203d8652011-04-29 18:40:29 +020031def connect(host, port):
Max82caa3e2016-11-17 17:24:53 +010032 if verbose:
33 print "Connecting to host %s:%i" % (host, port)
Daniel Willmann203d8652011-04-29 18:40:29 +020034
Max82caa3e2016-11-17 17:24:53 +010035 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36 sck.setblocking(1)
37 sck.connect((host, port))
38 return sck
Daniel Willmann203d8652011-04-29 18:40:29 +020039
Maxdbb63922016-08-01 18:54:17 +020040def do_set_get(sck, var, value = None):
Maxbd33f542016-11-17 17:29:45 +010041 (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)
Maxdbb63922016-08-01 18:54:17 +020045
46def set_var(sck, var, val):
Maxbd33f542016-11-17 17:29:45 +010047 (a, _, _) = do_set_get(sck, var, val)
48 return a
Maxdbb63922016-08-01 18:54:17 +020049
50def get_var(sck, var):
Maxbd33f542016-11-17 17:29:45 +010051 (_, _, v) = do_set_get(sck, var)
52 return v
53
Max47e17812017-01-11 15:10:16 +010054def _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
Maxbd33f542016-11-17 17:29:45 +010062 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
Maxdbb63922016-08-01 18:54:17 +020071
Max3ed214c2016-07-29 18:19:25 +020072if __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")
Max3ed214c2016-07-29 18:19:25 +020082 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 Willmann203d8652011-04-29 18:40:29 +020086
Max3ed214c2016-07-29 18:19:25 +020087 (options, args) = parser.parse_args()
Daniel Willmann203d8652011-04-29 18:40:29 +020088
Max3ed214c2016-07-29 18:19:25 +020089 verbose = options.verbose
Daniel Willmann203d8652011-04-29 18:40:29 +020090
Max3ed214c2016-07-29 18:19:25 +020091 if options.cmd_set and options.cmd_get:
Max82caa3e2016-11-17 17:24:53 +010092 parser.error("Get and set options are mutually exclusive!")
Daniel Willmann203d8652011-04-29 18:40:29 +020093
Max3ed214c2016-07-29 18:19:25 +020094 if not (options.cmd_get or options.cmd_set or options.monitor):
Max82caa3e2016-11-17 17:24:53 +010095 parser.error("One of -m, -g, or -s must be set")
Daniel Willmann203d8652011-04-29 18:40:29 +020096
Max3ed214c2016-07-29 18:19:25 +020097 if not (options.host):
Max82caa3e2016-11-17 17:24:53 +010098 parser.error("Destination host and port required!")
Daniel Willmann203d8652011-04-29 18:40:29 +020099
Max3ed214c2016-07-29 18:19:25 +0200100 sock = connect(options.host, options.port)
Daniel Willmann203d8652011-04-29 18:40:29 +0200101
Max3ed214c2016-07-29 18:19:25 +0200102 if options.cmd_set:
Max82caa3e2016-11-17 17:24:53 +0100103 if len(args) < 2:
104 parser.error("Set requires var and value arguments")
Max47e17812017-01-11 15:10:16 +0100105 _leftovers(sock, socket.MSG_DONTWAIT)
Maxbd33f542016-11-17 17:29:45 +0100106 print "Got message:", set_var(sock, args[0], ' '.join(args[1:]))
Daniel Willmann203d8652011-04-29 18:40:29 +0200107
Max3ed214c2016-07-29 18:19:25 +0200108 if options.cmd_get:
Max82caa3e2016-11-17 17:24:53 +0100109 if len(args) != 1:
110 parser.error("Get requires the var argument")
Max47e17812017-01-11 15:10:16 +0100111 _leftovers(sock, socket.MSG_DONTWAIT)
Maxbd33f542016-11-17 17:29:45 +0100112 (a, _, _) = do_set_get(sock, args[0])
113 print "Got message:", a
Daniel Willmann203d8652011-04-29 18:40:29 +0200114
Max3ed214c2016-07-29 18:19:25 +0200115 if options.monitor:
Max82caa3e2016-11-17 17:24:53 +0100116 while True:
Max47e17812017-01-11 15:10:16 +0100117 if not _leftovers(sock, 0):
Max82caa3e2016-11-17 17:24:53 +0100118 print "Connection is gone."
119 break
Max3ed214c2016-07-29 18:19:25 +0200120 sock.close()