mirror of
https://github.com/radareorg/radare2-r2pipe.git
synced 2026-11-07 06:35:58 -10:00
r2pipe for Ruby
Interact with radare2 from Ruby using one of the supported transports:
- Spawn a local
r2 -q0process - Reuse
R2PIPE_IN/R2PIPE_OUTfrom#!pipe - Connect to an HTTP r2 web server
- Connect to a TCP r2 server
Requirements
- Ruby with the standard library
radare2inPATHfor local spawn and#!pipeuse
Usage
Spawn a local radare2 process
require_relative 'r2pipe'
r2 = R2Pipe.new('/bin/ls')
puts r2.cmd('?V')
puts r2.cmd('pi 5')
p r2.cmdj('ij')
r2.quit
Use named options
require_relative 'r2pipe'
r2 = R2Pipe.new(
file: '/bin/ls',
analyze: true,
writable: false
)
puts r2.cmd('afl')
r2.close
The constructor also accepts analyse and writeable for compatibility with other bindings.
Use from inside radare2 with #!pipe
require_relative 'r2pipe'
r2 = R2Pipe.new('#!pipe')
puts r2.cmd('?e hello-from-r2')
r2.quit
Example:
r2 -q0 -c '#!pipe ruby myscript.rb' /bin/ls
Connect over HTTP
Start radare2 with the web server enabled:
r2 -q0 -e http.bind=127.0.0.1 -e http.port=9090 -c '=H' /bin/ls
Then connect from Ruby:
require_relative 'r2pipe'
r2 = R2Pipe.new('http://127.0.0.1:9090')
puts r2.cmd('pi 5')
p r2.cmdj('ij')
r2.quit
Connect over TCP
require_relative 'r2pipe'
r2 = R2Pipe.new('tcp://127.0.0.1:9080')
puts r2.cmd('?V')
r2.quit
API
R2Pipe.new(target = nil, **options)
Creates a new session.
nil,'', or#!pipe: useR2PIPE_IN/R2PIPE_OUT'/path/to/file': spawnradare2 -q0 /path/to/file'http://host:port': use the HTTP transport'tcp://host:port': use the TCP transport
Options:
file/filenamehttp/urltcpanalyze/analysewritable/writeabledebugflagsr2bin
cmd(command)
Runs an r2 command and returns the string response.
cmdj(command)
Runs an r2 command and parses the response as JSON.
json(string)
Parses a JSON string using the same error handling as cmdj.
quit / close
Closes the current session.
Tests
Run the Ruby suite with:
make -C ruby test