2017-12-21 13:37:23 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-11-23 15:42:59 +00:00
|
|
|
# -*- mode: python-mode; py-indent-tabs-mode: nil -*-
|
|
|
|
"""
|
|
|
|
/*
|
2017-12-21 13:37:23 +00:00
|
|
|
* Copyright (C) 2016-2017 sysmocom s.f.m.c. GmbH
|
2017-11-23 15:42:59 +00:00
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
"""
|
2017-12-21 13:37:23 +00:00
|
|
|
|
2017-11-23 15:42:59 +00:00
|
|
|
from optparse import OptionParser
|
|
|
|
from osmopy.osmo_ipa import Ctrl
|
|
|
|
import socket
|
|
|
|
|
|
|
|
verbose = False
|
|
|
|
|
|
|
|
def connect(host, port):
|
|
|
|
if verbose:
|
2017-12-15 11:15:39 +00:00
|
|
|
print("Connecting to host %s:%i" % (host, port))
|
2017-11-23 15:42:59 +00:00
|
|
|
|
|
|
|
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sck.setblocking(1)
|
|
|
|
sck.connect((host, port))
|
|
|
|
return sck
|
|
|
|
|
|
|
|
def do_set_get(sck, var, value = None):
|
2018-11-26 11:18:03 +00:00
|
|
|
_leftovers(sck, socket.MSG_DONTWAIT)
|
2017-11-23 15:42:59 +00:00
|
|
|
(r, c) = Ctrl().cmd(var, value)
|
|
|
|
sck.send(c)
|
2018-11-23 22:02:27 +00:00
|
|
|
while True:
|
|
|
|
ret = sck.recv(4096)
|
|
|
|
# handle multiple messages, ignore TRAPs
|
|
|
|
ret = Ctrl().skip_traps(ret)
|
|
|
|
if ret != None:
|
|
|
|
(i, k, v) = Ctrl().parse(ret)
|
|
|
|
break;
|
2017-12-21 13:38:39 +00:00
|
|
|
return (Ctrl().rem_header(ret),) + Ctrl().verify(ret, r, var, value)
|
2017-11-23 15:42:59 +00:00
|
|
|
|
|
|
|
def set_var(sck, var, val):
|
|
|
|
(a, _, _) = do_set_get(sck, var, val)
|
|
|
|
return a
|
|
|
|
|
|
|
|
def _leftovers(sck, fl):
|
|
|
|
"""
|
|
|
|
Read outstanding data if any according to flags
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
data = sck.recv(1024, fl)
|
2017-12-15 11:15:39 +00:00
|
|
|
except socket.error as _:
|
2017-11-23 15:42:59 +00:00
|
|
|
return False
|
|
|
|
if len(data) != 0:
|
|
|
|
tail = data
|
|
|
|
while True:
|
|
|
|
(head, tail) = Ctrl().split_combined(tail)
|
2017-12-15 11:15:39 +00:00
|
|
|
print("Got message:", Ctrl().rem_header(head))
|
2017-11-23 15:42:59 +00:00
|
|
|
if len(tail) == 0:
|
|
|
|
break
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = OptionParser("Usage: %prog [options] var [value]")
|
|
|
|
parser.add_option("-d", "--host", dest="host",
|
|
|
|
help="connect to HOST", metavar="HOST")
|
|
|
|
parser.add_option("-p", "--port", dest="port", type="int",
|
|
|
|
help="use PORT", metavar="PORT", default=4249)
|
|
|
|
parser.add_option("-g", "--get", action="store_true",
|
|
|
|
dest="cmd_get", help="perform GET operation")
|
|
|
|
parser.add_option("-s", "--set", action="store_true",
|
|
|
|
dest="cmd_set", help="perform SET operation")
|
|
|
|
parser.add_option("-v", "--verbose", action="store_true",
|
|
|
|
dest="verbose", help="be verbose", default=False)
|
|
|
|
parser.add_option("-m", "--monitor", action="store_true",
|
|
|
|
dest="monitor", help="monitor the connection for traps", default=False)
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
verbose = options.verbose
|
|
|
|
|
|
|
|
if options.cmd_set and options.cmd_get:
|
|
|
|
parser.error("Get and set options are mutually exclusive!")
|
|
|
|
|
|
|
|
if not (options.cmd_get or options.cmd_set or options.monitor):
|
|
|
|
parser.error("One of -m, -g, or -s must be set")
|
|
|
|
|
|
|
|
if not (options.host):
|
|
|
|
parser.error("Destination host and port required!")
|
|
|
|
|
|
|
|
sock = connect(options.host, options.port)
|
|
|
|
|
|
|
|
if options.cmd_set:
|
|
|
|
if len(args) < 2:
|
|
|
|
parser.error("Set requires var and value arguments")
|
2017-12-15 11:15:39 +00:00
|
|
|
print("Got message:", set_var(sock, args[0], ' '.join(args[1:])))
|
2017-11-23 15:42:59 +00:00
|
|
|
|
|
|
|
if options.cmd_get:
|
|
|
|
if len(args) != 1:
|
|
|
|
parser.error("Get requires the var argument")
|
|
|
|
(a, _, _) = do_set_get(sock, args[0])
|
2017-12-15 11:15:39 +00:00
|
|
|
print("Got message:", a)
|
2017-11-23 15:42:59 +00:00
|
|
|
|
|
|
|
if options.monitor:
|
|
|
|
while True:
|
|
|
|
if not _leftovers(sock, 0):
|
2017-12-15 11:15:39 +00:00
|
|
|
print("Connection is gone.")
|
2017-11-23 15:42:59 +00:00
|
|
|
break
|
|
|
|
sock.close()
|