From d725080e28281dfa2945e09ed7f36a739baae172 Mon Sep 17 00:00:00 2001
From: bmzsombi <bmzsombi@sch.bme.hu>
Date: Fri, 26 May 2023 11:56:29 +0200
Subject: [PATCH] Repair added, almost working with algorithm

---
 .vscode/settings.json                         |  3 ++
 int.sh                                        |  4 ++
 inventory/hosts.yaml                          |  2 +-
 maskrecover.py                                |  8 +++-
 playbooks/get_interfaces.yaml                 | 15 ++++++++
 playbooks/get_routing_tables.yaml             | 16 ++++++++
 playbooks/repair_routing.yaml                 | 13 +++++++
 plays/test.yaml                               | 25 ------------
 router1/router1_ip.txt                        |  4 --
 router1/router1_mask.txt                      |  8 +---
 router1/{router1.txt => router1_routingt.txt} |  3 +-
 router2/router2.txt                           | 11 ------
 router2/router2_interface.txt                 | 38 ++++++++++++++-----
 router2/router2_ip.txt                        |  2 +
 router2/router2_mask.txt                      |  1 +
 router_config                                 |  4 +-
 sort.sh                                       |  6 +--
 start.sh                                      |  4 ++
 18 files changed, 100 insertions(+), 67 deletions(-)
 create mode 100644 .vscode/settings.json
 create mode 100755 int.sh
 create mode 100644 playbooks/get_interfaces.yaml
 create mode 100644 playbooks/get_routing_tables.yaml
 create mode 100644 playbooks/repair_routing.yaml
 delete mode 100644 plays/test.yaml
 rename router1/{router1.txt => router1_routingt.txt} (87%)
 delete mode 100644 router2/router2.txt
 create mode 100755 start.sh

diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..9d14cfb
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+    "ansible.python.interpreterPath": "/bin/python"
+}
\ No newline at end of file
diff --git a/int.sh b/int.sh
new file mode 100755
index 0000000..abdfea2
--- /dev/null
+++ b/int.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+cat ./router2/router2_interface.txt | grep "^  Internet" | cut -d' ' -f6 | cut -d'/' -f1 >> ./router2/router2_ip.txt
+cat ./router2/router2_interface.txt | grep "^  Internet" | cut -d' ' -f6 | cut -d'/' -f2 >> ./router2/router2_mask.txt
diff --git a/inventory/hosts.yaml b/inventory/hosts.yaml
index 79a6e45..64313ee 100644
--- a/inventory/hosts.yaml
+++ b/inventory/hosts.yaml
@@ -1,5 +1,5 @@
 [routers]
-router1 int_ip=10.10.20.100
+router1 int_mask=255.255.255.248
 router2 int_ip=10.10.20.101
 
 [routers:vars]
diff --git a/maskrecover.py b/maskrecover.py
index b4d7b00..8860fbf 100755
--- a/maskrecover.py
+++ b/maskrecover.py
@@ -12,8 +12,14 @@ def read_file():
         router2_ips = file.readlines()
     with open(router2_mask_path, "r") as file:
         router2_masks = file.readlines()
-    if router1_masks < router2_masks:
+    print(router1_masks[0])
+    print(router2_masks[0])
+    if router1_masks[0] < router2_masks[0]:
         router1_masks[0] = router2_masks[0]
+        print(router1_masks[0])
+    with open(router1_mask_path, "w") as file:
+        for item in router1_masks:
+            file.write(str(item) + "\n")
 
 
 if __name__ == "__main__":
