Python Forum
how to run linux command with multi pipes by python !!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to run linux command with multi pipes by python !!
hey all ..
i am trying to run this command on my machine by using python subprocess module ...
command :
ping -c 1 -t 1 -w 1 | grep "ttl=" | awk {'print $4'} | sed 's/.$//'

import subprocess

q=['ping', '-c' ,'1' ,'' , '| grep ttl' , ' awk {"print $4"}' , " sed 's/.$//'"], capture_output=True)
i dont get an error i just get an empty result !!

import subprocess
q= subprocess.check_output("ping -c 1 -t 1 -w 1 | grep 'ttl=' | awk {'print $4'} | sed 's/.$//'", shell=True)
import shlex
from subprocess import PIPE, Popen

def run_pipes(cmds):
    Run commands in PIPE, return the last process in chain
    cmds = map(shlex.split, cmds)
    first_cmd, *rest_cmds = cmds
    procs = [Popen(first_cmd, stdout=PIPE)]
    for cmd in rest_cmds:
        last_stdout = procs[-1].stdout
        proc = Popen(cmd, stdin=last_stdout, stdout=PIPE)
    return procs[-1]

cmds = [
    "ping -c1",
    "grep ttl",
    "awk '{print $4}'",
    "sed 's/.$//'",

last_proc = run_pipes(cmds)
stdout = last_proc.stdout
for line in stdout:
    line = line.decode()
    print(line, end="")
The function shlex.split splits a string like the shell is doing it.
This is useful to split the command. Using shell=True is not so good because it makes vulnerabilities possible.

This part here, can be done with Python instead of rely on os dependend tools.
For example, grep, awk and sed are not installed on Windows.
grep 'ttl=' | awk {'print $4'} | sed 's/.$//'"
The ping command itself could still be used because it's almost everywhere available.
Some options differs from implementation to implementation. So Windows-Ping has for example some different options.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python syntax in Linux St0rmcr0w 2 158 Jul-29-2021, 01:40 PM
Last Post: snippsat
  Duplex pipes GrahamL 0 331 Dec-16-2020, 09:44 AM
Last Post: GrahamL
  Handling multi-input/output audio in python bor1904 4 948 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  where to get portable Python for Linux (Fedora)? python001 5 753 Nov-01-2020, 05:23 PM
Last Post: Larz60+
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 538 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Python in Linux environment on RPI kendias 22 5,984 Sep-05-2020, 03:04 AM
Last Post: K_Research
  How do I pick the right python in Linux env? MDRI 9 1,250 Jun-27-2020, 05:40 PM
Last Post: snippsat
  control a linux program with python Fifoux082 9 1,538 May-08-2020, 04:24 PM
Last Post: Fifoux082
  Python version on Linux whois1230 5 1,260 Apr-10-2020, 07:12 PM
Last Post: buran
  Unable to install python-wnck python-imaging on Void Linux linuxlight 1 1,177 Mar-08-2020, 03:53 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020