Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing netcat shell command output in Python [duplicate]

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
like image 391
user3383192 Avatar asked Dec 01 '25 02:12

user3383192


1 Answers

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.

like image 163
Mikko Ohtamaa Avatar answered Dec 02 '25 17:12

Mikko Ohtamaa