Automation Lab Setup
Automation Lab Setup
Python 3
(CCNA 200-301)
Lab Setup
⮚ cd C:\Program Files\GNS3
⮚ Loopback-manager.cmd
Rename the newly installed loopback interface (You can choose any name)
Check on Show Special Ethernet Interfaces and Select LoopBack Interface then Add
(config-if)#no shR1utdown
R1#ping 10.10.10.5
R1(config)#line vty 0 4
R1(config-line)#login local
Learn Variables:
Python is completely object oriented, and not "statically typed". You do not need to declare
variables before using them, or declare their type. Every variable in Python is an object.
Variables are just names which assign a value.
This tutorial will go over a few basic types of variables.
For example: you are working on a company. There are so many employee in the company.
Emp = Employee
In Py Shell Type:
>>> Emp1
'John'
Here Emp1 is variable and John is value
>>> Emp1
'John'
>>> Emp2
'Paul'
>>> Emp3
'July'
>>> Emp1
'John'
>>> Emp2
'Paul'
>>> Emp3
'July'
>>> Emp4
'Ruby'
>>> Emp5
'Sharkar'
>>> Emp6
'Shoumy'
>>> Emp5
'Mahmud'
>>> Emp6
'Mahmud'
>>> Emp7
'Mahmud'
Learn Print:
The print function in Python is a function that outputs to your console window whatever you say
you want to print out.
At first blush, it might appear that the print function is rather useless for programming, but it is
actually one of the most widely used functions in all of python. The reason for this is that it makes
for a great debugging tool.
"Debugging" is the term given to the act of finding, removing, and fixing errors and mistakes within
code. If something isn't acting right, you can use the print function to print out what is happening in
the program. Print is a built-in function in Python.
>>> print ('Hello Friends')
Hello Friends
>>> print(Emp3)
July
>>> print(Emp3,Emp5,Emp1)
Integer:
>>> age = 50
>>> type(age)
<class 'int'>
>>> age = 50
>>> type(age)
<class 'int'>
>>> age1 = 60
>>> age2 = 90
>>> age1-age2
-30
>>> age2-age1
30
>>> age1+age2
150
>>> age1*age2
5400
>>> age1/age2
0.6666666666666666
>>> age1%age2
60
String:
String will always be in Quotation Mark. That can be single or double quotation.
>>> Name="Kakoli"
>>> type(Name)
<class 'str'>
>>>
>>> FirstName= "Quazi"
>>> MiddleName= "Mahmudul"
>>> LastName= "Huq"
>>> FullName= FirstName +MiddleName +LastName
>>> FullName
'QuaziMahmudulHuq'
>>> FullName= FirstName+" "+MiddleName+" "+LastName
>>> FullName
'Quazi Mahmudul Huq'
>>> FullName
'Quazi Mahmudul Huq'
We want to find 6th Character of given string of FullName (Note: Space is also a value here)
>>> FullName[6]
'M'
We want to find 6th Character of given string of FullName
>>> FullName[0]
'Q'
List
A list is a collection which is ordered and changeable. Allows duplicate members. In Python lists are
written with square brackets.
>>> list=["Cat","Dog","Cow","Goat"]
>>> type(list)
0: <class 'list'>
List Serial No. start with 0
>>> list[0]
1: 'Cat'
>>> list[1]
2: 'Dog'
>>> list[2]
3: 'Cow'
>>> list[3]
4: 'Goat'
>>> list
5: ['Cat', 'Dog', 'Cow', 'Goat']
>>> num1=[10, 20, 30]
>>> num2=[40, 50]
>>> num3=num1+num2
>>> num3
6: [10, 20, 30, 40, 50]
>>> type(num3)
7: <class 'list'>
Delete from list:
>>> del list[1]
>>> list
8: ['Cat', 'Cow', 'Goat']
Add to list:
>>> list.append("Dog")
>>> list
9: ['Cat', 'Cow', 'Goat', 'Dog']
Check Length of List:
>>> len(list)
10: 4
You can Add Same count Multiple Times:
>>> list.append("Cat")
>>> list
11: ['Cat', 'Cow', 'Goat', 'Dog', 'Cat']
Check How many times “Cat” is in the list.
>>> list.count("Cat")
12: 2
Add character in num3:
>>> num3.append(-20)
>>> num3
13: [10, 20, 30, 40, 50, -20]
Check the maximum number in num3 list:
>>> max(num3)
14: 50
Check the minimum number in num3 list:
>>> min(num3)
15: -20
Remove a number from num3 list:
>>> num3.remove(40)
>>> num3
16: [10, 20, 30, 50, -20]
Reverse order of list num3:
>>> num3.reverse()
>>> num3
18: [-20, 50, 30, 20, 10]
Sort list in ascending order:
>>> num3.sort()
>>> num3
19: [-20, 10, 20, 30, 50]
Dictionary
Python dictionary is an unordered collection of items. Each item of a dictionary has
a key/value pair.
"john":10 = Here john is key and 10 is value
Dictionaries are optimized to retrieve values when the key is known.
Tuple
A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use
parentheses, whereas lists use square brackets.
>>> tup1=("Jannat",10,20,30,"Rakhi")
>>> tup1
0: ('Jannat', 10, 20, 30, 'Rakhi')
>>> type(tup1)
1: <class 'tuple'>
>>> tup2=(10,20,30,40,50)
>>> tup2
2: (10, 20, 30, 40, 50)
Count, how many times 20 have in tup2:
>>> tup2.count(20)
3: 1
Slicing the tuple:
>>> tup2[0:3]
4: (10, 20, 30)
Check Serial Number (index) of a value of tup2:
>>> tup2.index(40)
5: 3
We cannot update a tuple, we can delete a tuple:
>>> del tup1
>>> tup1
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
tup1
NameError: name 'tup1' is not defined
>>> tup2
6: (10, 20, 30, 40, 50)
Python Scripting
Modules in Python are simply Python files with a .py extension. The name of the module will be the
name of the file. A Python module can have a set of functions, classes or variables defined and
implemented. We have many built-in modules in Python.
import getpass
import sys
import telnetlib
HOST = "10.10.10.6"
user = input("Enter your telnet username: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"enable\n")
tn.write(b"1234\n")
tn.write(b"conf t\n")
tn.write(b"hostname CCNA\n")
tn.write(b"int loop 0\n")
tn.write(b"ip address 1.1.1.1 255.255.255.255\n")
tn.write(b"int loop 1\n")
tn.write(b"ip address 2.2.2.2 255.255.255.255\n")
tn.write(b"router ospf 10\n")
tn.write(b"network 0.0.0.0 255.255.255.255 area 0\n")
tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
R1>enable
Password:
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#hostname CCNA
CCNA(config)#int loop 0
CCNA(config-if)#ip address 1.1.1.1 255.255.255.255
CCNA(config-if)#int loop 1
CCNA(config-if)#ip address 2.2.2.2 255.255.255.255
CCNA(config-if)#router ospf 10
CCNA(config-router)#network 0.0.0.0 255.255.255.255 area 0
CCNA(config-router)#end
CCNA#exit
Router Configuration:
R1#configure terminal
R1(config)#enable password 1234
R1(config)#username admin password 1234
R1(config)#line vty 0 4
R1(config-line)#login local
R1(config-line)#transport input telnet
R1(config-line)#exit
R1(config)#interface fastEthernet 0/1
R1(config-if)#ip address 10.10.10.6 255.255.255.0
R1(config-if)#no shutdown
R1#ping 10.10.10.5
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.10.10.5, timeout is 2 seconds:
.!!!!
Success rate is 80 percent (4/5), round-trip min/avg/max = 40/46/52 ms
Configuration ESW1:
ESW1#configure terminal
ESW1(config)#enable password 1234
ESW1(config)#username admin password 1234
ESW1(config)#line vty 0 4
ESW1(config-line)#login local
ESW1(config-line)#transport input telnet
ESW1(config-line)#exit
ESW1(config)#interface fastEthernet 1/2
ESW1(config-if)#ip address 10.10.10.7 255.255.255.0
ESW1(config-if)#no shutdown
ESW1#ping 10.10.10.5
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.10.10.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 60/270/1052 ms
Configuration ESW2:
ESW2#configure terminal
ESW2(config)#enable password 1234
ESW2(config)#username admin password 1234
ESW2(config)#line vty 0 4
ESW2(config-line)#login local
ESW2(config-line)#transport input telnet
ESW2(config-line)#exit
ESW2(config)#interface fastEthernet 1/1
ESW2(config-if)#ip address 10.10.10.8 255.255.255.0
ESW2(config-if)#no shutdown
ESW2#ping 10.10.10.5
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.10.10.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 68/270/1060 ms
CCNA>enable
Password:
CCNA#conf t
Enter configuration commands, one per line. End with CNTL/Z.
CCNA(config)#hostname CCNA
CCNA(config)#int fa 1/0
CCNA(config-if)#ip address 200.20.20.1 255.255.255.0
CCNA(config-if)#no shut
CCNA(config-if)#int fa 0/1
CCNA(config-if)#no shut
CCNA(config-if)#int fa 0/1.10
CCNA(config-subif)#encapsulation dot1Q 10
CCNA(config-subif)#ip address 192.168.10.1 255.255.255.0
CCNA(config-subif)#no shut
CCNA(config-subif)#ip access-group 150 in
CCNA(config-subif)#ip dhcp pool CCNA
CCNA(dhcp-config)#network 192.168.10.0 255.255.255.0
CCNA(dhcp-config)#default-router 192.168.10.1
CCNA(dhcp-config)#dns-server 8.8.8.8
CCNA(dhcp-config)#router ospf 10
CCNA(config-router)#network 200.20.20.0 0.0.0.255 area 0
CCNA(config-router)#network 192.168.10.0 0.0.0.255 area 0
CCNA(config-router)#$ 150 permit tcp host 192.168.10.2 host 200.20.20.2 eq 80
CCNA(config)#access-list 150 deny icmp host 192.168.10.2 host 200.20.20.2
CCNA(config)#access-list 150 permit ip any any
CCNA(config)#end
CCNA#wr
Building configuration...
[OK]
CCNA#exit
PS D:\Network Automation\Test>
ESW1>enable
Password:
ESW1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
ESW1(config)#hostname CCNA
CCNA(config)#int fa 1/0
CCNA(config-if)#switchport mode trunk
CCNA(config-if)#int fa 1/1
CCNA(config-if)#switchport mode trunk
CCNA(config-if)#switchport trunk allowed vlan 10
CCNA(config-if)#int fa 1/2
CCNA(config-if)#switchport mode access
CCNA(config-if)#switchport access vlan 10
CCNA(config-if)#exit
CCNA(config)#exit
CCNA#wr
Building configuration...
[OK]
CCNA#exit
Now Check your GNS3 Windows 10 Interface that It already configured from DHCP Server