PyGNS3 Examples

is package to interact with GNS3.

Github repository / PyPi

This notebook shows examples of implemented functionality so far. Not exhaustive, work in progress etc etc.


In [1]:
# Minimal setup
from pygns3 import *
GNS3API.load_configuration()

# Main class
controller = GNS3Controller()
print(controller)


GNS3 Controller API endpoint
    Host    http://127.0.0.1:3080
    Version 2.0.3
    Found   2 Projects
    Running 2 Computes


In [2]:
for compute in controller.computes:
    print(f'{compute.name:{30}} ({compute.id})')


GNS3 VM                        (11df1f68-23ab-42f5-9a93-af65b7daad2a)
DJ-Johns-MacBook-Pro.local     (local)

In [3]:
my_compute = GNS3Compute('local')
print(my_compute)
if my_compute.connected:
        print(f"Available images for emulating on dynamips:")
        for image in my_compute.images('dynamips'):
            print(f"    {image.filename}")


GNS3Compute settings:
    compute_id           local
    connected            True
    cpu_usage_percent    13.5
    host                 127.0.0.1
    memory_usage_percent 72.4
    name                 DJ-Johns-MacBook-Pro.local
    port                 3080
    protocol             http
    user                 admin
    node_types           ['cloud', 'ethernet_hub', 'ethernet_switch', 'vpcs', 'virtualbox', 'dynamips', 'frame_relay_switch', 'atm_switch', 'qemu', 'vmware']
    platform             darwin
    version              2.0.3

Available images for emulating on dynamips:
    c1700-adventerprisek9-mz.124-8.bin
    c2600-i-mz.123-9.bin
    c2691-entservicesk9-mz.124-13b.bin
    c2691-entservicesk9-mz.124-13b.image
    c3640-ik9o3s-mz124-13.bin
    c3640-ik9o3s-mz124-13.image
    c3660-jk9o3s-mz.124-25d.bin
    c3725-adventerprisek9-mz124-15.bin
    c3725-adventerprisek9-mz124-15.image
    c3745-advipservicesk9-mz.124-25d.bin
    c7200-advipservicesk9-mz.152-4.S5.bin
    c7200-advipservicesk9-mz.152-4.S5.image
    c7200p-advipservicesk9-mz.152-4.S5.bin

In [4]:
gns3vm = GNS3VM()

print(gns3vm)

for engine in gns3vm.engines:
    for vm in engine.vms:
        print(f'{engine.name:{15}} {vm}')


GNS3VM settings:
    enable    False
    engine    vmware
    headless  False
    ram       2048
    vcpus     1
    vmname    GNS3 VM
    when_exit stop

VMware Fusion   W16WORK
VMware Fusion   RHEL72-OC
VMware Fusion   W16 RESAM v10
VMware Fusion   GNS3 VM
VMware Fusion   Debian 8.x 64-bit
VMware Fusion   W16REVERSE
VMware Fusion   OpenSUSE 64-bit
VMware Fusion   w2016_Clean
VMware Fusion   Windows Server 2016
Remote          GNS3 VM

In [5]:
for project in controller.projects:
    print(f'{project.name:{25}} {project.project_id}')


Basic 4 Routers           a1ea2a19-2980-41aa-81ab-f1c80be25ca7
Basic Cloud Connection    5daa48ff-dbd6-407c-a3c6-645e743f233a

In [6]:
my_project = GNS3Project('5daa48ff-dbd6-407c-a3c6-645e743f233a')
print(my_project)


GNS3Project settings:
    auto_close   True
    auto_open    False
    auto_start   False
    filename     Basic Cloud Connection.gns3
    name         Basic Cloud Connection
    path         /Users/maarten/GNS3/projects/Basic Cloud Connection
    project_id   5daa48ff-dbd6-407c-a3c6-645e743f233a
    scene_height 1000
    scene_width  2000
    status       closed
    drawings     0
    links        0
    nodes        0
    snapshots    0


