May-29-2020, 05:11 PM
Hi, I'm working in the FreeBSD OS environment and using Python version 3.6.8.
I'm essentially trying to run a script that does the equivalent of the following FreeBSD command:
sysctl -a | grep "group level=\"2\"" -A 1
So, a sysctl command and then piping it to a grep command. Then, I'd like to parse the remaining output (text/string), capture it (in a variable?) and make decisions on it.
Below is my code where the first subprocess.Popen executes the FreeBSD sysctl command, and I pass the output to "p2" and then "grep it" and pass that to p3 and get the expected output text -- see below.
But, I can't save this text/string to a variable so to parse it further.
So, first here is sysctl run at the FreeBSD command line and the associated output...
# sysctl -a | grep "group level=\"2\"" -A 1
<group level="2" cache-level="3">
<cpu count="8" mask="ff,0,0,0">0, 1, 2, 3, 4, 5, 6, 7</cpu>
--
<group level="2" cache-level="3">
<cpu count="8" mask="ff00,0,0,0">8, 9, 10, 11, 12, 13, 14, 15</cpu>
Then, here's my Python code:
and output...
Any thoughts as so what I might be doing wrong?
Thanks!
I'm essentially trying to run a script that does the equivalent of the following FreeBSD command:
sysctl -a | grep "group level=\"2\"" -A 1
So, a sysctl command and then piping it to a grep command. Then, I'd like to parse the remaining output (text/string), capture it (in a variable?) and make decisions on it.
Below is my code where the first subprocess.Popen executes the FreeBSD sysctl command, and I pass the output to "p2" and then "grep it" and pass that to p3 and get the expected output text -- see below.
But, I can't save this text/string to a variable so to parse it further.
So, first here is sysctl run at the FreeBSD command line and the associated output...
# sysctl -a | grep "group level=\"2\"" -A 1
<group level="2" cache-level="3">
<cpu count="8" mask="ff,0,0,0">0, 1, 2, 3, 4, 5, 6, 7</cpu>
--
<group level="2" cache-level="3">
<cpu count="8" mask="ff00,0,0,0">8, 9, 10, 11, 12, 13, 14, 15</cpu>
Then, here's my Python code:
1 2 3 4 5 6 7 8 9 10 11 12 |
def sysctl_command(): p1 = subprocess.Popen([ 'sysctl' , '-a' ], stdout = subprocess.PIPE) p2 = subprocess.Popen([ 'grep' , 'group level="2"' , "-A 1" ], stdin = p1.stdout, stdout = subprocess.PIPE) p3 = subprocess.Popen([ 'grep' , 'cpu count' ], stdin = p2.stdout) p1.stdout.close() p2.stdout.close() output, err = p3.communicate() return output #sysctl_command() out = sysctl_command() print (out) |
Output:# python ./mlnx_tune_freebsd.py
<cpu count="8" mask="ff,0,0,0">0, 1, 2, 3, 4, 5, 6, 7</cpu>
<cpu count="8" mask="ff00,0,0,0">8, 9, 10, 11, 12, 13, 14, 15</cpu>
None
You'll notice that I tried to print 'out' variable and get "None" as opposed to the text string that is displayed to the screen.Any thoughts as so what I might be doing wrong?
Thanks!