Passing bash argument from python with pexpect or popen


I am trying to pass below argument from python. Before i used to use os system etc to do that, now i am trying to do it using pexpect and popen.

However i could not pass |, sort, uniq kind of commands. For example,

tshark -o column.format:'"Source", "%s", "Destination", "%d", "dstport", "%uD"' -r $FILE |sort|uniq

I devide the command as lists but could not figure out how to send |sort|uniq part.

Can someone help me on this with an example?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What version of Python do you use? I guess it is Python 2.x, but what is the x? If your Python version is greater or equal to 2.4, you should not oly to switch from os.system to os.popen. You should use the subprocess module instead.

See the doc

The $FILE is the shell variable. This way, you must at leasts start the processing via a shell from inside the Python program. If the case is simple enough, try the, shell=True)

Open in new window

where cmd is a string variable with your command that you would otherwise wrote at the console command line. Notice the shell=True.

You can also get the value of the $FILE variable via os.getenv (see the Anyway, the | pipe symbol is also interpreted by the shell. This way, the shell=True must be used also because of that. Alternatively, you can use subprocess.Popen with the standard output redirection using the subprocess.PIPE. But I prefer the simpler solution if it works.
b4silAuthor Commented:
Thanks for the answer

Normally for the bash without pipe, i have already used subprocess

def hostIPv4(filename):
       fields = ['frame_num', 'IP Src', 'TCP Src']
      cmd = r"tshark -r %s -q -z hosts,ipv4 " % (filename)
      subcmd = cmd.split(' ')
      lines = subprocess.Popen(subcmd, stdout = subprocess.PIPE)
      return lines

However even with a redirection, i could not handle pipe stuff regarding the above bash.

Besides that I am aware of shell=True solution but it is too risky in terms of security especially if you get user input with your program. it will end command execution issues.
I will write more detailed answer later. Anyway, if you do not use shell=True, I am not sure, but I believe you should pass full path utility names. The reason is that the shell is the one that scans the PATH when searching for the executables.
Have a look at -- try the full paths to the utilities.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Did it work or did not you try?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.