Professional Documents
Culture Documents
NETWORK ENGINEERS
PRESENTED BY
TOLGA KOCA
TRAINER:
TOLGA KOCA
Network Engineer
- Huawei (7 years)
- Customer Support for Turkcell (5 years)
- İşNet (1 Month)
1- Python Basics
2- Connect Network Devices
3- Log Management
CONTENT
● Python Introduction
● Data Types (String, List, Dictionary)
● Conditions (If, For)
● File Handling
● Re Module
● Device Connections
● Log Management
PYTHON BASICS
Presented content overview
Why Python?
Open Source
Interpreted
Object-oriented
Easy Syntax
High-level
Good Community
Dynamic Semantics
Platform Independent
Why we need Python For Network Engineering?
IF condition :
Do this
a = 33
b = 200
if b > a:
print("b is greater than a")
Python – If Statements
IF condition : a = 200
Do this b = 33
ELIF condition : if b > a:
Do this print("b is greater than a")
ELIF condition : elif a == b:
Do this print("a and b are equal")
... else:
ELSE : print("a is greater than b")
Do this
Python – For Loop Statements
Output: apple
banana
cherry
Python – For Loop Statements
import re
re.findall ( Find the characters , string_name )
import re
my_string = "He finally went to the school at the end of
the year."
x = re.findall ( "he" , my_string )
print(x) Output: ['he', 'he', 'he']
RE Module – Split ()
import re
re.split ( Find the characters , string_name , (optional) Number of times )
import re import re
my_string= "The rain in Spain" my_string= "The rain in Spain"
x = re.split( "i", my_string) x = re.split ( "i", my_string, 2 )
print(x) print(x)
Output: ['The ra', 'n ', 'n Spa', 'n'] Output: ['The ra', 'n ', 'n Spain']
RE Module - Special Sequences
\A Returns a match if the specified characters are at the beginning of the string
\b Returns a match where the specified characters are at the beginning or at the end of a word
\B Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a
word
\d Returns a match where the string contains digits (numbers from 0-9)
\S Returns a match where the string DOES NOT contain a white space character
\w Returns a match where the string contains any word characters ( character from a to Z, from 0-9, and the
underscore _ )
\W Returns a match where the string DOES NOT contain any word characters
\Z Returns a match if the specified characters are at the end of the string
RE Module - Special Sequences
import re import re
str = "+44 (231) 231 12" my_string = "I’m learning python"
x = re.findall("\d", str) x = re.findall("\w", my_string)
print(x) print(x)
Output: ['4', '4', '2', '3', '1', '2', '3', '1', '1', Output: ['I', 'm', 'l', 'e', 'a', 'r', 'n', 'i', 'n', 'g',
'2'] 'p', 'y', 't', 'h', 'o', 'n']
RE Module - Special Sequences
Output: ['Ea']
x = re.findall("E\D+", my_string)
print(x)
Output: ['Eastwood']
PYTHON IN NETWORK
Ping Test
import subprocess
reached = []
not_reached = []
hosts = ["192.168.1.1","123.214.2.2","www.google.com"]
for ip in hosts:
ping_test = subprocess.call('ping %s -n 2' % ip)
if ping_test == 0:
reached.append(ip)
else:
not_reached.append(ip)
print("{} is reachable".format(reached))
print("{} not reachable".format(not_reached))
Paramiko - Collect Logs From Multiple Devices
import paramiko
import time
c = open("Command_List.txt", "r")
command_list = c.read().split("\n")
d = open("Device_List.txt", "r")
hosts = d.read().split("\n")
port = 22
username = "root"
password = "Test1234."
logs = []
logs1= []
Paramiko - Collect Logs From Multiple Devices
for ip in hosts:
print("Try to login:", ip)
conn = paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn.connect(ip, port, username, password)
comm = conn.invoke_shell()
logs = output.split("xxxxx")
print("logs",logs)
logs1.extend(logs)
logs1 = ''.join(map(str, logs1))
with open("{}.txt".format(ip), "w") as f:
f.write(logs1)
logs1 = []
Netmiko- Collect Logs From One Device
logger = logging.getLogger("netmiko")
host = {
"host": "129.9.0.101",
"username": "root",
"password": "Test1234.",
"device_type": "huawei",
"global_delay_factor": 0.1,
}
Netmiko- Collect Logs From One Device
net_connect = Netmiko(**host)
print("Connected to:", net_connect.find_prompt())
output = net_connect.send_config_set(command, delay_factor=.5)
net_connect.disconnect()
print(output)
LOG MANAGEMENT
Configuration Change in File
x = x.replace("user-interface con 0\n authentication-mode password", " user-interface con 0\n authentication-
mode aaa")
x = x.replace("interface GigabitEthernet0/0/0", " interface GigabitEthernet0/0/0\nip address 1.1.1.1 24")
version = re.findall("(V\w+)\)",x)
soft_version.append(version[0])
print(soft_version)
Collect Device Name and Version
import re
from pandas import DataFrame
switch_name = []
device_model = []
soft_version = []
hosts = ["R1.txt","R2.txt","R3.txt","R4.txt"]
for i in hosts:
with open(i) as switches: #Open text file as switches
x = switches.read()
names = re.findall("\[(.*)\]",x)
switch_name.append(names[0])
model = re.findall("Version 5.130 \((\w+)",x)
device_model.append(model[0])
version = re.findall("(V\w+)\)", x)
soft_version.append(version[0])
print(switch_name)
print(device_model)
print(soft_version)
Collect Device Name and Version
df = DataFrame({'Device Name' : switch_name, 'Device Model' : device_model,
'Software Version': soft_version})
print(df)
df.to_excel("result.xlsx")
Precheck & Postcheck Compare
for i in range(6):
if precheck[i] == postcheck[i]:
result.append("OK")
else:
result.append("NOK")
Udemy – Ansible Training
Thank you
Tolga Koca
koca.tolga@gmail.com
HUAWEI ENTERPRISE SUPPORT COMMUNITY
a) x= x.replace ("_","")
b) x= x.replace ("_"," ")
c) x= x.split ("_"," ")
d) x= x.add ("_"," ")
QUESTION 3.
a) x = re.search ("\d+", x)
b) x = re.findall ("\d", x)
c) x = re.findall ("\d+", x)
d) x = re.findall ("\D", x)