user169294 user169294 - 1 month ago 7
Python Question

Print variable line by line with string in front Python 2.7

I am writing a recon tool in Python and I've run into a bit of a problem trying to print a string in front of a multiple line variable without editing the string itself

Here is my sliver of code:

# ...
query1 = commands.getoutput("ls -1 modules/recon | grep '.*\.py$' | grep -v '__init__.py'")
print("module/%s/%s" % (module_type, query1.strip(".py"))


I want to add the "module/#module_type/#module_name" and the module name being the only changing thing. So, using the shodan and bing module (random) the output would look something like this:

modules/recon/shodan
modules/recon/bing


but instead I get

modules/recon/bing.py
shodan


Thanks!

Answer

You can do what you are asking for like this:

from os import path

module_type = 'recon'
q = 'shoban.py\nbing.py'  # insert the your shell invocation here
modules = (path.splitext(m)[0] for m in q.split('\n'))
formatted = ('modules/%s/%s' % (module_type, m) for m in modules)
print('\n'.join(formatted))

output:

modules/recon/shodan
modules/recon/bing

But since you are already calling a unix shell from python, you might as well use sed for string processing:

print(commands.getoutput("ls modules/recon/ | sed '/.py$/!d; /^__init__.py$/d; s/\.py$//; s/^/modules\/recon\//'"))

You can also use shell's "globbing" feature to make the command simpler if the location where you are looking for the modules (e.g modules/recon) matches the prefix you need to output:

print(commands.getoutput("ls modules/recon/*.py | sed 's/.py$//; /\/__init__$/d'"))

Another option is to use just python's standard library:

from os import path
import glob

module_type = 'recon'
module_paths = glob.iglob('modules/recon/*.py')
module_files = (m for m in map(path.basename, modules) if m != '__init___.py')
modules = (path.splitext(m)[0] for m in module_files)
formatted = ("modules/%s/%s" % (module_type, m) for m in modules)
print('\n'.join(formatted))