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 !!
#1
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 192.168.1.81 | grep "ttl=" | awk {'print $4'} | sed 's/.$//'
code

import subprocess

q= subprocess.run(['ping', '-c' ,'1' ,'192.168.1.81' , '| grep ttl' , ' awk {"print $4"}' , " sed 's/.$//'"], capture_output=True)
print(q.stdout.decode())
i dont get an error i just get an empty result !!
Reply
#2
try

import subprocess
 
q= subprocess.check_output("ping -c 1 -t 1 -w 1 192.168.1.81 | grep 'ttl=' | awk {'print $4'} | sed 's/.$//'", shell=True)
print(q.decode())
Reply
#3
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)
        procs.append(proc)
    return procs[-1]


cmds = [
    "ping -c1 5.9.16.40",
    "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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 635 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  How to use a variable in linux command in python code? ilknurg 2 1,548 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  use subprocess on linux\pi wwith a "grep " command korenron 2 7,909 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Python syntax in Linux St0rmcr0w 2 44,931 Jul-29-2021, 01:40 PM
Last Post: snippsat
  Login to NordVPN on Linux with python script AGreenPig 2 5,912 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  Duplex pipes GrahamL 0 1,728 Dec-16-2020, 09:44 AM
Last Post: GrahamL
  Handling multi-input/output audio in python bor1904 4 3,498 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  where to get portable Python for Linux (Fedora)? python001 5 6,161 Nov-01-2020, 05:23 PM
Last Post: Larz60+
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 1,695 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Python in Linux environment on RPI kendias 22 10,914 Sep-05-2020, 03:04 AM
Last Post: K_Research

Forum Jump:

User Panel Messages

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