def _start_video_flow(self, flow, stack): # start apache server which is running on the cctestbed-client with get_ssh_client( flow.client.ip_wan, flow.client.username, key_filename=flow.client.key_filename) as ssh_client: start_apache_cmd = "sudo service apache2 start" exec_command(ssh_client, flow.client.ip_wan, start_apache_cmd) # change default cclag for client with get_ssh_client( flow.client.ip_wan, flow.client.username, key_filename=flow.client.key_filename) as ssh_client: change_ccalg = 'echo {} | sudo tee /proc/sys/net/ipv4/tcp_congestion_control'.format( flow.ccalg) exec_command(ssh_client, flow.client.ip_wan, change_ccalg) #TODO: should change ccalg back to default after running flow # delay flow start for start time plus 3 seconds web_download_cmd = 'timeout {}s google-chrome --disable-gpu --headless --remote-debugging-port=9222 --autoplay-policy=no-user-gesture-required "http://{}:1234/"'.format( flow.end_time, self.client.ip_lan) start_download = RemoteCommand(web_download_cmd, self.server.ip_wan, username=self.server.username, key_filename=self.server.key_filename, pgrep_string='google-chrome'.format( self.client.ip_lan)) stack.enter_context(start_download()) return start_download
def handleconn(mysocket): print("[+] handling connection ") while (True): user_input = mysocket.received_data() print("[+] user input ", user_input) if user_input == "99": break elif user_input == "1": print("[+] running system commands") exec_command(mysocket) # create function to run commands else: print("invalid option")
def cleanup_last_experiment(self, cleanup_tail=True): logging.info('Cleaning up last experiment just in case...') if cleanup_tail: run_local_command('pkill tail') cleanup_cmds = [('sudo pkill -9 tcpdump', ('server', 'client')), ('sudo pkill -9 cat', ('client')), ('sudo pkill iperf', ('server','client')), ('sudo rmmod tcp_probe_ray', ('client'))] for cmd, machines in cleanup_cmds: if 'client' in machines: with get_ssh_client(self.client.ip_wan, username=self.client.username, key_filename=self.client.key_filename) as ssh_client: exec_command(ssh_client, self.client.ip_wan, cmd) if 'server' in machines: with get_ssh_client(self.server.ip_wan, username=self.server.username, key_filename=self.server.key_filename) as ssh_client: exec_command(ssh_client, self.server.ip_wan, cmd)
def _add_nat_rule(instance, nat_ip='128.2.208.128', nat_username='******', nat_key_filename='/home/ranysha/.ssh/id_rsa'): with command.get_ssh_client(ip_addr=nat_ip, username=nat_username, key_filename=nat_key_filename) as ssh_client: cmd = ('sudo iptables -t nat -A PREROUTING -i enp11s0f0 ' '--source {} -j DNAT --to-destination 192.0.0.4').format( instance.public_ip_address) _, stdout, stderr = command.exec_command(ssh_client, nat_ip, cmd) exit_status = stdout.channel.recv_exit_status() return exit_status, stdout.read()
def add_nat_rule(instance, nat_ip='128.2.208.128', nat_username='******', nat_key_filename='/home/ranysha/.ssh/id_rsa'): """Will delete NAT rule when you exit context""" try: yield _add_nat_rule(instance, nat_ip, nat_username, nat_key_filename) finally: with command.get_ssh_client(ip_addr=nat_ip, username=nat_username, key_filename=nat_key_filename) as ssh_client: cmd = 'sudo iptables -t nat --delete PREROUTING 4' _, stdout, stderr = command.exec_command(ssh_client, nat_ip, cmd)
def run_ec2_command(ec2, instance, cmd, ec2_username='******'): key_pair_path = get_key_pair_path(ec2) with command.get_ssh_client(ip_addr=instance.public_ip_address, username=ec2_username, key_filename=key_pair_path) as ssh_client: _, stdout, stderr = command.exec_command(ssh_client, instance.public_ip_address, cmd) # actually should return a bad exit status exit_status = stdout.channel.recv_exit_status() return exit_status, stdout.read()
def wait_for_ssh(ec2, instance, ec2_username='******'): while True: try: with command.get_ssh_client(ip_addr=instance.public_ip_address, username=ec2_username, key_filename=get_key_pair_path(ec2)) as ssh_client: _, stdout, stderr = command.exec_command(ssh_client, instance.public_ip_address, 'echo "TESTING SSH CONNECTION"') break except: logging.info('Waiting 60s for machine to boot') time.sleep(60)
def wait_for_ssh(ec2, instance, ec2_username='******'): while True: try: print(instance.public_ip_address, "--", ec2_username, "--", get_key_pair_path(ec2)) with command.get_ssh_client( ip_addr=instance.public_ip_address, username=ec2_username, key_filename=get_key_pair_path(ec2)) as ssh_client: _, stdout, stderr = command.exec_command( ssh_client, instance.public_ip_address, 'echo "TESTING SSH CONNECTION"') break except: logging.info( 'Waiting 60s for machine to boot. Might contain an error!') print("Error>>", sys.exc_info()[0]) time.sleep(60)
def open_connection(connection_socket): while True: data = connection_socket.recv(512) #Client disconnect if len(data) == 0: print("Client disconnect") listen_for_connection() #<command-id>:<message:id>:[<parameter>] data_str = data.decode("utf-8") msg = message.Message() msg.get_from_string(data_str) result = command.exec_command(msg) if msg.get_command_id() == ct.Command_Types.get_file: send(result, connection_socket, msg.id) else: send_as_list(result, connection_socket, msg.id)