Is there a Ruby equivilant to Python's exec_file?

W

Wayne Pierce

I'm working on a white-box security auditing framework, which is
currently written in Python and makes use of exec_file to load some
security scans at startup. Is there a way to do the same thing in
Ruby?

For those not familiar with Python, exec_file allows me to read an
external file and save the resulting objects to a hash/dictionary of
my choosing. I can then check the values or execute functions that
have been saved in my hash.

The beginning of each security check looks like:

author =3D "Wayne Pierce"
authorEmail =3D "(e-mail address removed)"
license =3D "GNU GPL v2"
licenseLink =3D "http://www.gnu.org/licenses/gpl.html"
date =3D "19 June 2004"
version =3D "1.0.0"
status =3D "production"
platform =3D ["win32"]
category =3D ["system"]
connectionType =3D ["local","remote"]
description =3D "Return a listing of the running processes on a system.=
"

def check():
...

The Python program reads each file and saves the results in a hash
under the scan name. Then I can compare the requirements passed to
the scanner against the saved data; for example I might want to only
run scans that are in cagetory "network", platform "win32", have a
status of "production" and can be run over the network (connectionType
of "remote", "ssh" or "wmi").

I haven't been able to find a way in Ruby to evaluate these files and
save the results to a variable, I would also need to save the function
"check" without executing it. However, if a scan met the criteria I
would then execute the function.

I have looked at Kernel#load, but the results aren't saved.=20
Kernel#readlines could work, although interpreting the code block
portion looks like it would be very ugly.

Does anyone know if there is an existing way to do this?

Thanks for any help,

Wayne
 
J

Joel VanderWerf

Wayne said:
I'm working on a white-box security auditing framework, which is
currently written in Python and makes use of exec_file to load some
security scans at startup. Is there a way to do the same thing in
Ruby?

For those not familiar with Python, exec_file allows me to read an
external file and save the resulting objects to a hash/dictionary of
my choosing. I can then check the values or execute functions that
have been saved in my hash.

Check out "script" on RAA: http://raa.ruby-lang.org/project/script.
 
A

Ara.T.Howard


that's very cool joel! i did something with a similar intent, but slight
different impl, at

http://raa.ruby-lang.org/project/dynaload/

cheers.-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
===============================================================================
 
J

Joel VanderWerf

Ara.T.Howard said:
that's very cool joel! i did something with a similar intent, but slight
different impl, at

http://raa.ruby-lang.org/project/dynaload/

One difference: using script.rb, the loaded file doesn't have to know
that it is being used in that way. The entire file (and its local
dependencies) are encapsulated, rather than only the selected modules
that you pass to Dynaload::export.

I like the #modules and #classes methods of Dynaload. Maybe I will
borrow that for Script instances. Maybe they should even be instance
methods of Module.

It might make sense to propose something like these two libraries as
RCRs, since so many people either ask for it or reinvent it, but the
implementation is so easy that it's probably not worth it...
 
A

Ara.T.Howard

One difference: using script.rb, the loaded file doesn't have to know that
it is being used in that way. The entire file (and its local dependencies)
are encapsulated, rather than only the selected modules that you pass to
Dynaload::export.

yes exactly.
I like the #modules and #classes methods of Dynaload. Maybe I will borrow
that for Script instances. Maybe they should even be instance methods of
Module.

take what you like!
It might make sense to propose something like these two libraries as RCRs,
since so many people either ask for it or reinvent it, but the
implementation is so easy that it's probably not worth it...

on the other hand tons of the built-in libs are really quite simple - it's
what makes them re-usable and more or less bug free ;-)

cheers.


-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top