Python Forum

Full Version: can't save text string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

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)
and output...
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!
I don't understand why you're not using fileio.
with open('myfilename', 'w') as fp:
    fp.write(...)
Are you piping this to other processes?
Hi Lazr60,

Thank you for your reply and advice. I guess I could pipe the entire output of the FreeBSD command 'sysctl -a' into a file and then parse the file itself. That sounds like a reasonable way to go about it. In my example, I'm basically looking to find out how many NUMA are in the server and how many CPU cores are associated with each NUMA and then use that information ultimately for performance tuning purposes. So, in brief, my script will extract this type of information and then use it for server purpose tuning.

p.s. any idea what I'm doing wrong with my code?

Thanks again!