11.5. A few Other uncategorized mixed functions

11.5.1. @decorator: printmsg

  • Use it as decorator to print some messages before and after a function call.

  • keys to decorator
    1. pre: (default: None) - message to be printed before function call( default min message length = 80 )

    2. post: (default: None) - message to be printed after function call

    3. pre_ends: (default: n) - default message end character after pre message finish.

>>> from nettoolkit.nettoolkit_common import printmsg
>>> @printmsg(pre="Some Pre Message" ,pre_ends="\t", post="Some Post Message")
def SomeFunction():
    print("Function Message")

>>> SomeFunction()
Some Pre Message                                                                        Function Message
Some Post Message

11.5.2. remove_domain()

  • removes domain entry from fqdn entry

>>> from nettoolkit.nettoolkit_common import remove_domain
>>> remove_domain("hostname.domain.com")
"hostname"

11.5.3. read_file()

  • reads the provided text file and retuns output in list format

>>> from nettoolkit.nettoolkit_common import read_file
>>> read_file("sometextfile.txt")
['list of lines from text file',...]

11.5.4. standardize_mac()

  • removes . or : from mac address

>>> from nettoolkit.nettoolkit_common import standardize_mac
>>> standardize_mac("aa:bb:cc:dd:ee:ff")
"aabbccddeeff"

11.5.5. mac_2digit_separated()

  • converts input mac to 2 digit separated mac format, separator=`:`

>>> from nettoolkit.nettoolkit_common import mac_2digit_separated
>>> mac_2digit_separated("aabb.ccdd.eeff")
"aa:bb:cc:dd:ee:ff"

11.5.6. mac_4digit_separated()

  • converts input mac to 4 digit separated mac format, separator=`.`

>>> from nettoolkit.nettoolkit_common import mac_4digit_separated
>>> mac_4digit_separated("aa:bb:cc:dd:ee:ff")
"aabb.ccdd.eeff"

11.5.7. create_folders()

  • Creates Folders

  • argument (folders) - can be string or list of strings.

  • argument (silent) - boolean to display messages or not.

  • returns if success or not

>>> from nettoolkit.nettoolkit_common import create_folders
>>> create_folders([path1, path2], silent=True)

11.5.8. standardize_if()

  • standardizes interface naming

  • expand operator will make it full length name. Default is False.

>>> from nettoolkit.nettoolkit_common import standardize_if
>>> standardize_if("FastEth1/0")
"Fa1/0"
>>> standardize_if("FastEth1/0", expand=True)
"FastEthernet1/0"

11.5.9. get_file_path

  • returns Path object of provided file string.

from nettoolkit.nettoolkit_common import get_file_path
get_file_path('c:/users/ali/documents/change_batch_1.xlsx')
WindowsPath('c:/users/ali/documents')

11.5.10. get_file_name

  • returns filename of provided file string.

  • provide ext argument as True to get extension as well.

from nettoolkit.nettoolkit_common import get_file_name
get_file_name('c:/users/al202t/documents/change_batch_1.xlsx')
'change_batch_1'
get_file_name('c:/users/al202t/documents/change_batch_1.xlsx', ext=True)
'change_batch_1.xlsx'

11.5.11. create_folders

  • Create the folders dynamically via script.

  • provide list of folders create all folders.

  • provide silent=False, to display the activity. (default is True)

from nettoolkit.nettoolkit_common import create_folders
import os
os.listdir('c:/users/ali/documents/')
['A T M', 'change_batch_1.xlsx', 'change_batch_2.xlsx',]

folders_to_create = ['c:/users/ali/documents/test1', 'c:/users/ali/documents/test2']

create_folders(folders_to_create)
OK.
OK.
True
os.listdir('c:/users/ali/documents/')
['A T M', 'change_batch_1.xlsx', 'change_batch_2.xlsx', 'test1', 'test2']

folders_to_create = ['c:/users/ali/documents/test3', 'c:/users/ali/documents/test4']

create_folders(folders_to_create, silent=False)
Creating: c:/users/ali/documents/test3  OK.
Creating: c:/users/ali/documents/test4  OK.
True
os.listdir('c:/users/ali/documents/')
['A T M', 'change_batch_1.xlsx', 'change_batch_2.xlsx', 'test1', 'test2', 'test3', 'test4']

11.5.12. detect_device_type

  • detect the device type from the output configuration list

  • provide list as argument to function

  • ‘Unidentified’ will result if device type is not detected.

from nettoolkit.nettoolkit_common import detect_device_type

