DevNet 103: Network Automation Using Python


 Configure OSPF on Cisco Router by Python




Configure R1:

import paramiko
import time
import getpass

ip_address = "192.168.23.103"

username = raw_input("Enter your username of : " + ip_address + ": ")
password = getpass.getpass()

ssh_client  = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)

print "Successful connection  " + ip_address

remote_connection = ssh_client.invoke_shell()

print "Configure OSPF "

remote_connection.send('en\n')
time.sleep(1)
recv = remote_connection.recv(9999)

remote_connection.send('cisco\n')
time.sleep(1)
recv = remote_connection.recv(9999)

remote_connection.send("configure terminal\n")
remote_connection.send("interface loop 0\n")
remote_connection.send("ip address 1.1.1.1 255.255.255.0\n")
remote_connection.send("interface g1/0\n")
remote_connection.send("ip address 172.17.7.2 255.255.255.0\n")
remote_connection.send("exit\n")
remote_connection.send("router ospf 1\n")
remote_connection.send("network 1.1.1.1 0.0.0.0 area 0\n")
remote_connection.send("network 172.17.7.2 0.0.0.0 area 0\n")

remote_connection.send("exit\n")
time.sleep(15)                          
output  = remote_connection.recv(65535)
print output
ssh_client.close
Configure R2:
import paramiko
import time
import getpass

ip_address = "192.168.23.102"
username = raw_input("Enter your username of : " + ip_address + ": ")
password = getpass.getpass()

ssh_client  = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)

print "Successful connection  " + ip_address
remote_connection = ssh_client.invoke_shell()

print "Configure OSPF "

remote_connection.send('en\n')
time.sleep(1)
recv = remote_connection.recv(9999)
remote_connection.send('cisco\n')
time.sleep(1)
recv = remote_connection.recv(9999)

remote_connection.send("configure terminal\n")
remote_connection.send("interface loop 0\n")
remote_connection.send("ip address 2.2.2.2 255.255.255.0\n")
remote_connection.send("interface g1/0\n")
remote_connection.send("ip address 172.17.7.1 255.255.255.0\n")
remote_connection.send("exit\n")
remote_connection.send("router ospf 1\n")
remote_connection.send("network 2.2.2.2 0.0.0.0 area 0\n")
remote_connection.send("network 172.17.7.1 0.0.0.0 area 0\n")

remote_connection.send("exit\n")
time.sleep(15)                          
output  = remote_connection.recv(65535)
print output
ssh_client.close

Show IP Interfaces:
R1#show ip int br
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.23.103  YES manual up                    up
GigabitEthernet1/0         172.17.7.2      YES manual up                    up
Loopback0                  1.1.1.1         YES manual up                    up
R1#
Show IP Interfaces:
R2#show ip int br
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.23.102  YES manual up                    up
GigabitEthernet1/0         172.17.7.1      YES manual up                    up
Loopback0                  2.2.2.2         YES manual up                    up
R2#

Show IP Route on R1:

R1#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       a - application route
       + - replicated route, % - next hop override

Gateway of last resort is not set

      1.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C        1.1.1.0/24 is directly connected, Loopback0
L        1.1.1.1/32 is directly connected, Loopback0
      2.0.0.0/32 is subnetted, 1 subnets
O        2.2.2.2 [110/2] via 172.17.7.1, 00:27:00, GigabitEthernet1/0
      172.17.0.0/16 is variably subnetted, 2 subnets, 2 masks
C        172.17.7.0/24 is directly connected, GigabitEthernet1/0
L        172.17.7.2/32 is directly connected, GigabitEthernet1/0
      192.168.23.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.23.0/24 is directly connected, FastEthernet0/0
L        192.168.23.103/32 is directly connected, FastEthernet0/0

Show IP Route on R2:

R2#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       a - application route
       + - replicated route, % - next hop override

Gateway of last resort is not set

      1.0.0.0/32 is subnetted, 1 subnets
O        1.1.1.1 [110/2] via 172.17.7.2, 00:26:18, GigabitEthernet1/0
      2.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C        2.2.2.0/24 is directly connected, Loopback0
L        2.2.2.2/32 is directly connected, Loopback0
      172.17.0.0/16 is variably subnetted, 2 subnets, 2 masks
C        172.17.7.0/24 is directly connected, GigabitEthernet1/0
L        172.17.7.1/32 is directly connected, GigabitEthernet1/0
      192.168.23.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.23.0/24 is directly connected, FastEthernet0/0
L        192.168.23.102/32 is directly connected, FastEthernet0/0


Test OSPF:

R1#ping 2.2.2.2 source 1.1.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds:
Packet sent with a source address of 1.1.1.1
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 16/20/24 ms
R1#

R2#ping 1.1.1.1 source 2.2.2.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
Packet sent with a source address of 2.2.2.2
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 4/16/24 ms
R2#

---------------------------------------------------------------------------------------------------------------

Good Luck https://www.linkedin.com/in/ahmedms/










































Comments

Popular posts from this blog

Kubernetes 104: Create a 2-node k3s cluster with k3sup

Cisco Nexus: Configuration VXLAN.

How to configure OSPF on Palo Alto Networks Firewall?