1

I write this command on linux

netstat -ant | wc -l

but when I try to call from python with

subprocess.Popen(['netstat','-ant','|','wc','-l'])

I cant get all output, I see just result of first command (netstat -ant). How can I process this command on python ? (note: this command gives a int as a result)

Paul Rooney
  • 19,499
  • 9
  • 39
  • 60
pala9323
  • 201
  • 1
  • 2
  • 6

1 Answers1

0

I don't know if there's any easier method but you can go like:

from subprocess import run, Popen, PIPE
sess1 = run(['netstat', 'ant'], stdout=PIPE)
sess2 = Popen(['grep', '"SYN"'], stdin=PIPE)
sess2.stdin.write(sess1.stdout)
sess2.communicate() # required?
Piotr Kamoda
  • 896
  • 1
  • 9
  • 24