introduction to python

17
Introduction to Python for Network Engineer (Basic)

Upload: khnog

Post on 12-Apr-2017

47 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to Python

Introduction to Python for Network Engineer (Basic)

Page 2: Introduction to Python

Before We Start

Do you know these following languages?

´  C/C++

´  Java

´  Shell

´  Batch

´  TCL

´  Python

Page 3: Introduction to Python

Python Overview

´  A script programming language

´  A high-level programming language 

´  object-oriented

´  No compilation

´  No datatype declaration

Page 4: Introduction to Python

What can Python do?

Many many things which Python could do

but the only one thing we will talk about is

“ Network Automation”

Page 5: Introduction to Python

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

>>>

Page 6: Introduction to Python

Running Python: .py file

[porhai@python ~]$ vi khnog.py

print "hello world"

~

~

[porhai@python ~]$ python khnog.py

hello world

[porhai@python ~]$

Page 7: Introduction to Python

Python VS Shell

[porhai@python ~]$ echo Hello World

Hello World

[porhai@python ~]$

>>> print "Hello world"

Hello world

>>>

Shell

Python

Page 8: Introduction to Python

Python VS Shell

[porhai@python ~]$ vi khnog.sh

echo Hello World

~

[porhai@python ~]$ sh khnog.sh

Hello World

[porhai@python ~]$

Shell Python

[porhai@python ~]$ vi khnog.py

print "hello world"

~

[porhai@python ~]$ python khnog.py

hello world

[porhai@python ~]$

Page 9: Introduction to Python

Python Input/Output

>>> x = raw_input("x = ")

x = 123

>>>

>>> print x

123

Page 10: Introduction to Python

Python Datatypes

´  Integer (int)

´  Float (float)

´  String (str)

´  List (list)

´  Dictionary (dict)

>>> x=1 >>> type(x) <type 'int'>

>>> x="hello" >>> type(x) <type 'str'> >>>

>>> x=[1,2,3] >>> type(x) <type 'list'>

>>> x={'name':'porhai', 'age':22} >>> type(x) <type 'dict'>

>>> x=1.1 >>> type(x) <type 'float'>

Page 11: Introduction to Python

Python Modules

´  Telnetlib

´  Paramiko

´  Smtplib

´  PySNMP

Page 12: Introduction to Python

Python Program Example

´  Login to Switch

´  Enter Username and Password

´  Get configuration of interface

´  Output result

Page 13: Introduction to Python

Python Code

[porhai@python ~]$ vi khnog.py

import telnetlib

def open_telnet_conn(ip):

#Change exception message

try:

#Define telnet parameters

username = 'porhai'

password = 'porhai'

TELNET_PORT = 23

#Logging into device

connection = telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT)

Page 14: Introduction to Python

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")

Page 15: Introduction to Python

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

Page 16: Introduction to Python

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/

Page 17: Introduction to Python

Thanks you!!!