Skip to content
Snippets Groups Projects
Commit 6ac84619 authored by Barta Máté Zsombor's avatar Barta Máté Zsombor :speech_balloon:
Browse files

asd

parent 8bdfebc4
No related branches found
No related tags found
No related merge requests found
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
......@@ -2,25 +2,50 @@ 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
routersNumber = 0
searchedNetwork = 0
searchedMask = 0
sourceNetwork = 0
sourceMask = 0
pathlist= []
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
......@@ -49,24 +74,27 @@ for x in range(1, routersNumber + 1):
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)
'''
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("")
'''
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 i in range(len(hosts)-1):
for j in range(i+1, len(hosts)):
......@@ -76,8 +104,10 @@ for i in range(len(hosts)-1):
for nxnws in nextelement.networks:
if crnws == nxnws:
G.add_edge(currentelement.name, nextelement.name)
print(G.nodes.data())
pathlist= []
def findpath():
global pathlist
all_paths = list(nx.all_simple_paths(G, source="R2", target="R5"))
for path in all_paths:
......@@ -88,18 +118,38 @@ for path in all_paths:
print("\n")
pathlist.append(pathelements)
for all in pathlist:
for to in all:
print(to, end=" ")
def checkroutings():
#print(G.nodes.data())
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')
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()
---
- name: Get informations
hosts: routers
gather_facts: false
connection: network_cli
tasks:
- name: Show hostname
cisco.ios.ios_command:
commands:
- show run | i hostname
......@@ -6,14 +6,16 @@
tasks:
- name: Show hostname
cisco.ios.ios_command:
commands: show run | i 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]}}"
......
......@@ -16,3 +16,4 @@ 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
S 192.168.2.0/24
\ No newline at end of file
......@@ -13,3 +13,4 @@ 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
S 192.168.2.0/24
\ No newline at end of file
from ansible_playbook_runner import Runner
Runner(['inventory/hosts.yaml'], 'playbooks/asd.yaml').run()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment