Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't save text string
#1
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!
Reply
#2
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?
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,121 Jul-07-2023, 05:44 PM
Last Post: flash77
  Editing text between two string from different lines Paqqno 1 1,287 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  Extract a string between 2 words from a text file OscarBoots 2 1,827 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Replace String in multiple text-files [SOLVED] AlphaInc 5 7,963 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  fuzzywuzzy search string in text file marfer 9 4,433 Aug-03-2021, 02:41 AM
Last Post: deanhystad
Question How to extract multiple text from a string? chatguy 2 2,315 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Searching string in file and save next line dani8586 2 2,250 Jul-10-2020, 09:03 AM
Last Post: dani8586
  the uncompleteness of the text string Cava 2 2,137 Feb-21-2020, 03:54 PM
Last Post: Cava

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020