diff --git a/client/experimental_lib/00make_swig.sh b/client/experimental_lib/00make_swig.sh index 6c752d024..626a87986 100755 --- a/client/experimental_lib/00make_swig.sh +++ b/client/experimental_lib/00make_swig.sh @@ -1,4 +1,5 @@ #!/bin/bash -swig -lua -o ../src/pm3_luawrap.c ../src/pm3.i -swig -python -o ../src/pm3_pywrap.c ../src/pm3.i +cd .. +make src/pm3_luawrap.c +make src/pm3_pywrap.c diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 454e08e00..2bc02fd4b 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -455,7 +455,6 @@ else (SKIPWHEREAMISYSTEM EQUAL 1) message(STATUS "Whereami library: system library not found, using local library") endif (WHEREAMI_FOUND) endif (SKIPWHEREAMISYSTEM EQUAL 1) -message(STATUS "===================================================================") # Lua SWIG if (EXISTS ${PM3_ROOT}/client/src/pm3_luawrap.c) @@ -463,7 +462,7 @@ if (EXISTS ${PM3_ROOT}/client/src/pm3_luawrap.c) ${PM3_ROOT}/client/src/pm3_luawrap.c ${TARGET_SOURCES}) add_definitions(-DHAVE_LUA_SWIG) - message("Lua SWIG wrapper found") + message(STATUS "Lua SWIG: wrapper found") endif (EXISTS ${PM3_ROOT}/client/src/pm3_luawrap.c) # Python SWIG @@ -474,10 +473,11 @@ if (NOT SKIPPYTHON EQUAL 1) ${PM3_ROOT}/client/src/pm3_pywrap.c ${TARGET_SOURCES}) add_definitions(-DHAVE_PYTHON_SWIG) - message("Python SWIG wrapper found") + message(STATUS "Python SWIG: wrapper found") endif (EXISTS ${PM3_ROOT}/client/src/pm3_pywrap.c) endif (PYTHON3EMBED_FOUND OR PYTHON3_FOUND) endif (NOT SKIPPYTHON EQUAL 1) +message(STATUS "===================================================================") add_library(pm3rrg_rdv4 SHARED ${PM3_ROOT}/client/src/proxmark3.c diff --git a/client/experimental_lib/example_py/02run_test_grab_interactive.sh b/client/experimental_lib/example_py/02run_test_grab_interactive.sh new file mode 100755 index 000000000..d7dd7dd16 --- /dev/null +++ b/client/experimental_lib/example_py/02run_test_grab_interactive.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +PYTHONPATH=../../src ipython3 -i ./test_grab.py diff --git a/client/experimental_lib/example_py/output_grabber.py b/client/experimental_lib/example_py/output_grabber.py new file mode 100755 index 000000000..ad405c9d9 --- /dev/null +++ b/client/experimental_lib/example_py/output_grabber.py @@ -0,0 +1,78 @@ +import os +import sys +import threading +import time + +# From https://stackoverflow.com/a/29834357 +class OutputGrabber(object): + """ + Class used to grab standard output or another stream. + """ + escape_char = "\b" + + def __init__(self, stream=None, threaded=False): + self.origstream = stream + self.threaded = threaded + if self.origstream is None: + self.origstream = sys.stdout + self.origstreamfd = self.origstream.fileno() + self.capturedtext = "" + # Create a pipe so the stream can be captured: + self.pipe_out, self.pipe_in = os.pipe() + + def __enter__(self): + self.start() + return self + + def __exit__(self, type, value, traceback): + self.stop() + + def start(self): + """ + Start capturing the stream data. + """ + self.capturedtext = "" + # Save a copy of the stream: + self.streamfd = os.dup(self.origstreamfd) + # Replace the original stream with our write pipe: + os.dup2(self.pipe_in, self.origstreamfd) + if self.threaded: + # Start thread that will read the stream: + self.workerThread = threading.Thread(target=self.readOutput) + self.workerThread.start() + # Make sure that the thread is running and os.read() has executed: + time.sleep(0.01) + + def stop(self): + """ + Stop capturing the stream data and save the text in `capturedtext`. + """ + # Print the escape character to make the readOutput method stop: + self.origstream.write(self.escape_char) + # Flush the stream to make sure all our data goes in before + # the escape character: + self.origstream.flush() + if self.threaded: + # wait until the thread finishes so we are sure that + # we have until the last character: + self.workerThread.join() + else: + self.readOutput() + # Close the pipe: + os.close(self.pipe_in) + os.close(self.pipe_out) + # Restore the original stream: + os.dup2(self.streamfd, self.origstreamfd) + # Close the duplicate stream: + os.close(self.streamfd) + + def readOutput(self): + """ + Read the stream data (one byte at a time) + and save the text in `capturedtext`. + """ + while True: + char = os.read(self.pipe_out,1).decode(self.origstream.encoding) + if not char or self.escape_char in char: + break + self.capturedtext += char diff --git a/client/experimental_lib/example_py/test_grab.py b/client/experimental_lib/example_py/test_grab.py new file mode 100755 index 000000000..7cec5a94f --- /dev/null +++ b/client/experimental_lib/example_py/test_grab.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +import pm3 +from output_grabber import OutputGrabber + +out = OutputGrabber() +p=pm3.pm3("/dev/ttyACM1") +print("Device:", p.name) +with out: + p.console("hw status") +for line in out.capturedtext.split('\n'): + if "Unique ID" in line: + print(line)