在python mininet拓扑中为主机运行命令

时间:2019-09-10 10:39:06

标签: python mininet topology

在我的拓扑结构(请参见下文)中,我希望所有客户端和服务器(标记不同并存储在各自的列表中)运行单独的Java程序。我知道我可以从命令行执行此操作,但是我希望使该过程自动化。

我尝试使用以下代码为每个服务器运行命令(例如):

server = self.addHost('server%s' % (s+1))
self.addLink(server, switch)
servers.append(server) # append to list of servers
server.cmd('java -jar /home/<snip>/path/to/server.jar ' + arg1 + ' ' + arg2)

但是出现以下错误:

  

AttributeError:“ str”对象没有属性“ cmd”

现在我有两个问题:

  1. 如何在以下拓扑中为每个服务器和主机运行cmd
  2. 对于客户端,我需要服务器IP-我只需从当前列表中获取服务器并将其附加。有没有办法获取其IP,例如server.getIP()

以下是拓扑:

#!/usr/bin/python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel

import math
import random

# network with n hosts connected to one switch
class NetworkTopology(Topo):
    def __init__( self ):
        # Initialize topology
        Topo.__init__( self )

        num_clients = 10
        num_servers = 2

        servers = []

        arg1 = '3000'
        arg2 = '3'

        switch = self.addSwitch('s1')

        for s in range(num_servers):
            server = self.addHost('server%s' % (s+1))
            self.addLink(server, switch)
            servers.append(server)
            server.cmd('java -jar /home/<snip>/path/to/server.jar ' + arg1 + ' ' + arg2)

        for c in range(num_clients):
            client = self.addHost('client%s' % (c+1))
            self.addLink(client, switch)
            # get random server
            s = random.choice(servers)
            client.cmd('java -jar /home/<snip>/path/to/client.jar ' + s + ' ' + arg1 + ' ' + arg2)


topos = { 'mynetwork': ( lambda: NetworkTopology() ) }

0 个答案:

没有答案