• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 982
  • Last Modified:

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?

  • 4
1 Solution
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 http://docs.python.org/2/library/subprocess.html

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
subprocess.call(cmd, 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 http://docs.python.org/2/library/os.html#os.getenv). 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 http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline -- try the full paths to the utilities.
Did it work or did not you try?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now