In [7]:
# Besides grabbing a project from the collection of Controller.projects it is also possible to instantiate by name
my_project = GNS3Project.from_name('Basic 4 Routers')
print(my_project)


GNS3Project settings:
    auto_close   True
    auto_open    False
    auto_start   False
    filename     Basic 4 Routers.gns3
    name         Basic 4 Routers
    path         /Users/maarten/GNS3/Projects/Basic 4 Routers
    project_id   a1ea2a19-2980-41aa-81ab-f1c80be25ca7
    scene_height 1000
    scene_width  2000
    status       opened
    drawings     6
    links        6
    nodes        6
    snapshots    1


In [8]:
# You can start, suspend and stop all nodes with a simple method.
# If the project is visible in the GUI you can confirm (Whoo blinky lights!)
my_project.start_all_nodes()
my_project.suspend_all_nodes()
my_project.stop_all_nodes()


All nodes have been started.
All nodes have been suspended.
All nodes have been stopped.

In [9]:
# Drawings, links, nodes, and snapshots are listed by their number but internally consist of a list of objects
# Let's look at a node's settings
my_node = my_project.nodes[0]
print(my_node)


GNSNode settings:
    command_line       None
    compute_id         local
    console            5002
    console_host       127.0.0.1
    console_type       telnet
    first_port_name    None
    height             45
    label              {'rotation': 0, 'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;', 'text': 'C7200-1', 'x': 8, 'y': 21}
    name               C7200-1
    node_directory     /Users/maarten/GNS3/Projects/Basic 4 Routers/project-files/dynamips/61c67710-3c63-4f0d-bc4c-9680593e1a19
    node_id            61c67710-3c63-4f0d-bc4c-9680593e1a19
    node_type          dynamips
    port_name_format   Ethernet{0}
    port_segment_size  0
    ports              7
    project_id         a1ea2a19-2980-41aa-81ab-f1c80be25ca7
    status             stopped
    symbol             :/symbols/router.svg
    width              66
    x                  117
    y                  -173
    z                  1


In [10]:
# There are also properties defined on a node
print(my_node.properties)


GNSNodeProperties:
    auto_delete_disks       True
    aux                     None
    clock_divisor           4
    disk0                   0
    disk1                   0
    dynamips_id             1
    exec_area               64
    idlemax                 500
    idlepc                  0x63184bc8
    idlesleep               30
    image                   c7200-advipservicesk9-mz.152-4.S5.image
    image_md5sum            cbbbea66a253f1dac0fcf81274dc778d
    mac_addr                ca01.0578.0000
    midplane                vxr
    mmap                    True
    npe                     npe-400
    nvram                   512
    platform                c7200
    power_supplies          [1, 1]
    private_config          /Users/maarten/GNS3/projects/Basic 4 Routers/project-files/dynamips/61c67710-3c63-4f0d-bc4c-9680593e1a19/configs/i1_private-config.cfg
    private_config_content  
end

    ram                     512
    sensors                 [22, 22, 22, 22]
    slot0                   C7200-IO-FE
    slot1                   PA-2FE-TX
    slot2                   PA-2FE-TX
    slot3                   PA-GE
    slot4                   PA-GE
    slot5                   None
    slot6                   None
    sparsemem               True
    startup_config          configs/i1_startup-config.cfg
    startup_config_content  !
!
!
!
!
!
!
!
!
!
!
!
!
!
!

!
! Last configuration change at 13:40:22 UTC Wed Aug 2 2017
!
version 15.2
service timestamps debug datetime msec
service timestamps log datetime msec
!
hostname C7200-1
!
boot-start-marker
boot-end-marker
!
!
!
no aaa new-model
no ip icmp rate-limit unreachable
ip cef
!
!
!
!
!
!
no ip domain lookup
no ipv6 cef
!
!
multilink bundle-name authenticated
!
!
!
!
!
!
!
!
!
ip tcp synwait-time 5
! 
!
!
!
!
!
!
!
!
!
!
!
interface FastEthernet0/0
 ip address 10.0.0.2 255.255.255.252
 duplex full
