Docker Project 1 : NETWORK AUTOMATION USING DOCKER AND GNS3
Hello Folks !!
As we know that automation is the ultimate future of any
industry whether it is software, networking, cloud or any other.
For removing manual errors as well as providing a robust and
flawless infrastructure we need automation .
One such example where automation is used is Networking . Be
it CISCO devices, Juniper, Palo alto, or any other, automation is the need of
the hour .
Overview :
In this project i have automated the most difficult task we face in the Networking field . Yes, the configuration part . Without automation also we can carry out the process but that have more chances of making some errors . Keeping in mind this motive i have build the Network Automation Container Environment that will do the task for us . For the demonstration purpose i have used some configurations like logging in via telnet and configure VLANs, renaming them, Configuring the loopbacks and finally taking the whole backup of all the routers and switches connected in my topology with just a single click .
For better and crystal clear understanding of the project i have made an extensive and elaborative video that is uploaded on YOUTUBE . The link of the same is given below -
https://www.youtube.com/watch?v=xfSb0by_-MQ&t=540s
I have used the Docker community edition to carry out the desired task and the base OS used here is RED HAT RHEL8 .
Here we go ..
(1) Pulling the Network automation image from Docker
Hub registry.
# docker pull gns3/network_automation
(2)
Launching a container named NAC (alias for
NETWORK AUTOMATION CONTAINER)
#docker
run –it –name NAC network_automation:latest
The interface of NAC :
(3)
Installing required environment :
#apt-get update
#apt-get install python3
#apt-get install ssh
#apt-get install telnet
#apt-get update
(4)
Writing a complete python script for configuring
the routers and switches given in the below picture :
#Vi auomation1.py
Writing under automation1.py (AUTOMATION SCRIPT)
The python script :
-
reating 100 VLANS on all the switches (S1 to S6)
-
Giving all those VLANS a particular name
-
Writing the running config to startup config
(Saving the data)
-
Exiting from all those switches
-
Finally Displaying the logs on the screen
import getpass
import telnetlib
HOST = "localhost"
user = input("Enter your remote account: ")
password = getpass.getpass()
f = open ('myswitches')
for IP in f:
IP=IP.strip()
print
("Configuring Switch " + (IP))
HOST = IP
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"cisco\n")
tn.write(b"conf t\n")
for n in range (2,101) :
tn.write(b”vlan”+str(n).encode(‘ascii’)+n”\n”)
tn.write(b”name”
Python_VLAN_+str(n).encode (‘ascii’)+b”\n”)
tn.write(b"end\n")
tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
Running the Script in CONTAINER in GNS3 :
The python script for configuring the switches (automation
script) :
Debugging the Telnet connections on switch to verify the
configurations :
Output of Debugging :
Getting the logs messages
on the console of NAC :
( (5) Creating script for taking the backup from
all the routers and switches connected in our topolpogy
#vi automation2.py [TAKING THE BACKUP OF A COMPLETE TOPOLOGY]
Writing in automation2.py
import getpass
import telnetlib
user = input('Enter your telnet username: ')
password = getpass.getpass()
f = open('myswitches')
for IP in f:
IP=IP.strip()
print ('Get
running config from Switch ' + (IP))
HOST = IP
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"terminal length 0\n")
tn.write(b"show run\n")
tn.write(b'exit\n')
readoutput =
tn.read_all()
saveoutput = open("switch" + HOST,
"w")
saveoutput.write(readoutput.decode('ascii'))
saveoutput.write("\n")
saveoutput.close
The
NAC is also tested to work as a separate VM : (Screenshots for the same is attached below )
((6) Building the image file using “Docker commit”
#docker commit my_automation NAC:v1
((7) Checking the required image has been
successfully created or not ?
#docker images
((8) Migrating the docker file to docker hub using
“Docker push”
#docker
tag NAC:v1 kshitijrathore/NAC:v1
#docker
push kshitijrathore/NAC:v1
Also we can verify on docker hub
(hub.docker.com)
((9) Transfer this environment to local PC so as to
test on simulator GNS3 :
#docker save NAC:v1 –o NAC.tar
To verify :
#ls
(10) Importing
this NAC in GNS3 and run the command in it –
NAC# python3 automation1.py
NAC# python3 automation2.py
For exact working of this, I have uploaded an extensive
video on it, explaining each and every step :
https://www.youtube.com/watch?v=xfSb0by_-MQ&t=540s
Links of all the automation scripts (python scripts) used in this project is uploaded on GitHub :
https://github.com/kshitij-webster/network_automation
- KSHITIJ SINGH RATHORE
Comments
Post a Comment