To Python Network Programming For Network Architects and Engineers
To Python Network Programming For Network Architects and Engineers
To Python Network Programming For Network Architects and Engineers
to
Python Network Programming
for
Network Architects and Engineers
Vince Kelly
CCIE emeritus
CISSP
Introduction to Python Network Programming for Network Architects and Engineers
Part III Python Programming for Cisco ACI and UCS Environments
- ACI and UCS Object model review
- Leveraging Python REST API to manage/manipulate ACI and UCS
- Understanding ACI and UCS data structures; “packages”, “classes”, “objects”
- Python code generator for ACI
- “Is there an easier way to do this?”
o ACI toolkit and ACI SDK
Introduction to Python Network Programming for Network Architects and Engineers
1.How to take input from the command line or from the keyboard
2.Leveraging socket library to retrieve the IP address of a device
3.Save those names & addresses to disk
4.Simple UDP based client/server
Introduction to Python Network Programming for Network Architects and Engineers
Code Templates
1 template1.py Examples of printing, escape sequence and using ANSI text color colorama module
3 template3.py Example of functions & using basic Python socket library to retrieve local machine IP information
4 template4.py Examples of combining cmd line arguments and passing arguments to functions
https://cisco.box.com/v/ciscoLivePythonCode
Introduction to Python Network Programming for Network Architects and Engineers
2 13 min Python Implementation High level Python implementation differences, Cpython, Jython, IronPython, etc.
3 20 min Python 2.7 vs. Python 3.5 High level discussion on differences between Python2.7 vs. Python3.5 and rationale for installing them
4 22 min Python Installation Installing Python 2.7.12 Example of cutting & pasting to Windows7 clipboard using ‘pyperclip.py’ module
5 17 min Python Post Install 1 Downloading Python modules from PyPi and incorporating them in programs
6 12 min Python Post Install 2 Isolating Python modules and code in the development environment using virturalenv
7 9 min Python Post Install 3 Installing Python 3.5.3, running Python 2.7 and 3.5 together
8 10 min Python Post Install 4 Isolating Python 3.5 development environment. Downloading Pylint, (Python error & syntax checker)
https://cisco.box.com/v/ciscoLivePythonVideos
Introduction to Python Network Programming for Network Architects and Engineers
CONCATENATION
print "Quote's can have LOT's of Quote's!" Just need to be consistent
print "Hello World using 'old Python' !" # Python2.7 and below
print ("Hello World using ‘new Python' !“) # Python3.x
print variable1 + variable2 + str(variable3) # same thing just forcing it all to be a string
Introduction to Python Network Programming for Network Architects and Engineers
SUBSTITUTION
….directly INSIDE this string?
variable1 = "This is a 'sequence’ with a value of " # data type will be string
ESCAPE SEQUENCES
Tells Python how to do special things
https://docs.python.org/2/reference/lexical_analysis.html
Introduction to Python Network Programming for Network Architects and Engineers
print "printing new line:" # print statement Python 2.7 and below
print "hello world\n there“ # newline character anywhere inside a string
print '\n‘ # or by itself
print ("printing back spaces:") # print function Python 2.7 or 3.x and above
print ("hello world\b\b\b\b\bthere !") # backspace character – try it and see what happens
print '\n'
IMPORTING MODULES
Introduction to Python Network Programming for Network Architects and Engineers
Python Interpreter
How do you import Python modules?
import sys
Source Code
Python
Program_xyz.py
Interpreter
Operating System
Introduction to Python Network Programming for Network Architects and Engineers
sys.py
screen
argv = [ ‘Program_xyz.py’, ‘www.cisco.com’, ‘192.168.10.1’ ]
‘Program_xyz.py’
‘www.cisco.com’
‘192.168.10.1’
import sys
print sys.argv[0]
print sys.argv[1]
print sys.argv[2]
Program_xyz.py
Python Interpreter
Introduction to Python Network Programming for Network Architects and Engineers
TRY IT YOURSELF!
ANSI Color Codes
(bottom of template1.py)
Introduction to Python Network Programming for Network Architects and Engineers
Python Interpreter
PyPi.python.org
How do you download Python modules? colorama.py
(ANSI color)
colorama.py
(ANSI color) PIP !
import colorama
Source Code
Python
Program_xyz.py
Interpreter
Operating System
colorama.py
def class colorama(self):
def init():
do some stuff with colors
return
import colorama
colorama.init()
Program_xyz.py
Python Interpreter
Introduction to Python Network Programming for Network Architects and Engineers
colorama.init()
print ("printing color red in bold:")
print"\n“ \033[1;31m
separate
boldRed = "\033[1;31m" #
attributes
end = "\033[0;0m" # restore default color
with a ;
print "this is: " + boldRed + "the color " + end
print "\n"
\033[0;0m
Text Text
style color
Introduction to Python Network Programming for Network Architects and Engineers
Python Interpreter
How do you import Python networking modules?
import socket
Source Code
Python
Program_xyz.py
Interpreter
Operating System
NIC
Introduction to Python Network Programming for Network Architects and Engineers
Firefox
import socket UCSM APIC
Program_xyz.py Browser
TCP UDP
IP
Device Drivers
LAYER 2 PROTOCOL STACK
Framer/
Protocol
decode
B
U
Tx S Rx
Buffers Buffers
LAYER 1 NIC
Introduction to Python Network Programming for Network Architects and Engineers
Functions: Small blocks of code that don’t get executed until they are called
## Template 3
# Examples of using functions and basic Python socket library
# this just prints out the local machine NIC information
#
Functions: Small blocks of code that don’t get executed until they are called
#Template 4
# combines command line, functions
FILES
(template5.py)
Introduction to Python Network Programming for Network Architects and Engineers
Mode Description
“r” Read fro a txt file. Error if file not found
“w” Write to a file. If it already exists, it will be overwritten, if not it will be created
“a” Append to a file if it already exists, if not it will be created
“r+” Read and Write to a file. Created if it doesn’t exist, data is appended if it does
“w+” Write to and Read from a file. If it exists, its overwritten, if not its created
“a+” Append and Read from a file. Append if the file exists, create file if it dosen’t
fp = open(“filename”, “w”)
strng = “this is 1 string” a CR\LF will NOT be automatically added
fp.write(strng)
lines = [“Line 1\n”, Line 2 \n, Line 3 \n”]
fp.writelines(lines)
fp.close()
Introduction to Python Network Programming for Network Architects and Engineers
dir(fp) returns all of the methods that can be used by fp (e.g., ‘seek’)
help(fp.seek) print doc on the seek command
Introduction to Python Network Programming for Network Architects and Engineers
#Template 5
# piggyback on templete4 and write info retrieved to disk
# 'appending' to a simple text logfile
#
def get_machine_info(host_name):
# host_name = socket.gethostname()
# host_name = "www.google.com"
ip_address = socket.gethostbyname(host_name)
print "host name is: \t\t\t %s " % host_name
print "This machines IP address is: \t %s" % ip_address
return host_name, ip_address
###########################
# Wirte output to disk log file
fp = open("ip_addresses.txt","a")
fp.write(output)
fp.close
Introduction to Python Network Programming for Network Architects and Engineers
THANK YOU