How do I use python scripts or (.py files) in mininet?

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

any idea please?


source to share

2 answers

This is how I do it. Copy and paste the following code or download this file: .

__author__ = 'Ehsan'
from mininet.node import CPULimitedHost
from mininet.topo import Topo
from 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

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', '', 6633)
    net = Mininet(topo=SimplePktSwitch(), host=CPULimitedHost, controller=None)


# if the script is run directly (sudo custom/
if __name__ == '__main__':


Then you can run topo simply by using

sudo -E python <nameofthefile>


Now you can simply use sudo -E python

mininet to run.

Here is the link .

Please note that you need a controller. Let me know if you need instructions.

Hope it helps.



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

cd mininet/custom


then enter:



which will show you the current files inside a custom file.
Then you can use a text editor nano

to create or edit the python / text file, for example, you can type:



and it will open a custom python code example file. Then you can exit it and save it as a new file.

Since I started editing and writing python codes, it will get easier when you learn how to SSH mininet with putty.




All Articles