This document introduces Python for network engineers, covering an overview of Python, what tasks it can perform like network automation, how to run Python interactively and with files, differences between Python and shell scripting, Python data types, modules, and includes an example Python program to login to a switch and retrieve interface configuration using Telnet.
2. Before We Start
Do you know these following languages?
´ C/C++
´ Java
´ Shell
´ Batch
´ TCL
´ Python
3. Python Overview
´ A script programming language
´ A high-level programming language
´ object-oriented
´ No compilation
´ No datatype declaration
4. What can Python do?
Many many things which Python could do
but the only one thing we will talk about is
“ Network Automation”
5. Running Python: Interactively
Python is pre-installed on Linux platform
[porhai@python ~]$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print "Hello world"
Hello world
>>>
14. Python Code
connection.write(username + "n")
connection.write(password + "n")
time.sleep(1)
connection.write("show run int g0/1 n")
time.sleep(1)
Output = connection.read_very_eager()
print Output
#Closing the connection
connection.close()
except IOError:
print "Input parameter error! Please check username, password
and file name."
open_telnet_conn("10.1.1.1")
15. Python Program Executed
[porhai@python ~]$ python khnog.py
User Access Verification
Username: porhai
Password:
S1#show run int g0/1
Building configuration...
Current configuration : 66 bytes
!
interface GigabitEthernet0/1
no switchport
no ip address
end
16. Reference
´ Python Language Comparison
https://www.python.org/doc/essays/comparisons/
Shell
´ Python Lesson
http://learnpythonthehardway.org/
´ Python for Network Engineers
https://pynet.twb-tech.com/