I want execute netcat command from my python script, so I use the 'subprocess.Popen', but the problem is that the output of this command is directly printed in may shell console, i want o get him in a variable , so i can do some modification before printing it.
res = subprocess.Popen("nc -v 192.168.1.1 25", stdout=subprocess.PIPE, stderr=None, shell=True)
#output = res.communicate()
#output = str(output)
#print output
If you want make calling shell commands easy to yourself in Python use sh library.
In sh this would be:
from sh import nc
output = nc("-v", "192.168.1.1", "25") # output is string of the command output
As always, installing Python libraries are recommended to do with virtualenv.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With