!
interface FastEthernet1/0
 ip address 10.0.0.5 255.255.255.252
 speed auto
 duplex full
!
interface FastEthernet1/1
 no ip address
 shutdown
 speed auto
 duplex auto
!
interface FastEthernet2/0
 no ip address
 shutdown
 speed auto
 duplex auto
!
interface FastEthernet2/1
 no ip address
 shutdown
 speed auto
 duplex auto
!
interface GigabitEthernet3/0
 no ip address
 shutdown
 negotiation auto
!
interface GigabitEthernet4/0
 no ip address
 shutdown
 negotiation auto
!
ip forward-protocol nd
!
!
no ip http server
no ip http secure-server
!
!
!
!
control-plane
!
!
line con 0
 exec-timeout 0 0
 privilege level 15
 logging synchronous
 stopbits 1
line aux 0
 exec-timeout 0 0
 privilege level 15
 logging synchronous
 stopbits 1
line vty 0 4
 login
!
!
end

    system_id               FTX0945W0MY


In [11]:
# Similarly the ports of a node are internally represented as a list of GNS3NodePort objects
print(my_node.ports[0])


GNSNodePort:
    adapter_number   0
    data_link_types  {'Ethernet': 'DLT_EN10MB'}
    link_type        ethernet
    name             FastEthernet0/0
    port_number      0
    short_name       f0/0


In [12]:
# Links between nodes can currently only be displayed (later also created / deleted / captured)
print(my_project.links[0])


GNS3Link settings:
    capture_file_name  None
    capture_file_path  None
    capturing          False
    link_id            ec8cc0f1-455a-4c85-adf6-dc1083721cc5
    link_type          ethernet
    project_id         a1ea2a19-2980-41aa-81ab-f1c80be25ca7
    from               C3725-1 (f0/1)
    to                 C7200-2 (f0/0)


In [13]:
# Some pretty printing can be done like this
for link in my_project.links:
    print(f'{link.from_node.name} on port {link.from_port_name} is connected over {link.link_type} '
          f'to {link.to_node.name} on port {link.to_port_name}')


C3725-1 on port f0/1 is connected over ethernet to C7200-2 on port f0/0
C7200-1 on port f1/0 is connected over ethernet to C3725-2 on port f1/0
C3725-2 on port f2/0 is connected over ethernet to PC2 on port e0
PC1 on port e0 is connected over ethernet to C3725-1 on port f2/0
C3725-1 on port f0/0 is connected over ethernet to C7200-1 on port f0/0
C7200-2 on port f1/0 is connected over ethernet to C3725-2 on port f0/1

In [14]:
# Available snapshots can be viewed (and soon also created and deleted)
print(my_project.snapshots[0])


GNS3Snapshot settings:
    created_at   1502551604
    name         Test Snapshot
    project_id   a1ea2a19-2980-41aa-81ab-f1c80be25ca7
    snapshot_id  618b6734-6228-43fc-822a-49343d13718d


In [15]:
# Drawing objects can be inspected
my_drawing = my_project.drawings[0]
print(my_drawing)


GNS3Drawing:
    drawing_id  dc218a7f-221d-4340-9902-4d2c1726e081
    project_id  a1ea2a19-2980-41aa-81ab-f1c80be25ca7
    rotation    0
    svg         <svg height="20" width="67"><text fill="#000000" fill-opacity="1.0" font-family="TypeWriter" font-size="10.0" font-weight="bold">10.0.0.8/30</text></svg>
    x           -298
    y           -16
    z           1


In [16]:
# In theory this should work, not sure what's going wrong here. SVG from URL renders fine. #CheckLater
from IPython.display import SVG, display
display(SVG(data=my_drawing.svg))
# Would have been nice but isn't a priority


10.0.0.8/30