file = "c:/users/ali/Desktop/some_cisco_output.log"
with open(file, 'r' ) as f:
    lines = f.readlines()
    detect_device_type(lines)

'Cisco'


file = "c:/users/ali/Desktop/some_juniper_output.log"
with open(file, 'r' ) as f:
    lines = f.readlines()
    detect_device_type(lines)

'Juniper'

11.5.13. CapturesOut

  • CapturesOut is a class providing multiple usefull methods and properties on a device commands capture output

  • provide the device log file as input to class

  • see some of properties and its method listed below.

  • There are many more, explore and identify more in detail.

from nettoolkit.nettoolkit_common import CapturesOut

file1 = "c:/users/ali/Desktop/hostname-a.log"

CO = CapturesOut(file1)
CO.name
'hostname-a'

CO.device_type
'Juniper'

CO.has("show version")
True
CO.cmd_output("show version")
['#================================================================================', '', 'fpc0:', '--------------------------------------------------------------------------',  'Model: qfx5100-48s-6q', ...output trunked...]


file2 = "c:/users/ali/Desktop/hostname-b.log"
CO = CapturesOut(file2)
CO.name
'hostname-b'

CO.device_type
'Cisco'

CO.has("show bgp summary")
False
CO.has("show version")
True
CO.has("sh ver")              ## Trunked commands also will accpet
True

CO.cmd_output("show version")
['!================================================================================', '', 'Cisco IOS XE Software, Version 17.06.04', 'Cisco IOS Software [Bengaluru], ...output trunked... ]

11.5.14. add_blankdict_key

  • Add a blank dictionary with provided key to provided dictionary.

from nettoolkit.nettoolkit_common import add_blankdict_key

d = {'interfaces': {}}
add_blankdict_key(d['interfaces'], 'Fa0/0')
print(d)
{'interfaces': {'Fa0/0': {}}}

11.5.15. add_blankset_key

  • Add a blank set with provided key to provided dictionary.

from nettoolkit.nettoolkit_common import add_blankset_key

tacacs = {}
add_blankset_key(tacacs, 'servers')
print(tacacs)
{'servers': set()}

11.5.16. add_blanklist_key

  • Add a blank list with provided key to provided dictionary.

from nettoolkit.nettoolkit_common import add_blanklist_key

tacacs = {}
add_blanklist_key(tacacs, 'servers')
print(tacacs)
{'servers': []}

11.5.17. add_blanktuple_key

  • Add a blank tuple with provided key to provided dictionary.

from nettoolkit.nettoolkit_common import add_blanktuple_key

tacacs = {}
add_blanktuple_key(tacacs, 'servers')
print(tacacs)
{'servers': ()}

11.5.18. add_blanknone_key

  • Add a blank None with provided key to provided dictionary.

from nettoolkit.nettoolkit_common import add_blanknone_key

intf_dict = {}
add_blanknone_key(intf_dict, 'management')
print(intf_dict)
{'management': None}

11.5.19. update_key_value

  • updates a key value pair to given dictionary only if the key is not exist.

  • syntex : update_key_value(dic, key, value)

from nettoolkit.nettoolkit_common import update_key_value

tacacs = {}
update_key_value(tacacs, 'server', '10.10.10.1')
print(tacacs)
{'server': '10.10.10.1'}
update_key_value(tacacs, 'server', '10.10.10.5')
print(tacacs)
{'server': '10.10.10.1'}

11.5.20. get_vlans_cisco

  • get the vlan information from provided cisco input

from nettoolkit.nettoolkit_common import get_vlans_cisco

get_vlans_cisco("switchport trunk allowed vlan 10,20,30-34")
{'vlan_members': '10,20,30-34', 'access_vlan': None, 'voice_vlan': None, 'native_vlan': None}
get_vlans_cisco("switchport access vlan 201")
{'vlan_members': set(), 'access_vlan': '201', 'voice_vlan': None, 'native_vlan': None}
get_vlans_cisco("switchport voice vlan 201")
{'vlan_members': set(), 'access_vlan': None, 'voice_vlan': '201', 'native_vlan': None}

11.5.21. trunk_vlans_cisco

  • get the trunk vlans from provided cisco input

from nettoolkit.nettoolkit_common import trunk_vlans_cisco

trunk_vlans_cisco("switchport trunk allowed vlan 10,20,30-34")
{32, '10', 33, 34, '20', 30, 31}

11.5.22. get_vrf_cisco

  • get the vrf name from provided cisco input

from nettoolkit.nettoolkit_common import get_vrf_cisco

get_vrf_cisco(" vrf forwarding 100")
'100'