diff --git a/README.md b/README.md index e395ccdff96e5968b5030f4a10688f4a64310e06..63b5f568decd86686c2c8f04b9f39c7406318985 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,12 @@ cette commande est correcte en l’exécutant sur la topologie.  -# **2 - Routage et adressage** \ No newline at end of file +# **2 - Routage et adressage** + +Voici le résultat du playbook, la 77a0ae0163commande ping est effectivement exécuté et le paquet est bien envoyé sur le bon chemin (code sur le git) + + + +On peut voir grâce à la commande traceroute que le paquet utilise le bon chemin + + \ No newline at end of file diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000000000000000000000000000000000000..898fb2b400b57a5481e6e76fc08b74ae955103c0 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,10 @@ +[defaults] +inventory = inventory +remote_user = root +host_key_checking = False + +[privilege_escalation] +become = True +become_method = sudo +become_user = root +become_ask_pass = False diff --git a/changeHostname.sh b/changeHostname.sh new file mode 100755 index 0000000000000000000000000000000000000000..8aa9870ccd04bce33154dcf8c0baa51581839704 --- /dev/null +++ b/changeHostname.sh @@ -0,0 +1,4 @@ +ssh R1 hostnamectl set-hostname R1 +ssh R2 hostnamectl set-hostname R2 +ssh H1 hostnamectl set-hostname H1 +ssh H2 hostnamectl set-hostname H2 diff --git a/img/Untitled 4.png b/img/Untitled 4.png new file mode 100644 index 0000000000000000000000000000000000000000..878a764ae4f9b694114cb4af214bedae587f5f31 Binary files /dev/null and b/img/Untitled 4.png differ diff --git a/img/Untitled 5.png b/img/Untitled 5.png new file mode 100644 index 0000000000000000000000000000000000000000..e41e3d1d4c8dd12f3af1e6a8f5f2ae687f32b468 Binary files /dev/null and b/img/Untitled 5.png differ diff --git a/inventory b/inventory new file mode 100644 index 0000000000000000000000000000000000000000..2eede14dd1699450087e53a598b6bfda6a063979 --- /dev/null +++ b/inventory @@ -0,0 +1,20 @@ +[routers] +R1 ip="1.0.0.1" ip2="2.0.0.1" gateway="3.0.0.2" +R2 ip="3.0.0.2" ip2="2.0.0.2" gateway="3.0.0.2" +# R[1:2] +[hosts] +H1 ip="1.0.0.3" gateway="1.0.0.1" +H2 ip="3.0.0.3" gateway="3.0.0.2" +# H[1:2] + +[all:vars] +netmask="255.255.255.0" + +[hosts:vars] +ifname="eth0" +type="host" + +[routers:vars] +ifname="eth1" +ifname2="eth0" +type="router" diff --git a/main.py b/main.py new file mode 100644 index 0000000000000000000000000000000000000000..87a1b4b6167de34cefc3390788cc5a2c94f1b8bb --- /dev/null +++ b/main.py @@ -0,0 +1,109 @@ +import requests +import jinja2 +import json +import sys +import shutil +import subprocess + +PROJECTS_URL="https://gns3.hepiapp.ch/v2/projects" + +def usage(): + print(f"{sys.argv[0]} PROJECT_NAME") + +def get_projects_uid(): + r = requests.get(PROJECTS_URL) + for p in r.json(): + if p["name"] == sys.argv[1]: + print("Project ID: " + p["project_id"]) + return p["project_id"] + +def get_nodes(project_id): + print(PROJECTS_URL + '/' + str(project_id) + '/nodes') + r = requests.get(PROJECTS_URL + '/' + project_id + '/nodes') + return r.json() + +def create_ssh_config(name, console_port, console_host): + template = jinja2.Template("""Host {{ name }} + User root + HostName {{ console_host }} + Port {{ console_port }} + StrictHostKeyChecking no + UserKnownHostsFile /dev/null + IdentityFile ~/.ssh/gns3.rsa + """) + return template.render(name=name, console_port=console_port, console_host=console_host) + +def change_hostname(nodes): + cmds = "" + for node in nodes: + if node['console_type'] == "none": + continue + cmds += "ssh "+node["name"] + " hostnamectl set-hostname "+node["name"]+"\n" + + f = open("changeHostname.sh", "w") + f.write(cmds) + f.close() + +def change_mac(nodes): + cmds = "" + index = 0 + for node in nodes: + if node['console_type'] == "none": + continue + if node['name'][0] == 'H': + index += 1 + cmds += "ssh " + node["name"] + " ip addr add 192.168.0." + str(index) + "/24 dev eth0\n" + cmds += "ssh "+node["name"]+" ip link set dev eth0 addr 00:00:00:00:00:"+'{:02x}'.format(index)+"\n" + cmds += "ssh "+node["name"]+" ip link set dev eth0 up \n" + + + cmds += "echo Sleeping 10s for params to be applieds \n" + cmds += "sleep 10 \n" + index = 0 + for node in nodes: + if node['console_type'] == "none": + continue + if node['name'][0] == 'H': + index += 1 + cmds += "ssh H1 ping -c 1 192.168.0." + str(index) + "\n" + + f = open("changeMac.sh", "w") + f.write(cmds) + f.close() + +def installTcdump(nodes): + cmds = "" + for node in nodes: + if node['console_type'] == "none": + continue + cmds += "ssh "+node["name"] + " dhclient -v eth0\n" + cmds += "ssh "+node["name"]+" apt install -y tcpdump\n" + + f = open("installTcdump.sh", "w") + f.write(cmds) + f.close() + +def main(): + + project_id = get_projects_uid() + nodes = get_nodes(project_id) + sshConfigText = "" + for node in nodes: + if node['console_type'] == "none": + continue + # print(node["name"] + " " + str((node["console"] + 1)) + " " + node["console_host"]) + sshConfigText += create_ssh_config(node["name"], node["console"] + 1, node["console_host"]) + sshConfigText += "\n" + print(sshConfigText) + #Changer cette ligne pour le chemin de votre fichier de config ssh + f = open("/home/alexandre/.ssh/config.d/sshconfig", "w") + f.write(sshConfigText) + f.close() + + + change_hostname(nodes) + # installTcdump(nodes) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/playbook.yaml b/playbook.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c4a2e69ce7066bbaf5915bf394e23f21269b154f --- /dev/null +++ b/playbook.yaml @@ -0,0 +1,27 @@ +- name: Set eth0 static + hosts: all + tasks: + - name: Set iface static + template: + src: static_iface.j2 + dest: /etc/network/interfaces.d/ansible.conf + mode: 0644 +- name: Restart networking eth0 + hosts: all + tasks: + - name: Restart networking eth0 + shell: ifdown eth0 && ifup eth0 +- name: Restart networking eth1 + hosts: routers + tasks: + - name: Restart networking eth1 + shell: ifdown eth1 && ifup eth1 +- name: Test config by pinging + hosts: H1 + tasks: + - name: Ping H2 + shell: ping -c 3 3.0.0.3 + register: ping_result + - name: Print ping result + debug: + var: ping_result.stdout_lines diff --git a/static_iface.j2 b/static_iface.j2 new file mode 100644 index 0000000000000000000000000000000000000000..b242c1f6987d82c2eb6b0ef5feb755d2cd35b90e --- /dev/null +++ b/static_iface.j2 @@ -0,0 +1,18 @@ +iface {{ifname}} inet static + address {{ip}} + netmask {{netmask}} + +{% if type == "host" %} + post-up ip route add default via {{gateway}} +{% endif %} + +{% if type == "router" %} +iface {{ifname2}} inet static + address {{ip2}} + netmask {{netmask}} + {% if inventory_hostname == "R1" %} + post-up ip route add 3.0.0.0/24 nexthop via 2.0.0.2 + {% else %} + post-up ip route add 1.0.0.0/24 nexthop via 2.0.0.1 + {% endif %} +{% endif %} \ No newline at end of file