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_zmq_client.py | 76 +++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) (limited to 'scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py') 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.') + -- cgit 1.2.3-korg