def connect_to_container(container_id, cmd, extra_ssh_flags=()):
 try:
  cont_data = subprocess.check_output(('docker inspect %s' % container_id), shell=True)
 except subprocess.CalledProcessError:
  print ('Failed to inspect container with id %s' % container_id)
  sys.exit(1)
 try:
  ip_address = json.loads(cont_data)[0]['NetworkSettings']['IPAddress']
 except ValueError:
  print 'Invalid JSON output from inspect command'
  sys.exit(1)
 ssh_key = os.path.join(ROOT_PATH, 'w3af-docker.prv')
 os.chmod(ssh_key, 600)
 ssh_cmd = ['ssh', '-i', ssh_key, '-t', '-t', '-oStrictHostKeyChecking=no', '-o UserKnownHostsFile=/dev/null', '-o LogLevel=quiet']
 for extra_ssh_flag in extra_ssh_flags:
  ssh_cmd.append(extra_ssh_flag)
 ssh_cmd.append(('root@' + ip_address))
 ssh_cmd.append(cmd)
 try:
  subprocess.call(ssh_cmd)
 finally:
  os.chmod(ssh_key, 436)

def connect_to_container ( container_id , cmd , extra_ssh_flags = ( ) ) : try : cont_data = subprocess . check_output ( ( 'docker inspect %s' % container_id ) , shell = True ) except subprocess . CalledProcessError : print ( 'Failed to inspect container with id %s' % container_id ) sys . exit ( 1 ) try : ip_address = json . loads ( cont_data ) [ 0 ] [ 'NetworkSettings' ] [ 'IPAddress' ] except ValueError : print 'Invalid JSON output from inspect command' sys . exit ( 1 ) ssh_key = os . path . join ( ROOT_PATH , 'w3af-docker.prv' ) os . chmod ( ssh_key , 600 ) ssh_cmd = [ 'ssh' , '-i' , ssh_key , '-t' , '-t' , '-oStrictHostKeyChecking=no' , '-o UserKnownHostsFile=/dev/null' , '-o LogLevel=quiet' ] for extra_ssh_flag in extra_ssh_flags : ssh_cmd . append ( extra_ssh_flag ) ssh_cmd . append ( ( 'root@' + ip_address ) ) ssh_cmd . append ( cmd ) try : subprocess . call ( ssh_cmd ) finally : os . chmod ( ssh_key , 436 )
connect to a running container .