0

I read already this What is the best way to call a Python script from another Python script?

In my case I don't want to call another python script in a python script, but I want to call for example the ssylze.py with the specific options

$ python sslyze.py --regular www.target1.com

like consider in https://code.google.com/p/sslyze/wiki/QuickStart

So I have script test1.py and in that script I would like to use

sslyze.py --regular www.target1.com

how I do that?

Community
  • 1
  • 1
Loretta
  • 154
  • 15
  • 1
    subprocess.Popen(["python", "sslyze.py", "--regular", "www.target1.com"]) – paddyg Apr 22 '15 at 12:36
  • Why don't you just `import sslyze` and call whatever entry point function the command line call would? If it's sensibly structured, this will be trivial. – jonrsharpe Apr 22 '15 at 12:41
  • @paddyg the subprocess works fine, but I have two problems. for the "www.target1.com" I want to use a variable, so that I can make for-loop and read form txt different ips (but it didn't work out), and the second one is that I don't want that this command print the information on the console, but that it print this in a new file. – Loretta May 04 '15 at 10:52
  • import subprocess file_in = open("ip.txt", "r") fname = "scan.txt" file_out = open("scan.txt","w") i = 1 for line in file_in: print line process = subprocess.Popen(["python", "sslyze.py", "--regular", line], stdout=subprocess.PIPE) print process.stdout.read() i = i + 1 file_out.close() file_in.close() the problem is that it only can scan the last ip, at the others ips it says "=> WARNING: Could not resolve hostname; discarding corresponding tasks." – Loretta May 04 '15 at 11:33
  • @paddyg process.wait() was missing , that was my problem:) – Loretta May 08 '15 at 13:19

2 Answers2

1

Not sure if I've unscrambled the code from your comment ok and whether this is what you are trying to do. As I don't know what sslyze.py is doing I haven't tested it. However your problem might be due to not waiting for each subprocess to terminate:

import subprocess 
with open("ip.txt", "r") as file_in:
  fname = "scan.txt" 
  with open("scan.txt","w") as file_out:
    for line in file_in: 
      process = subprocess.Popen(["python", "sslyze.py", "--regular", line], stdout=subprocess.PIPE) 
      file_out.write(process.communicate()) # you might need to append \n to whatever you write
paddyg
  • 2,153
  • 20
  • 24
  • if I do it like that - I get the following exception: "File "SHA1Scanner.py", line 38, in file_out.write(process.communicate()) TypeError: expected a character buffer object" – Loretta May 08 '15 at 11:23
  • process.wait() was missing , that was my problem. but thanks for the indirect tip with the "with" command - it is much better that I did ;) – Loretta May 08 '15 at 13:19
0

You can use the argparse module:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--regular", action="store")
args = parser.parse_args()
print vars(args)["regular"]

Running the above snippet (asd.py) with python asd.py --regular www.target1.com will print "www.target1.com". Hope this provides enough of an example to be helpful.

EvenLisle
  • 4,672
  • 3
  • 24
  • 47