1

I am new to this site so hopefully this is the correct location to place this question.

I am trying to write a script using python for Linux, that:

  1. creates a file file.txt
  2. appends the output of the 'lsof' command to file.txt
  3. read each line of the output and append them to an array.
  4. then print each line.

I'm basically just doing this to familiarize myself with using python for bash, I'm new to this area so any help would be great. I'm not sure where to go from here. Also if there is a better way to do this I'm open to that!

#!/usr/bin/env python
import subprocess

touch = "touch file.txt"
subprocess.call(touch, shell=True)
xfile = "file.txt"

connection_count = "lsof -i tcp | grep ESTABLISHED | wc -l"
count = subprocess.call(connection_count, shell=True)

if count > 0:
    connection_lines = "lsof -i tcp | grep ESTABLISHED >> file.txt"

subprocess.call(connection_lines, shell=True)

with open(subprocess.call(xfile, shell=True), "r") as ins:
    array = []
    for line in ins:
        array.append(line)

for i in array:
    print i
6
  • 3
    And what’s the problem? Commented Feb 26, 2015 at 0:36
  • 3
    "using python for bash" is an odd thing to want to get familiar with. If you want to do things for which bash is appropriate doing that in a bash script is a better idea than trying to use python for no real reason. Commented Feb 26, 2015 at 0:36
  • 2
    This sounds like it might be a better fit for codereview.stackexchange.com ... but then again you may not get much help keeping with this direction there as opposed to doing this with less shell and more python instead. Commented Feb 26, 2015 at 0:37
  • @EtanReisner -- Doubt it. The above code isn't working which (IIRC) is a basic requirement for code there. Ultimately, this question would be much better if OP would describe exactly what behavior they were trying to accomplish alongside the code (which is already there) and a description of the error the code is generating (with traceback :-). Unfortunately, the error description is missing (and the desired behavior), which makes it appear like a "how can I make this code better" type of question. Commented Feb 26, 2015 at 0:58
  • @mgilson Given that it isn't working, that's indeed true. I didn't know it wasn't working that wasn't specified (and I didn't read the code closely enough or work with python enough to glean that from the code offhand). Commented Feb 26, 2015 at 2:30

1 Answer 1

2

subprocess.call returns the return code for the process that was started ($? in bash). This is almost certainly not what you want -- and explains why this line almost certainly fails:

with open(subprocess.call(xfile, shell=True), "r") as ins:

(you can't open a number).

Likely, you want to be using subprocess.Popen with stdout=subprocess.PIPE. Then you can read the output from the pipe. e.g. to get the count, you probably want something like:

connection_count = "lsof -i tcp | grep ESTABLISHED"
proc = subprocess.POPEN(connection_count, shell=True, stdout=subprocess.PIPE)
# line counting moved to python :-)
count = sum(1 for unused_line in proc.stdout)

(you could also use Popen.communicate here)

Note, excessive use of shell=True is always a bit scary for me... It's much better to chain your pipes together as demonstrated in the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that makes more sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.