Python:
import subprocess import json def osquery(query): return json.loads(subprocess.check_output(["osqueryi", "--json", query])) #>>> osquery("select * from routes where destination = '::1'") #>>> [{'destination': '::1', 'flags': '2098181', 'gateway': '::1', 'hopcount': '0', 'interface': 'lo0', 'metric': '0', 'mtu': '16384', 'netmask': '128', 'source': '', 'type': 'local'}]
const { execFile } = require("child_process"); async function osquery(query) { return new Promise((resolve, reject) => { execFile("osqueryi", ["--json", query], (error, out, _err) => { if (error) { reject(error); return; } resolve(JSON.parse(out)); }); }); } //> await osquery("select * from routes where destination = '::1'"); //> [ //> { //> destination: '::1', //> flags: '2098181', //> gateway: '::1', //> hopcount: '0', //> interface: 'lo0', //> metric: '0', //> mtu: '16384', //> netmask: '128', //> source: '', //> type: 'local' //> } //> ]
require "open3" require "json" def osquery(query) out, err, exit_status = Open3.capture3("osqueryi", "--json", query) if exit_status != 0 raise err end return JSON.parse(out, symbolize_names: true) end #irb(main):1:0> osquery("select * from routes where destination = '::1'") #=> #[{:destination=>"::1", # :flags=>"2098181", # :gateway=>"::1", # :hopcount=>"0", # :interface=>"lo0", # :metric=>"0", # :mtu=>"16384", # :netmask=>"128", # :source=>"", # :type=>"local"}]
Python:
Node: Ruby: