Challenge
Challenge¶
Topics: Loops, Dictionaries, Conditionals, Lists, Strings
Task: Create a Python script that takes input from a user and creates a configuration snippet. Optionally connect to a network device and get more information.
Background¶
Your customer has asked you to create a Python script that generates a configuration snippet based on user input. The script should prompt the user for the following information:
- Device hostname
- Device IP address
- Device model
- Access VLAN id
The snippet should either collect interface information using Netmiko or prompt the user for the following information:
- Interface name(s)
The script should then generate a configuration snippet that includes the device hostname, IP address, model, and interface information. The snippet should be printed to the screen.
hostname R1
!
interface Loopback0
ip address 10.9.8.7 255.255.255.255
no shutdown
!
interface GigabitEthernet0/0
switchport mode access
switchport access vlan 20
no shutdown
!
[... more access interfaces ...]
Solution / Inspiration¶
We realise things are getting trickier and hence a suggested implementation is provided already.
We are now combining multiple concepts into a single challenge. This is a common scenario in programming where you need to combine different concepts to solve a problem.
If you have the guts, attempt the challenge on your own. However, if you get stuck or need some guidance or inspiration, a suggested solution is provided below.
You can always improve upon the solution provided with your own ideas with a slight twist of the problem statement.
Click here to see the solution
import netmiko
from getpass import getpass
# Prompt the user for device information
hostname = input("Enter the device hostname: ")
ip = input("Enter the device IP address: ")
# Create a dictionary to store device information
device = {
"hostname": hostname,
"ip": ip,
"model": input("Enter the device model (default: cisco_ios): "),
"vlan": input("Enter the access VLAN ID: "),
}
# If the user does not provide a model, use the default value
if not device.get("model"):
device["model"] = "cisco_ios"
# Let the user provide a comma-separated list of interfaces
interfaces = input("Enter the interface name(s) (comma-separated) (press enter to collect): ")
# If the user does not provide any interfaces, use Netmiko to get more information
if not interfaces:
# Connect to the device and get more information
user = input("Enter your username: ")
password = getpass("Enter your password: ")
handler = netmiko.ConnectHandler(
device_type=device["model"],
username=user,
password=password,
host=device["ip"],
)
output = handler.send_command("show ip interface brief", use_textfsm=True)
interfaces = []
for interface in output:
interface_name = interface["interface"]
interfaces.append(interface_name)
else:
interfaces = interfaces.split(",")
# define a template, we will later use .format() to fill in the values
interface_template = """!
interface {name}
switchport mode access
switchport access vlan {vlan_id}
no shutdown
"""
# Create a configuration snippet
config = f"""
hostname {device['hostname']}
!
interface Loopback0
ip address {device['ip']} 255.255.255.255
no shutdown
!
"""
for interface_name in interfaces:
# Create an interface configuration snippet using the template
interface_config = interface_template.format(name = interface_name, vlan_id=device["vlan"])
# extend the configuration snippet with interface information
config += interface_config
print(config)