0

I'm trying to get the following command to be called from a python script and to get the output of the command: awk -F':' '$1 == "VideoEdge" {print $2, $3, $8}' /etc/shadow

I've got the function working using subprocess.check_output and .Popen in a python shell but when called from a script it doesn't work and causes an exception of which has no apparent output or message.

How can I get this command working from a script?

I've tried using check_output, Popen and shlex to help with possible issues I thought were causing the issue. Code works fine in a shell.

temp = "User"
cmd = "awk -F':' '$1 == \"" + temp + "\" {print $2, $3, $8}' /etc/shadow"
cmdOutput = subprocess.check_output(shlex.split(cmd))
print cmdOutput

temp = "User"
cmd = "awk -F':' '$1 == \"" + temp + "\" {print $2, $3, $8}' /etc/shadow"
cmdOutput = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
print cmdOutput.communicate()[0]
1
  • 1
    That first block runs fine.On the second ,do subprocess.Popen(shlex(cmd), stdin=PIPE, stdout=PIPE, stderr=PIPE) Commented Mar 27, 2019 at 13:55

3 Answers 3

1

I'd just do. [Just for user consideration, (it will be cryptic, in the comments)]

user = "someuser"
with open('/etc/shadow') as f:
    for line in f:
        if line.startswith(user):
            data = line.split(':')
            break
print(data)
Sign up to request clarification or add additional context in comments.

Comments

1

Make the same shlex.split(cmd) and it will work:

cmdOutput = subprocess.Popen(shlex.split(cmd), stdin=PIPE, stdout=PIPE, stderr=PIPE)

2 Comments

Thanks, it works in the python shell but not the script, does subprocess wait for the command to finish? cmdoutput.communicate()[0] is returning an empty value in the script but work as expected in a shell
It did work in the script for me, once I wrapped cmd in shlex in the the Popen() invocation.
0

I had a permission issue with the file i was performing the command. bangs head against desk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.