Inconsistent Command Execution Responses using FalconPy SDK #1077
-
Hello FalconPy Community, I am currently working with the FalconPy SDK to execute commands on a remote system using the Real Time Response (RTR) capabilities. I've encountered an inconsistency in the command execution responses that I'm hoping to get some insight into. Issue Description: I am using the execute_command method to run basic commands like ls. On one occasion, I successfully received a "Completed True" status for the ls command, on sequence_id=1, indicating that the command executed and completed as expected. However, subsequent attempts to run the same command result in the script continuously polling without receiving a completion status. Here's a brief overview of my approach:
Additionally, when I attempt to run other commands like dir or echo Hello, I receive a 400 status code with an error message indicating 'Command not found'. Steps Taken: Verified command format and compatibility. Questions:
I would greatly appreciate any guidance, insights, or suggestions you can provide regarding this issue. Below is a snippet of the code I'm using for reference: import time
from falconpy import RealTimeResponse
# Constants
CLIENT_ID = ""
CLIENT_SECRET = ""
DEVICE_ID = ""
MEMBER_CID = ""
def get_falcon_session(client_id, client_secret, member_cid):
falcon = RealTimeResponse(client_id=client_id, client_secret=client_secret, member_cid=member_cid)
return falcon
def init_rtr_session(falcon, device_id):
response = falcon.init_session(device_id=device_id)
if response['status_code'] == 201 and 'resources' in response['body']:
session_id = response['body']['resources'][0]['session_id']
return session_id
else:
print(f"Failed to initialize RTR session. Status Code: {response['status_code']}")
return None
def execute_command(falcon, session_id, command):
response = falcon.execute_command(session_id=session_id, command_string=command, base_command="string", persist=False)
if response['status_code'] in (200, 201) and 'resources' in response['body']:
cloud_request_id = response['body']['resources'][0]['cloud_request_id']
return cloud_request_id
else:
print(f"Failed to execute command. Status Code: {response['status_code']}")
print("Error response:", response)
return None
def check_command_status(falcon, cloud_request_id, sequence_id=0):
response = falcon.check_command_status(cloud_request_id=cloud_request_id, sequence_id=sequence_id)
# Debug print statements
print("Debug: Full response from check_command_status:")
print(response)
if 'errors' in response and response['errors']:
print("Errors in response:", response['errors'])
if 'resources' in response['body']: # Access 'resources' inside 'body'
resource = response['body']['resources'][0]
complete = resource.get('complete', False)
stdout = resource.get('stdout', '')
stderr = resource.get('stderr', '')
print("Command Complete:", complete)
print("Standard Output:", stdout)
print("Standard Error:", stderr)
return complete, stdout, stderr
else:
print("No 'resources' key found in response.")
return False, '', ''
def refresh_rtr_session(falcon, device_id, origin):
response = falcon.pulse_session(device_id=device_id, origin=origin, queue_offline=False)
if response['status_code'] in (200, 201):
print("RTR session refreshed successfully.")
else:
print(f"Failed to refresh RTR session. Status Code: {response['status_code']}")
def main():
falcon = get_falcon_session(CLIENT_ID, CLIENT_SECRET, MEMBER_CID)
session_id = init_rtr_session(falcon, DEVICE_ID)
if not session_id:
return
print(f"Session opened: {session_id}")
try:
cloud_request_id = execute_command(falcon, session_id, 'ls') # or 'dir' for Windows
if not cloud_request_id:
return
complete = False
output = ''
sequence_id = 0
while not complete:
complete, stdout, stderr = check_command_status(falcon, cloud_request_id, sequence_id)
output += stdout
if not complete:
print("Command still executing, waiting for output...")
sequence_id += 1
time.sleep(5) # Wait before polling again
refresh_rtr_session(falcon, DEVICE_ID, "your_origin")
else:
print(f"Command completed. Final output:\n{output}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main() Thank you for your time and assistance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Output from the script:
And it goes on and on. |
Beta Was this translation helpful? Give feedback.
-
Please disregard, I didn't need to increment the sequence_id by one. Once I use only 0, it works! But it's weird that it did work once, when incrementing the sequence_id by one, as it happened here:
|
Beta Was this translation helpful? Give feedback.
Please disregard, I didn't need to increment the sequence_id by one. Once I use only 0, it works!
But it's weird that it did work once, when incrementing the sequence_id by one, as it happened here: