From 5f9c0e65223f26de99958260420601670df4e012 Mon Sep 17 00:00:00 2001 From: itraviv Date: Sun, 21 Aug 2016 18:00:29 +0300 Subject: scapy_service: supported_methods now returns all functions when invoked with parameter 'all' zmq_client: added simple console to interact with server scapy_zmq_server: added verbosity feature --- .../stl/services/scapy_server/scapy_service.py | 4 +- .../stl/services/scapy_server/scapy_zmq_client.py | 76 +++++++++++++++++++++- .../stl/services/scapy_server/scapy_zmq_server.py | 8 +++ 3 files changed, 84 insertions(+), 4 deletions(-) (limited to 'scripts/automation/trex_control_plane/stl/services/scapy_server') diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py index 9b05c4ef..11a9db42 100755 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py @@ -269,8 +269,8 @@ class Scapy_service(Scapy_service_api): def get_version(self): return {'built_by':'itraviv','version':'v1.01'} - def supported_methods(self,method_name=''): - if method_name=='': + def supported_methods(self,method_name='all'): + if method_name=='all': methods = {} for f in dir(Scapy_service): if inspect.ismethod(eval('Scapy_service.'+f)): diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py index 24e1593e..18d32272 100644 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py @@ -1,15 +1,22 @@ +import sys +import os +python2_zmq_path = os.path.abspath(os.path.join(os.pardir,os.pardir,os.pardir,os.pardir, + os.pardir,'external_libs','pyzmq-14.5.0','python2','fedora18','64bit')) +sys.path.append(python2_zmq_path) import zmq import json - +from argparse import * +from pprint import pprint class Scapy_server_wrapper(): def __init__(self,dest_scapy_port=5555,server_ip_address='localhost'): + self.server_ip_address = server_ip_address self.context = zmq.Context() self.socket = self.context.socket(zmq.REQ) self.dest_scapy_port =dest_scapy_port - self.socket.connect("tcp://"+str(server_ip_address)+":"+str(self.dest_scapy_port)) #ip address of csi-trex-11 + self.socket.connect("tcp://"+str(self.server_ip_address)+":"+str(self.dest_scapy_port)) def call_method(self,method_name,method_params): json_rpc_req = { "jsonrpc":"2.0","method": method_name ,"params": method_params, "id":"1"} @@ -42,3 +49,68 @@ class Scapy_server_wrapper(): def _get_all_pkt_offsets(self,pkt_desc): return self.call_method('_get_all_pkt_offsets',[pkt_desc]) + + def _activate_console(self): + context = zmq.Context() + # Socket to talk to server + print 'Connecting:' + socket = context.socket(zmq.REQ) + socket.connect("tcp://"+str(self.server_ip_address)+":"+str(self.dest_scapy_port)) + try: + print('This is a simple console to communicate with Scapy server.\nInvoke supported_methods (with 1 parameter = all) to see supported commands\n') + while True: + command = raw_input("enter RPC command [enter quit to exit]:\n") + if (command == 'quit'): + break + parameter_num = 0 + params = [] + while True: + try: + parameter_num = int(raw_input('Enter number of parameters to command:\n')) + break + except Exception: + print('Invalid input. Try again') + for i in range(1,parameter_num+1,1): + print "input parameter %d:" % i + user_parameter = raw_input() + params.append(user_parameter) + pprint_output = raw_input('pprint the output [y/n]? ') + while ((pprint_output!= 'y') and (pprint_output!='n')): + pprint_output = raw_input('pprint the output [y/n]? ') + json_rpc_req = { "jsonrpc":"2.0","method": command ,"params":params, "id":"1"} + request = json.dumps(json_rpc_req) + print("Sending request in json format %s " % request) + socket.send(request) + + # Get the reply. + message = socket.recv() + print ('received reply:') + parsed_message = json.loads(message) + if (pprint_output == 'y'): + pprint(parsed_message) + else: + print message + except KeyboardInterrupt: + print('Terminated By Ctrl+C') + finally: + socket.close() + context.destroy() + + + +if __name__=='__main__': + parser = ArgumentParser(description='Example of client module for Scapy server ') + parser.add_argument('-p','--dest-scapy-port',type=int, default = 4507, dest='dest_scapy_port', + help='Select port to which this Scapy Server client will send to.\n default is 4507\n',action='store') + parser.add_argument('-s','--server',type=str, default = 'localhost', dest='dest_scapy_ip', + help='Remote server IP address .\n default is localhost\n',action='store') + parser.add_argument('-c','--console', + help='Run simple client console for Scapy server.\nrun with \'-s\' and \'-p\' to determine IP and port of the server\n', + action='store_true',default = False) + args = parser.parse_args() + if (args.console): + s = Scapy_server_wrapper(args.dest_scapy_port,args.dest_scapy_ip) + sys.exit(s._activate_console()) + else: + print('Scapy client: for interactive console re-run with \'-c\', else import as seperate module.') + diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py index 0b88668a..3f0bf3cc 100755 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py @@ -73,6 +73,7 @@ class Scapy_wrapper: def error_handler(self,e,req_id): + response = [] try: raise e except ParseException as e: @@ -108,7 +109,11 @@ class Scapy_server(): try: while True: message = self.socket.recv_string() + if args.verbose: + print('Received Message: %s \n' % message) try: + params = [] + method='' req_id = 'null' method,params,req_id = self.scapy_wrapper.parse_req_msg(message) if (method == 'shut_down'): @@ -121,6 +126,8 @@ class Scapy_server(): response = self.scapy_wrapper.error_handler(e,req_id) finally: json_response = json.dumps(response) + if args.verbose: + print('Sending Message: %s \n' % json_response) # Send reply back to client self.socket.send_string(json_response) if (method == 'shut_down'): @@ -145,6 +152,7 @@ if __name__=='__main__': parser = ArgumentParser(description=' Runs Scapy Server ') parser.add_argument('-s','--scapy-port',type=int, default = 4507, dest='scapy_port', help='Select port to which Scapy Server will listen to.\n default is 4507\n',action='store') + parser.add_argument('-v','--verbose',help='Print Client-Server Request-Reply logging',action='store_true',default = False) args = parser.parse_args() port = args.scapy_port sys.exit(main(port)) -- cgit 1.2.3-korg