1

I'm working on following script to automate enumeration phase.

I use subprocess to run nmap like this:

subprocess.run(["nmap", "192.168.1.1"])

I want to achive possibility to kill this subprocess and execute rest of code, but when i pick ctrl + c whole script run down.

3
  • Welcome. Could you please include the overall structure so that it could bring more clarity to the question? Commented Nov 12, 2020 at 15:05
  • of course there is line of code like as above and user doesn't want to wait for the result of that command so how can he just skip it without killing whole script subprocess.run(["nmap", "192.168.1.1"]) - i don't want to wait for result of that command just skip to next Commented Nov 12, 2020 at 15:13
  • Please, provide example of your code, so that developers here can reproduce your error and help you with it, thank you Commented Nov 12, 2020 at 15:20

3 Answers 3

1

Background it by doing :

ctrl+z
Sign up to request clarification or add additional context in comments.

1 Comment

yes but it hang process, i want to kill just that one subprocess to execute rest of code
0

You can make subprocess in try / except:

try:
    subprocess.run(["nmap", "192.168.1.1"])
except:
    print("Nmap Stopped.")
    pass

When you press CTRL+C an error will be occur and you handle it with except section.

1 Comment

No problem. Just select this comment as answer if your problem fixed.
0

this is where you should use subprocess.Popen or subprocess.call rather than subprocess.run

subprocess.call will be a blocking call, which mean it wait until the command finish and then it will move on with your code.

or another way is to put your subprocess.run in an async function and execute it separately

example

import asyncio
async def run_nmap(ip:str):
    subprocess.run(["nmap", ip])

def run_the_thing():
    ...
    loop = asyncio.get_event_loop()
    loop.create_task(run_nmap("192.168.1.1"))
    ...

Comments

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.