-1

I need to filter the output of a command executed in network equipment in order to bring only the lines that match a text like '10.13.32.34'. I created a python code that brings all the output of the command but I need only part of this.

I am using Python 3.7.3 running on a Windows 10 Pro.

The code I used is below and I need the filtering part because I am a network engineer without the basic notion of python programming. (till now...)

from steelscript.steelhead.core import steelhead
from steelscript.common.service import UserAuth
auth = UserAuth(username='admin', password='password')
sh = steelhead.SteelHead(host='em01r001', auth=auth)
from steelscript.cmdline.cli import CLIMode
sh.cli.exec_command("show connections optimized", mode=CLIMode.CONFIG)
output = (sh.cli.exec_command("show connections optimized"))
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

I have no idea what your output looks like, so used the text in your question as example data. Anyway, for a simple pattern such as what's shown in your question, you could do it like this:

output = '''\
I need to filter the output of a command executed
in network equipment in order to bring only the
lines that match a text like '10.13.32.34'. I
created a python code that brings all the output
of the command but I need only part of this.

I am using Python 3.7.3 running on a Windows 10
Pro.

The code I used is below and I need the filtering
part because I am a network engineer without the
basic notion of python programming. (till now...)
'''

# Filter the lines of text in output.
filtered = ''.join(line for line in output.splitlines()
                            if '10.13.32.34' in line)

print(filtered)  # -> lines that match a text like '10.13.32.34'. I

You could do something similar for more complex patterns by using the re.search() function in Python's built-in regular expression re module. Using regular expressions is more complicated, but extremely powerful. There are many tutorials on using them, including one in Python's own documentation titled the Regular Expression HOWTO.

martineau
  • 119,623
  • 25
  • 170
  • 301