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
  [SOLVED] [Linux] Run Python script through cron? Winfried 2 1,253 Oct-19-2024, 06:29 PM
Last Post: Winfried
  Linux, Python, C ++, Brain2 and errors. PiotrBujakowski 0 1,022 Jun-24-2024, 03:41 PM
Last Post: PiotrBujakowski
  Is possible to run the python command to call python script on linux? cuten222 6 2,410 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  How to use a variable in linux command in python code? ilknurg 2 2,440 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  use subprocess on linux\pi wwith a "grep " command korenron 2 11,307 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Python syntax in Linux St0rmcr0w 2 66,316 Jul-29-2021, 01:40 PM
Last Post: snippsat
  Login to NordVPN on Linux with python script AGreenPig 2 7,551 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  Duplex pipes GrahamL 0 2,366 Dec-16-2020, 09:44 AM
Last Post: GrahamL
  Handling multi-input/output audio in python bor1904 4 4,938 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  where to get portable Python for Linux (Fedora)? python001 5 8,865 Nov-01-2020, 05:23 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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