diff --git a/playbooks/get_interfaces.yaml b/playbooks/get_interfaces.yaml
new file mode 100644
index 0000000..2710319
--- /dev/null
+++ b/playbooks/get_interfaces.yaml
@@ -0,0 +1,15 @@
+---
+- name: Get interfaces
+  hosts: router2
+  gather_facts: false
+  connection: network_cli
+  tasks:
+    - name: Show interfaces informations
+      cisco.ios.ios_command:
+        commands: show int gi0/1
+      register: interface_config
+
+    - name: Print interface config
+      copy:
+        content: "{{interface_config.stdout[0]}}"
+        dest: ~/onlab/{{inventory_hostname}}/{{inventory_hostname}}_interface.txt
diff --git a/playbooks/get_routing_tables.yaml b/playbooks/get_routing_tables.yaml
new file mode 100644
index 0000000..be39b03
--- /dev/null
+++ b/playbooks/get_routing_tables.yaml
@@ -0,0 +1,16 @@
+---
+- name: Get routing tables
+  hosts: router1
+  gather_facts: false
+  connection: network_cli
+  tasks:
+
+    - name: Show static routing table
+      cisco.ios.ios_command:
+        commands: show ip static route
+      register: routing_table
+
+    - name: Print routing table
+      copy:
+        content: "{{routing_table.stdout[0]}}"
+        dest: ~/onlab/{{inventory_hostname}}/{{inventory_hostname}}_routingt.txt
diff --git a/playbooks/repair_routing.yaml b/playbooks/repair_routing.yaml
new file mode 100644
index 0000000..722fa6e
--- /dev/null
+++ b/playbooks/repair_routing.yaml
@@ -0,0 +1,13 @@
+---
+- name: Repair routing mask
+  hosts: router1
+  gather_facts: false
+  connection: network_cli
+  tasks:
+
+    - name: Mask repair
+      cisco.ios.ios_command:
+        commands: 
+          - configure terminal
+          - no ip route *
+          - "ip route 10.10.20.176 {{int_mask}} 10.10.20.170"
diff --git a/plays/test.yaml b/plays/test.yaml
deleted file mode 100644
index 1f23be6..0000000
--- a/plays/test.yaml
+++ /dev/null
@@ -1,25 +0,0 @@
----
-- name: Test
-  hosts: routers
-  gather_facts: no
-  connection: network_cli
-  tasks:
-    - name: Show static ruting table
-      cisco.ios.ios_command:
-        commands: show ip static route
-      register: routing_table
-
-    - name: Print routing table
-      copy:
-        content: "{{routing_table.stdout[0]}}"
-        dest: ~/onlab/{{inventory_hostname}}/{{inventory_hostname}}.txt
-
-    - name: Show config of interfaces gi0/1
-      cisco.ios.ios_command:
-        commands: show running-config interface gigabitEthernet 0/1
-      register: interface_config
-
-    - name: Print interface config
-      copy:
-        content: "{{interface_config.stdout[0]}}"
-        dest: ~/onlab/{{inventory_hostname}}/{{inventory_hostname}}_interface.txt
diff --git a/router1/router1_ip.txt b/router1/router1_ip.txt
index 8c0e09b..504526b 100644
--- a/router1/router1_ip.txt
+++ b/router1/router1_ip.txt
@@ -1,8 +1,4 @@
-10.10.20.160
 10.10.20.176
-10.10.20.160
 10.10.20.176
-10.10.20.160
 10.10.20.176
-10.10.20.160
 10.10.20.176
diff --git a/router1/router1_mask.txt b/router1/router1_mask.txt
index 7fd56bc..95daef6 100644
--- a/router1/router1_mask.txt
+++ b/router1/router1_mask.txt
@@ -1,8 +1,2 @@
 29
-29
-29
-29
-29
-29
-29
-29
+
diff --git a/router1/router1.txt b/router1/router1_routingt.txt
similarity index 87%
rename from router1/router1.txt
rename to router1/router1_routingt.txt
index 4cf3cb5..73a17d4 100644
--- a/router1/router1.txt
+++ b/router1/router1_routingt.txt
@@ -12,5 +12,4 @@ Codes in []: A - active, N - non-active, B - BFD-tracked, D - Not Tracked, P - p
 
 Static local RIB for default 
 
