Python functions for beginners // network engineers
1. Basic Function Creation
def show_connection():
print("Connecting to network device.")
show_connection()
2. Functions with Parameters
Example 1:
def connect_to_device(device_ip):
print(f"Connecting to device {device_ip}.")
connect_to_device("10.10.10.1")
Example 2:
def show_device_info(device_ip, vlan):
print(f"Device {device_ip} is using VLAN {vlan}.")
show_device_info(vlan=10,device_ip="10.10.10.1")
3. Functions with Return Values
def get_http_status_text(http_code):
if http_code == 200:
return "OK"
elif http_code == 404:
return "Not Found"
elif http_code == 500:
return "Internal Server Error"
else:
return "Unknown"
print(get_http_status_text(500))
4. More Examples
Example 1: Using individual variables
device_1 = "SW1"
device_2 = "SW2"
def show_connection(device_name):
print(f"Connecting to {device_name} is in progress.")
print("Connection successful.")
show_connection(device_1)
show_connection(device_2)
# Example 2: Using a list of devices
list_switches = ["SW1", "SW2", "SW3"]
def show_connection(device_name):
print(f"Connecting to {device_name} is in progress.")
print("Connection successful.")
for switch in list_switches:
show_connection(switch)
#python #devnet #devops