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?

5
  • 1
    subprocess.Popen(["python", "sslyze.py", "--regular", "www.target1.com"]) Commented Apr 22, 2015 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. Commented Apr 22, 2015 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. Commented May 4, 2015 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." Commented May 4, 2015 at 11:33
  • @paddyg process.wait() was missing , that was my problem:) Commented May 8, 2015 at 13:19

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

if I do it like that - I get the following exception: "File "SHA1Scanner.py", line 38, in <module> file_out.write(process.communicate()) TypeError: expected a character buffer object"
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 ;)
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.

1 Comment

it is not my intention to print the domain or I understand you wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.