0

I wrote a python module to generate a random topology using Mininet and connected OpenDayLight as the remote controller.

I would like to pass a command to the mininet CLI, but from inside the python module. After generating the hosts and switches as well as connecting them to the remote controller, the module randomly chooses 2 hosts to act as Server and Client. I am having trouble doing something similar to this, but from in-line inside the python module:

mininet> h<random_number> sudo python HTTPTraffic.py <SrcIP> <DstIP>

HTTPTraffic.py is another module that takes arguments SrcIP and DstIP

This is the part of the main module that elects 2 random hosts and gets their IPs. I would like the HTTPTraffic.py to run in the host corresponding to SrcIP

Nodes = list(range(1,h))
Src = random.choice(Nodes)         # randomly chooses 1 host
Nodes.remove(Src)
Dst = random.choice(Nodes)         # randomly chooses another host

SrcName = 'h%s' %(Src)
DstName = 'h%s' %(Dst)
SrcNode=net.get(SrcName)
DstNode=net.get(DstName)

SrcIP = SrcNode.IP()               # IP retrieval of chosen hosts
DstIP = DstNode.IP()
print ('The Souce/Client is: '+ SrcName)
print (SrcIP)
print ('The Destination/Server is: '+ DstName)
print (DstIP) 

SrcName +".cmd('sudo python HTTPTraffic.py "+SrcIP+" " +DstIP+"')"    # this is where I am having trouble

From the mininet API documentation (See section 'Customizing a Network'), something like that is possible but I am having so much trouble doing it. Also, I would like for this command to run in the background while the main program runs. Any feedback is appreciated.

  • I just realized this after I read your question again, it looks like my suggestion doesn't actually answer your question. It looks like to do what you are trying to do, you're going to want to use the mininet API instead, since the mininet CLI is acts as an interactive terminal. – wheeler Mar 30 '17 at 20:34

2 Answers2

0

I recommend you take a look at the documentation for the Python module subprocess, which acts as an platform-agnostic API for running other programs from your program.

For example, you can call ls by adding the following line to your code:

subprocess.run(["ls", "-l"])

The first argument of run takes an array of strings where each string is a part of the command you are executing.

wheeler
  • 2,823
  • 3
  • 27
  • 43
  • actually this is not what I am looking for. My primary issue is how to pass a command from python to the Mininet CLI. Your suggestion might be answering my second issue which is to run this in the background. Still I wanna try it as soon as I find a solution to my first problem. – Ahmed Ayman Ibrahim Mar 31 '17 at 09:25
  • Take a look at [this](http://stackoverflow.com/questions/10962141/wrapping-an-interactive-cli-in-python) SO question and answer, and read some of the comments. There is a way to wrap an interactive CLI in python using `subprocess.Popen`. Essentially you will call `sudo mn` using `Popen` and pass it commands with `proc.stdin.write("command string")` and read the output of mininet with `line = proc.stdout.readline()`. – wheeler Mar 31 '17 at 14:23
  • I tried using `subprocess` as you suggested, but it is passing the command to the Linux CLI instead of mininet CLI. You mentioned calling `sudo mn` using Popen, how it this done can you provide an example ? Also, the code seems to have problems with passing commands consisting of more that 1 word. `shlex.split` did not do the trick unfortunately. – Ahmed Ayman Ibrahim Apr 06 '17 at 12:07
  • Ok, so it's going to look a little like this: `proc = subprocess.Popen(['sudo', 'mn'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)` Then you will pass commands to mininet by using the `proc.stdin.write` method. You can then read the results by using `proc.stdout.readline()`. – wheeler Apr 12 '17 at 17:15
0

You should to build somthing like this:

n=3
for i in range(4):
    user_input = 'h' + str(n)
    globals()[user_input] = net.addHost('h'+ str(n), cls=Host, ip='10.0.1.2'+ str(n) +'/24', defaultRoute='via 10.0.1.1')
    net.addLink(globals()[user_input],s6)
    n += 1