Advertisement
limmen

client manager API

Mar 23rd, 2023
3,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.15 KB | None | 0 0
  1. import time
  2. from typing import List
  3. import csle_collector.client_manager.client_manager_pb2_grpc
  4. import csle_collector.client_manager.client_manager_pb2
  5. import csle_collector.client_manager.query_clients
  6. import grpc
  7.  
  8.  
  9. def stop_client_population():
  10.     with grpc.insecure_channel(f'172.18.0.3:50044') as channel:
  11.         stub = csle_collector.client_manager.client_manager_pb2_grpc.ClientManagerStub(channel)
  12.         stop_clients_msg = csle_collector.client_manager.client_manager_pb2.StopClientsMsg()
  13.         clients_dto = stub.stopClients(stop_clients_msg)
  14.         return clients_dto
  15.  
  16.  
  17. def start_client_population(mu: float, lamb: float, time_step_len_seconds: int, commands : List[str],
  18.                             sine_modulated: bool, num_commands: int, period_scaling_factor: float,
  19.                             time_scaling_factor: float):
  20.     # Open a gRPC session
  21.     with grpc.insecure_channel(f'172.18.0.3:50044') as channel:
  22.         stub = csle_collector.client_manager.client_manager_pb2_grpc.ClientManagerStub(channel)
  23.         start_clients_msg = csle_collector.client_manager.client_manager_pb2.StartClientsMsg(
  24.             mu=mu, lamb=lamb, time_step_len_seconds=time_step_len_seconds, commands=commands,
  25.             num_commands=num_commands, sine_modulated=sine_modulated, period_scaling_factor=period_scaling_factor,
  26.             time_scaling_factor=time_scaling_factor
  27.         )
  28.         clients_dto = stub.startClients(start_clients_msg, timeout=300)
  29.         return clients_dto
  30.  
  31.  
  32. def stop_client_producer():
  33.     with grpc.insecure_channel(f'172.18.0.3:50044') as channel:
  34.         stub = csle_collector.client_manager.client_manager_pb2_grpc.ClientManagerStub(channel)
  35.         stop_producer_msg = csle_collector.client_manager.client_manager_pb2.StopProducerMsg()
  36.         clients_dto = stub.stopProducer(stop_producer_msg, timeout=300)
  37.         return clients_dto
  38.  
  39. def start_client_producer(kafka_ip: str, kafka_port: int, time_step_len_seconds: int):
  40.     with grpc.insecure_channel(f'172.18.0.3:50044') as channel:
  41.         stub = csle_collector.client_manager.client_manager_pb2_grpc.ClientManagerStub(channel)
  42.         start_producer_msg = csle_collector.client_manager.client_manager_pb2.StartProducerMsg(
  43.             ip=kafka_ip, port=kafka_port,
  44.             time_step_len_seconds=time_step_len_seconds
  45.         )
  46.         clients_dto = stub.startProducer(start_producer_msg, timeout=300)
  47.         return clients_dto
  48.  
  49. if __name__ == '__main__':
  50.     print("Stopping client population..")
  51.     stop_client_population()
  52.     print("client population stopped")
  53.     time.sleep(20)
  54.     print("Starting client population")
  55.     start_client_population(lamb=20, mu=4, num_commands=2, time_scaling_factor=0.01, period_scaling_factor=20,
  56.                             time_step_len_seconds=30, sine_modulated=True, commands = [
  57.             "timeout 5 sshpass -p 'testcsleuser' ssh -oStrictHostKeyChecking=no {} > /dev/null 2>&1".format("15.4.2.21"),
  58.             "timeout 5 snmpwalk -v2c {} -c csle_1234 > /dev/null 2>&1".format("15.4.2.21"),
  59.             "timeout 10 /irc_login_test.sh {} > /dev/null 2>&1".format("15.4.2.21"),
  60.             "timeout 5 psql -h {} -p 5432 > /dev/null 2>&1".format("15.4.2.21"),
  61.             "timeout 5 ping {} > /dev/null 2>&1".format("15.4.2.21"),
  62.             "timeout 5 traceroute {} > /dev/null 2>&1".format("15.4.2.21"),
  63.             "timeout 5 sshpass -p 'testcsleuser' ssh -oStrictHostKeyChecking=no {} > /dev/null 2>&1".format("15.4.2.10"),
  64.             "timeout 5 ping {} > /dev/null 2>&1".format("15.4.2.10"),
  65.             "timeout 5 traceroute {} > /dev/null 2>&1".format("15.4.2.10"),
  66.             "timeout 5 sshpass -p 'testcsleuser' ssh -oStrictHostKeyChecking=no {} > /dev/null 2>&1".format("15.4.2.78"),
  67.             "timeout 5 curl {}:80 > /dev/null 2>&1".format("15.4.2.78"),
  68.             "timeout 5 ping {} > /dev/null 2>&1".format("15.4.2.78"),
  69.             "timeout 5 traceroute {} > /dev/null 2>&1".format("15.4.2.78"),
  70.             "timeout 5 sshpass -p 'testcsleuser' ssh -oStrictHostKeyChecking=no {} > /dev/null 2>&1".format("15.4.2.3"),
  71.             "timeout 5 curl {} > /dev/null 2>&1".format("15.4.2.3"),
  72.             "(sleep 2; echo test; sleep 2; echo test; sleep 3;) | telnet {} > /dev/null 2>&1".format("15.4.2.3"),
  73.             "timeout 5 ping {} > /dev/null 2>&1".format("15.4.2.3"),
  74.             "timeout 5 traceroute {} > /dev/null 2>&1".format("15.4.2.3"),
  75.             "timeout 5 ftp {} > /dev/null 2>&1".format("15.4.2.79"),
  76.             "timeout 5 sshpass -p 'testcsleuser' ssh -oStrictHostKeyChecking=no {} > /dev/null 2>&1".format("15.4.2.79"),
  77.             "timeout 5 curl {}:8080 > /dev/null 2>&1".format("15.4.2.79"),
  78.             "timeout 5 ping {} > /dev/null 2>&1".format("15.4.2.79"),
  79.             "timeout 5 traceroute {} > /dev/null 2>&1".format("15.4.2.79")
  80.         ])
  81.     print("Client population started")
  82.     time.sleep(20)
  83.     print("Stopping the client producer")
  84.     stop_client_producer()
  85.     print("Client producer stopped")
  86.     time.sleep(20)
  87.     print("Starting the client producer")
  88.     start_client_producer(kafka_ip="15.4.253.253", kafka_port=9092, time_step_len_seconds=30)
  89.     print("Client producer started")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement