Source

imalse / scenario / spamming / botmaster.py

"""

This file contains the BotMaster Command Meta Description for the spamming
scenario.

"""

import jsonpickle

from core.nodes.botmaster import BotMasterManInput
from core.real.node import PhysicalNode
from scenario.spamming.config import Botmaster, CrawlCmd

def print_help():
    print "Type 'bots' to view connected bots."
    print "Type 'order' to start sending commands to bots"
    
class BotMaster(BotMasterManInput):
    
    """This class is a subclass of BotMasterManInput."""
    
    def __init__(self, desc=None):
        """Initialize."""
        if desc == None:
            desc = Botmaster()
            
        BotMasterManInput.__init__(self, desc)
        self.bots = []
        
        self.OPTIONS = {'bots': self.display_bots,
                        'order' : self.order_bot}
    
    def display_bots(self):
        """Display the bots connected to the server."""
        i = 0
        print self.bots
        for bot in self.bots:            
            print "Bot " + str(i) + " ip: " + bot[0] + " port: " + str(bot[1])
            i += 1
                    
        if i == 0:
            print "There aren't any bots connected with the server."    
    
    def order_bot(self):
        """Order a specific bot to execute the Crawl command."""
        cmd = CrawlCmd()
        
        print "Choose url"
        cmd.start_url = raw_input(">>")
        
        print "Define max amount of iterations"
        cmd.max_iterations = raw_input(">>")
                      
        print "Input botID"
        bot_id = raw_input(">>")
        cmd.addr = self.bots[int(bot_id)]
        
        self.node.send(self.sock, jsonpickle.encode(cmd))
        
        print "The order has been send."
        
    def parse_input(self):
        """Parse the raw input."""
        choice = raw_input(">>")
        
        if choice in self.OPTIONS:
            self.OPTIONS[choice]()
        else:
            print 'Try Again!'

    def add_address(self, socket, data):
        """Add addresses received from the server to the bots list."""
        for address in data.addresses:
            if self.bots.count(address) == 0:
                self.bots.append(address)
    
    def recv_ack(self):
        """Send and receive data to the server."""        
        self.node.send(self.sock, jsonpickle.encode(self.verification_command))      
        self.node.recv(self.sock, 512, self.dispatcher, threaded=True)
        self.node.sleep(5)
        
        if isinstance(self.node, PhysicalNode):
            print_help()        
            while True:            
                self.parse_input()
        else:
            self.node.send(self.sock, jsonpickle.encode(CrawlCmd()))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.