Automation

last person joined: 3 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Extract Mac Address of JUNOS device

    Posted 02-11-2019 21:38

    Hi Experts,

    Previously i have asked questions to extract some info from JUNOS and it works. I just found that the value that i extract is not the one the i require and some more it have missing character

     

    I used the command below to extract mac address 

    show chassis mac-addresses | match "Private base"

    and return as follows

     Private base address    00:1c:95:b9:e7:f0
    
    {master}

     In python code below

    output_mac = connection.send_command('show chassis mac-addresses | match 
    "Private base"')
    
    print("router_mac1: " + re.sub('\{|master|\:|0|\}|\\n','',output_mac))

    from the print output result below

    router_mac:   Private base address    1db5d9e7f

    Appreciate if someone could advise me on how i can get the correct mac address 00:1c:95:b9:e7:f0 

    I have read some example of using regex but i dont know exactly how to do it and apply onto the code above. I really appreciate if someone could help me on this. I would like to say thanks and may god bless you.



  • 2.  RE: Extract Mac Address of JUNOS device
    Best Answer

    Posted 02-12-2019 01:35

    Hi,

     

    Yor are actualy using regex , re in the python is for the regext. Ty removing ":" from your print and it should give you the mac address with the : .

     

    I tried and it worked, see below :

     

    >>> x = " Private base address 00:1c:95:b9:e7:f0"


    >>> print("router_mac1: " + re.sub('\{|master|\:|0|\}|\\n','',x))
    router_mac1: Private base address 1c95b9e7f    <-- it's without : when I use :


    >>> print("router_mac1: " + re.sub('\{|master|\|0|\}|\\n','',x))
    router_mac1: Private base address 00:1c:95:b9:e7:f0  <-- with : when I dont use : in the regex match.

     

    Thanks,

    Vikas



  • 3.  RE: Extract Mac Address of JUNOS device

    Posted 02-12-2019 18:58

    Hi,

    It work sir. Thanks

    I have seen there is a regex for mac address such as 

    ^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$

    Can use this regex onto the code? how to use it...thanks



  • 4.  RE: Extract Mac Address of JUNOS device

    Posted 02-12-2019 22:24

    Hi,

     

    Either matches give me the same result, see below :

     

    x = " Private base address 00:1c:95:b9:e7:f0"

     

    print("router_mac1: " + re.sub('^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$','',x))  <-- Try removing $ if mac is not in the first line of the output.


    router_mac1: Private base address 00:1c:95:b9:e7:f0
    >>> print("router_mac1: " + re.sub('\{|master|\|0|\}|\\n','',x))
    router_mac1: Private base address 00:1c:95:b9:e7:f0

     

    For more details, you can use pythons help feature to see what fuction you can use based on their descriptions and how to match regex etc.

     

    >>> import re
    >>> help(re)

     

    Help on module re:

    NAME
    re - Support for regular expressions (RE).

    FILE
    /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py

    MODULE DOCS
    http://docs.python.org/library/re

    DESCRIPTION
    This module provides regular expression matching operations similar to
    those found in Perl. It supports both 8-bit and Unicode strings; both
    the pattern and the strings being processed can contain null bytes and
    characters outside the US ASCII range.

    Regular expressions can contain both special and ordinary characters.
    Most ordinary characters, like "A", "a", or "0", are the simplest
    regular expressions; they simply match themselves. You can
    concatenate ordinary characters, so last matches the string 'last'.

    The special characters are:
    "." Matches any character except a newline.
    "^" Matches the start of the string.
    "$" Matches the end of the string or just before the newline at
    the end of the string.
    "*" Matches 0 or more (greedy) repetitions of the preceding RE.
    Greedy means that it will match as many repetitions as possible.
    "+" Matches 1 or more (greedy) repetitions of the preceding RE.
    "?" Matches 0 or 1 (greedy) of the preceding RE.
    *?,+?,?? Non-greedy versions of the previous three special characters.
    {m,n} Matches from m to n repetitions of the preceding RE.
    {m,n}? Non-greedy version of the above.
    "\\" Either escapes special characters or signals a special sequence.
    [] Indicates a set of characters.
    A "^" as the first character indicates a complementing set.
    "|" A|B, creates an RE that will match either A or B.
    (...) Matches the RE inside the parentheses.
    The contents can be retrieved or matched later in the string.

     

    sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl. repl can be either a string or a callable;
    if a string, backslash escapes in it are processed. If it is
    a callable, it's passed the match object and must return
    a replacement string to be used.

     

    THanks,

    Vikas

     

     



  • 5.  RE: Extract Mac Address of JUNOS device

    Posted 02-13-2019 17:49

    Noted and thank you so much sir. I really appreciated your support and attention to my inquiry.

    Thanks again.