Python tesztkörnyezet EVE-NG-ben

A sikeres JNCIA DevOps után úgy gondoltam folytatom az ismerkedést a Python nyelvvel – amennyire időm engedi.

A tanuláshoz EVE-NG-ben (hol máshol) állítottam össze egy teszt topológiát. Egy linux, egy switch, három router, kezdésnek bőven elég lesz. Az a jó a virtualizált labor környezetben hogy pillanatok alatt bővíthető tetszőleges eszközökkel.

Az internet kapcsolat a linuxon a frissítések miatt kell, de csak ideiglenesen.

 

 

Linuxnak az EVE-NG csapat által testre szabott Ubuntu Servert használok (a telepítéshez szükséges dokumentációt megtalálod itt).

A hálózati eszközök szinte “üres” konfiggal futnak (van egy IP címük amin a szerver eléri őket és engedélyezve van az SSH), hiszen a cél pont az lenne hogy majd a Python végezze el a munkát.

 

conf t
hostname X
int e0/0 / VLAN1
ip add 10.0.0.10X 255.255.255.0
no shutdown
ip domain-name fkuris.lab
username python priv 15 secret python
crypto key generate rsa mod 1024
line vty 0 4
transport input ssh
login local
end

 

Miután beállítottuk a szerveren a static IP-t nézzük meg hogy tudja -e pingelni a hálózati eszközöket.

 

root@ubuntu:~# ping 10.0.0.100 -c 2
PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data.
64 bytes from 10.0.0.100: icmp_seq=1 ttl=255 time=0.791 ms
64 bytes from 10.0.0.100: icmp_seq=2 ttl=255 time=0.942 ms

--- 10.0.0.100 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.791/0.866/0.942/0.081 ms
root@ubuntu:~# ping 10.0.0.101 -c 2
PING 10.0.0.101 (10.0.0.101) 56(84) bytes of data.
64 bytes from 10.0.0.101: icmp_seq=1 ttl=255 time=0.704 ms
64 bytes from 10.0.0.101: icmp_seq=2 ttl=255 time=0.790 ms

--- 10.0.0.101 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.704/0.747/0.790/0.043 ms
root@ubuntu:~# ping 10.0.0.102 -c 2
PING 10.0.0.102 (10.0.0.102) 56(84) bytes of data.
64 bytes from 10.0.0.102: icmp_seq=1 ttl=255 time=0.817 ms
64 bytes from 10.0.0.102: icmp_seq=2 ttl=255 time=0.956 ms

--- 10.0.0.102 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.817/0.886/0.956/0.075 ms
root@ubuntu:~# ping 10.0.0.103 -c 2
PING 10.0.0.103 (10.0.0.103) 56(84) bytes of data.
64 bytes from 10.0.0.103: icmp_seq=1 ttl=255 time=0.995 ms
64 bytes from 10.0.0.103: icmp_seq=2 ttl=255 time=0.997 ms

--- 10.0.0.103 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.995/0.996/0.997/0.001 ms
root@ubuntu:~#

 

Eddig rendben vagyunk.

A python telepítésével sem kell foglalkoznunk, elő van készítve a python2 és python3 is.

 

root@ubuntu:~# python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@ubuntu:~# python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@ubuntu:~#

 

Következő lépés a “netmiko” telepítése. Hogy mire való elolvashatod részletesen itt, de a lényeg röviden:

 

The purposes of this library are the following:

Successfully establish an SSH connection to the device
Simplify the execution of show commands and the retrieval of output data
Simplify execution of configuration commands including possibly commit actions
Do the above across a broad set of networking vendors and platforms

 

Az alábbi gyártókat/eszközöket támogatja:

 

Platform Support:

As of January 2019, Netmiko supports the following platforms with the indicated level of testing.

Regularly tested:

  • Arista EOS
  • Cisco ASA
  • Cisco IOS/IOS-XE
  • Cisco IOS-XR
  • Cisco NX-OS
  • Cisco SG300
  • HP Comware7
  • HP ProCurve
  • Juniper Junos
  • Linux

Limited testing:

  • Alcatel AOS6/AOS8
  • Apresia Systems AEOS
  • Calix B6
  • Cisco AireOS (Wireless LAN Controllers)
  • Dell OS9 (Force10)
  • Dell OS10
  • Dell PowerConnect
  • Extreme ERS (Avaya)
  • Extreme VSP (Avaya)
  • Extreme VDX (Brocade)
  • Extreme MLX/NetIron (Brocade/Foundry)
  • Huawei
  • IP Infusion OcNOS
  • Mellanox
  • NetApp cDOT
  • OneAccess
  • Palo Alto PAN-OS
  • Pluribus
  • Ruckus ICX/FastIron
  • Ubiquiti EdgeSwitch
  • Vyatta VyOS

 

Experimental:

  • A10
  • Accedian
  • Aruba
  • Ciena SAOS
  • Citrix Netscaler
  • Cisco Telepresence
  • Check Point GAiA
  • Coriant
  • Dell OS6
  • Dell EMC Isilon
  • Eltex
  • Enterasys
  • Extreme EXOS
  • Extreme Wing
  • Extreme SLX (Brocade)
  • F5 TMSH
  • F5 Linux
  • Fortinet
  • MRV Communications OptiSwitch
  • Nokia/Alcatel SR-OS
  • QuantaMesh
  • Rad ETX

 

Telepíteni szuper egyszerű.

 

root@ubuntu:~# apt install python-pip


.....

root@ubuntu:~# pip install netmiko


....

 

Nézzünk egy scriptet ami csak annyit csinál hogy kapcsolódik az eszközökre és utána kiírja a “show uptime” parancs kimenetét. Ezzel tesztelem hogy működik -e a labor környezet.

 

#!/usr/bin/env python

from netmiko import ConnectHandler

R1 = {
   'device_type': 'cisco_ios',
   'ip': '10.0.0.101',
   'username': 'python',
   'password': 'python',
}

R2 = {
   'device_type': 'cisco_ios',
   'ip': '10.0.0.102',
   'username': 'python',
   'password': 'python',
}

R3 = {
   'device_type': 'cisco_ios',
   'ip': '10.0.0.103',
   'username': 'python',
   'password': 'python',
}

SW1 = {
   'device_type': 'cisco_ios',
   'ip': '10.0.0.100',
   'username': 'python',
   'password': 'python',
}


all_devices = [R1, R2, R3, SW1]


for devices in all_devices:
    net_connect = ConnectHandler(**devices)
    prompt = net_connect.find_prompt()
    print ("Uptime for " + str(prompt))
    output = net_connect.send_command("sh ver | i uptime")
    print (output)
    print ("\n")
    print ("\n")

 

 

 

Az eredmény pedig:

 

 

A labor készen áll, lehet tanulni programozni.