diff --git a/ansibletest.py b/ansibletest.py new file mode 100644 index 0000000000000000000000000000000000000000..538b9cdef9b5948213980260c396dc2b3b5aeb0a --- /dev/null +++ b/ansibletest.py @@ -0,0 +1,22 @@ +from ansible import context +from ansible.cli import CLI +from ansible.module_utils.common.collections import ImmutableDict +from ansible.executor.playbook_executor import PlaybookExecutor +from ansible.parsing.dataloader import DataLoader +from ansible.inventory.manager import InventoryManager +from ansible.vars.manager import VariableManager + +loader = DataLoader() + +context.CLIARGS = ImmutableDict(tags={}, listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', + module_path=None, forks=100, remote_user='admin', private_key_file=None, + ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, + verbosity=True, check=False, start_at_task=None) + +inventory = InventoryManager(loader=loader, sources=('inventory/hosts.yaml',)) + +variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False)) + +pbex = PlaybookExecutor(playbooks=['playbooks/asd.yaml'], inventory=inventory, variable_manager=variable_manager, loader=loader, passwords={}) + +results = pbex.run() \ No newline at end of file diff --git a/networkxtest.py b/networkxtest.py index ba309271c1015181bd49031b1ed829f966585184..2133af47378a9398ed3e3f69c47d64dc122f16ad 100644 --- a/networkxtest.py +++ b/networkxtest.py @@ -2,104 +2,154 @@ import networkx as nx import matplotlib.pyplot as plt import os as os - class Host: ips = list() masks = list() networks = list() + sroutes = list() + smasks = list() def __init__(self): self.name = "" self.ips = [] self.masks = [] self.networks = [] + self.sroutes = [] + self.smasks = [] G = nx.Graph() -routersNumber = 5 - -hosts = [] -newitem = Host() - -for x in range(1, routersNumber + 1): - - newitem = Host() #reconstruct new item to empty item - - os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_hostname.txt | grep "h" | cut -d\' \' -f2)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_name.txt') - with open('./router' + str(x) + '/router' + str(x) + '_name.txt','r') as file_1: - for line in file_1.readlines(): - line = line.replace('\n', '') - newitem.name = line - - os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_routes.txt | grep "^L " | cut -d\' \' -f9 | cut -d\'/\' -f1)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_locales.txt') - with open('./router' + str(x) + '/router' + str(x) + '_locales.txt', 'r') as file_2: - for line in file_2.readlines(): - #print(line) - line = line.replace('\n', '') - newitem.ips.append(line) - - os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_routes.txt | grep "^C " | cut -d\' \' -f9 | cut -d\'/\' -f2)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_masks.txt') - with open('./router' + str(x) + '/router' + str(x) + '_masks.txt', 'r') as file_3: - for line in file_3.readlines(): - line = line.replace('\n', '') - newitem.masks.append(line) - - os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_routes.txt | grep "^C " | cut -d\' \' -f9 | cut -d\'/\' -f1)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_networks.txt') - with open('./router' + str(x) + '/router' + str(x) + '_networks.txt', 'r') as file_4: - for line in file_4.readlines(): - line = line.replace('\n', '') - newitem.networks.append(line) - hosts.append(newitem) -''' -for all in hosts: - print(all.name) - for i in all.ips: - print(i) - for i in all.masks: - print(i) - for i in all.networks: - print(i) - print("") -''' - -for all in hosts: - G.add_node(all.name) - G.add_nodes_from([all.name], ips = all.ips) - G.add_nodes_from([all.name], masks = all.masks) - G.add_nodes_from([all.name], networks = all.networks) - -for i in range(len(hosts)-1): - for j in range(i+1, len(hosts)): - currentelement = hosts[i] - nextelement = hosts[j] - for crnws in currentelement.networks: - for nxnws in nextelement.networks: - if crnws == nxnws: - G.add_edge(currentelement.name, nextelement.name) +routersNumber = 0 +searchedNetwork = 0 +searchedMask = 0 +sourceNetwork = 0 +sourceMask = 0 pathlist= [] -all_paths = list(nx.all_simple_paths(G, source="R2", target="R5")) -for path in all_paths: - pathelements = [] - for all in path: - pathelements.append(all) - print(all, end=' ') - print("\n") - pathlist.append(pathelements) - -for all in pathlist: - for to in all: - print(to, end=" ") - -#print(G.nodes.data()) -nx.draw_networkx(G) -plt.show() +hosts = [] +def read(): + global routersNumber + global searchedNetwork + global searchedMask + global sourceNetwork + global sourceMask + + newitem = Host() + + routersNumber = int(input("Adja meg hany router van a halozatban (szammal): ")) + + searchedNetwork = input("Adja meg a keresett halozatot (maszk nelkul): ") + searchedMask = input("Adja meg a keresett halozat maszkjat: ") + + sourceNetwork = input("Adja meg a kiindulo halozatot (maszk nelkul): ") + sourceMask = input("Adja meg a kiindulo halozat maszkjat: ") + + for x in range(1, routersNumber + 1): + + newitem = Host() #reconstruct new item to empty item + + os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_hostname.txt | grep "h" | cut -d\' \' -f2)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_name.txt') + with open('./router' + str(x) + '/router' + str(x) + '_name.txt','r') as file_1: + for line in file_1.readlines(): + line = line.replace('\n', '') + newitem.name = line + + os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_routes.txt | grep "^L " | cut -d\' \' -f9 | cut -d\'/\' -f1)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_locales.txt') + with open('./router' + str(x) + '/router' + str(x) + '_locales.txt', 'r') as file_2: + for line in file_2.readlines(): + #print(line) + line = line.replace('\n', '') + newitem.ips.append(line) + + os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_routes.txt | grep "^C " | cut -d\' \' -f9 | cut -d\'/\' -f2)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_masks.txt') + with open('./router' + str(x) + '/router' + str(x) + '_masks.txt', 'r') as file_3: + for line in file_3.readlines(): + line = line.replace('\n', '') + newitem.masks.append(line) + + os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_routes.txt | grep "^C " | cut -d\' \' -f9 | cut -d\'/\' -f1)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_networks.txt') + with open('./router' + str(x) + '/router' + str(x) + '_networks.txt', 'r') as file_4: + for line in file_4.readlines(): + line = line.replace('\n', '') + newitem.networks.append(line) + + os.system('echo "$(cat ~/onlab/router' + str(x) + '/router' + str(x) + '_routes.txt | grep "^S " | cut -d\' \' -f5)" >> ~/onlab/router' + str(x) + '/router' + str(x) + '_sroutes.txt') + with open('./router' + str(x) + '/router' + str(x) + '_sroutes.txt', 'r') as file_5: + for line in file_5.readlines(): + line = line.replace('\n', '') + if line != '': + x = line.split('/') + print(x) + newitem.sroutes.append(x[0]) + newitem.smasks.append(x[1]) + + hosts.append(newitem) + +def graphConstruct(): + for all in hosts: + G.add_node(all.name) + G.add_nodes_from([all.name], ips = all.ips) + G.add_nodes_from([all.name], masks = all.masks) + G.add_nodes_from([all.name], networks = all.networks) + G.add_nodes_from([all.name], sroutes = all.sroutes) + G.add_nodes_from([all.name], smasks = all.smasks) -for x in range(1, routersNumber + 1 ): - os.system('rm ~/onlab/router' + str(x) + '/router' + str(x) + '_locales.txt ~/onlab/router' + str(x) + '/router' + str(x) + '_name.txt ~/onlab/router' + str(x) + '/router' + str(x) + '_masks.txt ~/onlab/router' + str(x) + '/router' + str(x) + '_networks.txt') - + for i in range(len(hosts)-1): + for j in range(i+1, len(hosts)): + currentelement = hosts[i] + nextelement = hosts[j] + for crnws in currentelement.networks: + for nxnws in nextelement.networks: + if crnws == nxnws: + G.add_edge(currentelement.name, nextelement.name) + print(G.nodes.data()) + +def findpath(): + global pathlist + + all_paths = list(nx.all_simple_paths(G, source="R2", target="R5")) + for path in all_paths: + pathelements = [] + for all in path: + pathelements.append(all) + print(all, end=' ') + print("\n") + pathlist.append(pathelements) + +def checkroutings(): + + for i in pathlist: + for j in i: + for x in hosts: + if j == x.name: + if searchedNetwork in x.networks or searchedNetwork in x.sroutes: + print(x.name) + print("route megnezve") + +def repairtables(): + #itt majd javitja a routingot + print("routing javitva") + +def draw(): + nx.draw_networkx(G) + plt.show() + +def clear(): + for x in range(1, routersNumber + 1 ): + os.system('rm ~/onlab/router' + str(x) + '/router' + str(x) + '_locales.txt ~/onlab/router' + str(x) + '/router' + str(x) + '_name.txt ~/onlab/router' + str(x) + '/router' + str(x) + '_masks.txt ~/onlab/router' + str(x) + '/router' + str(x) + '_networks.txt ~/onlab/router' + str(x) + '/router' + str(x) + '_sroutes.txt') + +def main(): + read() + graphConstruct() + findpath() + checkroutings() + draw() + clear() + +if __name__ == "__main__": + main() diff --git a/playbooks/asd.yaml b/playbooks/asd.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a9e903bd832858cf6aabbfbe019f4e174e09326b --- /dev/null +++ b/playbooks/asd.yaml @@ -0,0 +1,12 @@ +--- +- name: Get informations + hosts: routers + gather_facts: false + connection: network_cli + tasks: + - name: Show hostname + cisco.ios.ios_command: + commands: + - show run | i hostname + + diff --git a/playbooks/get_informations.yaml b/playbooks/get_informations.yaml index 0f12bd717c5b6186a0a866e8b74dbe12e7b56f94..462933e30b8844d0447238e86cbce8a6f9cdd30d 100644 --- a/playbooks/get_informations.yaml +++ b/playbooks/get_informations.yaml @@ -6,14 +6,16 @@ tasks: - name: Show hostname cisco.ios.ios_command: - commands: show run | i hostname - register: hostname + commands: + - show run | i hostname + register: hostname - name: Create dir ansible.builtin.file: - path: ~/onlab/{{inventory_hostname}} + path: "{{ ansible_env.HOME }}/onlab/{{ inventory_hostname }}" state: directory + - name: Print interface config copy: content: "{{hostname.stdout[0]}}" diff --git a/router4/router4_routes.txt b/router4/router4_routes.txt index a49869ba18fdf7a4b590ee09698a7efb018dbf47..c9a9cac47ddd3ef113f13e7d832042d49415fdb5 100644 --- a/router4/router4_routes.txt +++ b/router4/router4_routes.txt @@ -15,4 +15,5 @@ L 192.168.3.2/32 is directly connected, GigabitEthernet0/0/0 C 192.168.5.0/24 is directly connected, GigabitEthernet0/0/1 L 192.168.5.2/32 is directly connected, GigabitEthernet0/0/1 C 192.168.6.0/24 is directly connected, GigabitEthernet0/0/1 -L 192.168.6.2/32 is directly connected, GigabitEthernet0/0/1 \ No newline at end of file +L 192.168.6.2/32 is directly connected, GigabitEthernet0/0/1 +S 192.168.2.0/24 \ No newline at end of file diff --git a/router5/router5_routes.txt b/router5/router5_routes.txt index 6e5cf89b77a5afa42eb5ed86ebf780134002d7b4..71ae8768ca6985bd2f212b52d96d0c522cd58499 100644 --- a/router5/router5_routes.txt +++ b/router5/router5_routes.txt @@ -12,4 +12,5 @@ Gateway of last resort is not set C 192.168.4.0/24 is directly connected, GigabitEthernet0/0/1 L 192.168.4.2/32 is directly connected, GigabitEthernet0/0/1 C 192.168.6.0/24 is directly connected, GigabitEthernet0/0/1 -L 192.168.6.1/32 is directly connected, GigabitEthernet0/0/1 \ No newline at end of file +L 192.168.6.1/32 is directly connected, GigabitEthernet0/0/1 +S 192.168.2.0/24 \ No newline at end of file diff --git a/test2.py b/test2.py new file mode 100644 index 0000000000000000000000000000000000000000..77aa5a35251f4a2079b96e994f218b715fd90f60 --- /dev/null +++ b/test2.py @@ -0,0 +1,3 @@ +from ansible_playbook_runner import Runner + +Runner(['inventory/hosts.yaml'], 'playbooks/asd.yaml').run() \ No newline at end of file