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
  • 20,879
  • 9
  • 40
  • 61
pala9323
  • 211
  • 1
  • 3
  • 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
  • 956
  • 1
  • 9
  • 24