Lab 11 - Writing Scripts¶
This lab helps make the transition from writing on the Python Interpreter to actually writing Python standalone scripts.
- Lab 11 - Writing Scripts
- Task 1 - Hello Network Automation
- Task 2 - Print Data from a Script
- Conclusion
Task 1 - Hello Network Automation¶
Step 1-1¶
Within your home directory, create a new directory called labs and within it, a sub-directory called python:
Step 1-2¶
Navigate to the python directory.
Step 1-3¶
Create a new file called networkauto.py
Step 1-4¶
Open the file in the text editor of your choice.
- If you used RDP to the jump host, you can use VSCode (shortcut's on the desktop).
- If you used SSH into the jump host, you can use terminal editors like nano or vim.
- An alternative (but recommended, especially for later) option is to set up VSCode with the Remote-SSH extension so you can develop and run your code on the jump-host all in one application.
Note: Your instructor can assist you in setting up with any of these options, please ask for help if you're having any trouble!
Step 1-5¶
Create a Hello World script that just prints Hello Network Automation! - add the following code to the networkauto.py file:
Note: We've also included the optional "shebang" (
#!) that instructs the system which version of Python to use when running this script. That is the first line of the script. You could also write/usr/local/bin/python3.8, but then you'd need to change the line in all of your scripts vs. using your environment should you want to test different versions of Python. Using the recommended shebang, you just need to change the Python version in your environment.
Step 1-6¶
Save the file and execute it from the command line:
As you can see, it was quite simple to write a Python script. It's technically no different from typing on the Python shell.
Task 2 - Print Data from a Script¶
In this task, you will create a script that replicates what you did in previous labs. This script will print the facts for three devices.
Step 2-1¶
Create a new file called print_facts.py in the /home/ntc/labs/python directory.
Step 2-2¶
Take the code below and paste it in print_facts.py. Don't forget to save the file!
#! /usr/bin/env python
import json
facts1 = {'vendor': 'cisco', 'os': 'nxos', 'ipaddr': '10.1.1.1'}
facts2 = {'vendor': 'cisco', 'os': 'ios', 'ipaddr': '10.2.1.1'}
facts3 = {'vendor': 'arista', 'os': 'eos', 'ipaddr': '10.1.1.2'}
devices = [facts1, facts2, facts3]
print(json.dumps(devices, indent=4))
Step 2-3¶
Execute the script.
ntc@ntc-training:python$ python print_facts.py
[
{
"vendor": "cisco",
"os": "nxos",
"ipaddr": "10.1.1.1"
},
{
"vendor": "cisco",
"os": "ios",
"ipaddr": "10.2.1.1"
},
{
"vendor": "arista",
"os": "eos",
"ipaddr": "10.1.1.2"
}
]
Conclusion¶
The point in this lab is to showcase the transition from learning to write code on the Python shell to developing longer scripts in a code editor. The Python interpreter is still one of the best tools for experimentation and trying out code on the fly!