Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use python scripts or (.py files) in mininet?

Tags:

mininet

I am new to mininet and python. i want to execute a python script in mininet, but i don't know how can we run python scripts in mininet and where to store .py files in order to call from mininet.

any idea please?

like image 523
zafar malik Avatar asked Jul 23 '15 14:07

zafar malik


People also ask

How do I run a Python file in Mininet?

Copy and paste the bellow code or download this file: Simple_Pkt_Topo.py. Now, you could just use sudo -E python Simple_Pkt_Topo.py to start up the mininet. Here is the tutorial link. Note that you need a controller.

How do I run a Python .py file?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do I run a Python file in NPP?

To run the Python file from the notepad++ text editor, you have to click on the Run option from the menu and then choose the first option - Run... from the dropdown menu. It will open a new window on the screen, as shown below. Alternatively, You can also press the F5 key on the keyboard to open this window.


2 Answers

When you open mininet just navigate to custom folder by typing:

cd mininet/custom

then type:

ls

which will show you the current files inside the custom file.
Then you can use the nano text editor to create or edit a python/text file, for example you can type:

nano custom.py

and it will open the custom file that has an example of using python code. Then you can exit it and save it as a new file.

That's how I started to edit and write python codes, then it will become easier once you learn how to SSH the mininet using putty.

good luck

like image 126
ahmed faeq Avatar answered Oct 13 '22 16:10

ahmed faeq


Here is how I do it. Copy and paste the bellow code or download this file: Simple_Pkt_Topo.py.

__author__ = 'Ehsan'
from mininet.node import CPULimitedHost
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.log import setLogLevel, info
from mininet.node import RemoteController
from mininet.cli import CLI
"""
Instructions to run the topo:
    1. Go to directory where this fil is.
    2. run: sudo -E python Simple_Pkt_Topo.py.py

The topo has 4 switches and 4 hosts. They are connected in a star shape.
"""


class SimplePktSwitch(Topo):
    """Simple topology example."""

    def __init__(self, **opts):
        """Create custom topo."""

        # Initialize topology
        # It uses the constructor for the Topo cloass
        super(SimplePktSwitch, self).__init__(**opts)

        # Add hosts and switches
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')
        h3 = self.addHost('h3')
        h4 = self.addHost('h4')

        # Adding switches
        s1 = self.addSwitch('s1', dpid="0000000000000001")
        s2 = self.addSwitch('s2', dpid="0000000000000002")
        s3 = self.addSwitch('s3', dpid="0000000000000003")
        s4 = self.addSwitch('s4', dpid="0000000000000004")

        # Add links
        self.addLink(h1, s1)
        self.addLink(h2, s2)
        self.addLink(h3, s3)
        self.addLink(h4, s4)

        self.addLink(s1, s2)
        self.addLink(s1, s3)
        self.addLink(s1, s4)


def run():
    c = RemoteController('c', '0.0.0.0', 6633)
    net = Mininet(topo=SimplePktSwitch(), host=CPULimitedHost, controller=None)
    net.addController(c)
    net.start()

    CLI(net)
    net.stop()

# if the script is run directly (sudo custom/optical.py):
if __name__ == '__main__':
    setLogLevel('info')
    run()

Then you can run the topo by just using

sudo -E python <nameofthefile>

Now, you could just use sudo -E python Simple_Pkt_Topo.py to start up the mininet.

Here is the tutorial link.

Note that you need a controller. Let me know if you need some instructions on that.

Hope it helps.

like image 27
Ehsan Ab Avatar answered Oct 13 '22 17:10

Ehsan Ab