-M  10.10.20.160/29 [1/0] via 10.10.20.177 [N]
-M  10.10.20.176/29 [1/0] via 10.10.20.177 [N]
\ No newline at end of file
+M  10.10.20.176/28 [1/0] via 10.10.20.170 [A]
\ No newline at end of file
diff --git a/router2/router2.txt b/router2/router2.txt
deleted file mode 100644
index b241739..0000000
--- a/router2/router2.txt
+++ /dev/null
@@ -1,11 +0,0 @@
-Codes: M - Manual static, A - AAA download, N - IP NAT, D - DHCP,
-       G - GPRS, V - Crypto VPN, C - CASA, P - Channel interface processor,
-       B - BootP, S - Service selection gateway
-       DN - Default Network, T - Tracking object
-       L - TL1, E - OER, I - iEdge
-       D1 - Dot1x Vlan Network, K - MWAM Route
-       PP - PPP default route, MR - MRIPv6, SS - SSLVPN
-       H - IPe Host, ID - IPe Domain Broadcast
-       U - User GPRS, TE - MPLS Traffic-eng, LI - LIIN
-       IR - ICMP Redirect
-Codes in []: A - active, N - non-active, B - BFD-tracked, D - Not Tracked, P - permanent
\ No newline at end of file
diff --git a/router2/router2_interface.txt b/router2/router2_interface.txt
index ad92115..af435e1 100644
--- a/router2/router2_interface.txt
+++ b/router2/router2_interface.txt
@@ -1,10 +1,28 @@
-Building configuration...
-
-Current configuration : 119 bytes
-!
-interface GigabitEthernet0/1
- ip address 10.10.20.177 255.255.255.248
- duplex auto
- speed auto
- media-type rj45
-end
\ No newline at end of file
+GigabitEthernet0/1 is up, line protocol is up 
+  Hardware is iGbE, address is 5254.0012.473f (bia 5254.0012.473f)
+  Internet address is 10.10.20.170/29
+  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec, 
+     reliability 255/255, txload 1/255, rxload 1/255
+  Encapsulation ARPA, loopback not set
+  Keepalive set (10 sec)
+  Auto Duplex, Auto Speed, link type is auto, media type is RJ45
+  output flow-control is unsupported, input flow-control is unsupported
+  ARP type: ARPA, ARP Timeout 04:00:00
+  Last input 00:00:45, output 00:00:08, output hang never
+  Last clearing of "show interface" counters never
+  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
+  Queueing strategy: fifo
+  Output queue: 0/40 (size/max)
+  5 minute input rate 0 bits/sec, 0 packets/sec
+  5 minute output rate 0 bits/sec, 0 packets/sec
+     35 packets input, 12756 bytes, 0 no buffer
+     Received 16 broadcasts (0 IP multicasts)
+     0 runts, 0 giants, 0 throttles 
+     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
+     0 watchdog, 0 multicast, 0 pause input
+     177 packets output, 23709 bytes, 0 underruns
+     0 output errors, 0 collisions, 2 interface resets
+     0 unknown protocol drops
+     0 babbles, 0 late collision, 0 deferred
+     0 lost carrier, 0 no carrier, 0 pause output
+     0 output buffer failures, 0 output buffers swapped out
\ No newline at end of file
diff --git a/router2/router2_ip.txt b/router2/router2_ip.txt
index e69de29..30253fb 100644
--- a/router2/router2_ip.txt
+++ b/router2/router2_ip.txt
@@ -0,0 +1,2 @@
+10.10.20.170
+10.10.20.170
diff --git a/router2/router2_mask.txt b/router2/router2_mask.txt
index e69de29..f04c001 100644
--- a/router2/router2_mask.txt
+++ b/router2/router2_mask.txt
@@ -0,0 +1 @@
+29
diff --git a/router_config b/router_config
index 32adc7d..838fe3a 100644
--- a/router_config
+++ b/router_config
@@ -1,6 +1,6 @@
 enable
 configure terminal
-hostname R3
+hostname R2
 enable secret cisco
 !
 username bmzsombi secret cisco
@@ -17,7 +17,7 @@ vrf definition Mgmt-intf
 address-family ipv4
 interface g0/0
 vrf forwarding Mgmt-intf
-ip address 10.10.20.102 255.255.255.0
+ip address 10.10.20.101 255.255.255.0
 no shutdown
 exit
 ip route vrf Mgmt-intf 0.0.0.0 0.0.0.0 10.10.20.254
diff --git a/sort.sh b/sort.sh
index 9a6d5bd..e389884 100755
--- a/sort.sh
+++ b/sort.sh
@@ -1,4 +1,2 @@
-cat ./router1/router1.txt | grep "^M" | cut -d' ' -f3 | cut -d'/' -f1 >> ./router1/router1_ip.txt
-cat ./router1/router1.txt | grep "^M" | cut -d' ' -f3 | cut -d'/' -f2 >> ./router1/router1_mask.txt
-cat ./router2/router2.txt | grep "^M" | cut -d' ' -f3 | cut -d'/' -f1 >> ./router2/router2_ip.txt
-cat ./router2/router2.txt | grep "^M" | cut -d' ' -f3 | cut -d'/' -f2 >> ./router2/router2_mask.txt
+cat ./router1/router1_routingt.txt | grep "^M" | cut -d' ' -f3 | cut -d'/' -f1 >> ./router1/router1_ip.txt
+cat ./router1/router1_routingt.txt | grep "^M" | cut -d' ' -f3 | cut -d'/' -f2 >> ./router1/router1_mask.txt
diff --git a/start.sh b/start.sh
new file mode 100755
index 0000000..a06bb3c
--- /dev/null
+++ b/start.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+ansible-playbook ./plays/test.yaml -k -K
+
-- 
GitLab