user169294 user169294 - 2 months ago 8
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 ''")
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:


but instead I get




You can do what you are asking for like this:

from os import path

module_type = 'recon'
q = '\'  # 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)



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; /^$/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 != '')
modules = (path.splitext(m)[0] for m in module_files)
formatted = ("modules/%s/%s" % (module_type, m) for m in modules)