def main(): if '-gen' in sys.argv: A = int(sys.argv[2]) B = int(sys.argv[3]) ip_block(A, B) # Create a way to run this distributed from python elif '-multi-setup' in sys.argv: import network workers = [] peers = {} # Determine which nodes are available to work for n in network.get_node_names(): name = n.split('/')[-1].split('.')[0] h, i, p, m = utils.load_credentials(name, False) peers[name] = [h, i, p, m] # check if online if network.check_connected(h, i, p): workers.append(name) else: print '%s@%s offline' % (name, i) # Now distribute the assignments print '[*] %d Workers Available' % len(workers) for w in workers: # give them the latest copy of this program H, I, P, M = peers[w] rpath = '/home/%s/Documents/PoolParty/code/0.6/' % H if utils.remote_file_exists(H, I, P, rpath + 'trace.py'): utils.ssh_exec('rm %strace.py' % rpath, I, H, P, False) utils.put_file('trace.py', rpath, H, I, P, False) # Now give them a ip_space.txt file and a script to run trace.py utils.ssh_exec('rm %shops.txt' % rpath, I, H, P, True) # c = 'cd %s; python trace.py 0>&-' % rpath # put this in a .sh script and transfer, then execute # utils.ssh_exec('cd %s; python trace.py 0>&-' % rpath,H,I,P,False) elif '-multi-view' in sys.argv: import tracewatch print('[*] Monitoring Traces...') else: ips = list(utils.swap('ip_space.txt', False)) if not os.path.isfile(os.getcwd() + '/hops.txt'): os.system('touch hops.txt') random.shuffle(ips) pool = multiprocessing.Pool(10) for ip in ips: try: event = pool.apply_async(func=trace, args=(ip, False)) hopdata, nhops = event.get(timeout=60) print ' - %d hops to %s' % (nhops, ip) open('hops.txt', 'a').write('%s:%d\n' % (ip, nhops)) except multiprocessing.TimeoutError: open('hops.txt', 'a').write('%s:?\n' % ip) pass
def cmd_all(peers, cmd): replies = {} for p in peers.keys(): ip = peers[p]['ip'] mac = peers[p]['mac'] hname = peers[p]['hname'] pword = peers[p]['pword'] connected = peers[p]['connected'] replies[p] = utils.ssh_exec(cmd, ip,hname, pword, True) return replies
def check_connected(host, ip, passwd): try: reply = utils.ssh_exec('whoami',ip,host,passwd,False).pop() except IndexError: return False if reply == host: return True else: return False
def update_all(peers): results = {} for p in peers.keys(): ip = peers[p]['ip'] mac = peers[p]['mac'] hname = peers[p]['hname'] pword = peers[p]['pword'] connected = peers[p]['connected'] update = 'cd Documents/PoolParty/code; bash update.sh' results[p] = utils.ssh_exec(update, ip, hname, pword, True) return results
def add_nodes(): # Now how to setup nodes as efficiently as possible? possible_nodes = network.autodiscover_local() for ip_addr, MAC in possible_nodes: prompt = ('[?] Would you like to add %s as a Peer? [y/n]: ' % ip_addr) if input(prompt).upper() == 'Y': clientAdded, newPeer = register_commandline(ip_addr, MAC) if clientAdded: print('[*] Adding %s:%s' % (ip_addr, MAC)) if newPeer['hname'] == utils.ssh_exec( 'whoami', ip_addr, newPeer['hname'], newPeer['pword'], True).pop().replace('\n', ''): print('[*] Successfully Added %s to Network' % newPeer['pname'])
def distribute(self): # now make sure all remote hosts have share folders for receiving for peer in self.peers: h, i, p, k = utils.load_credentials(peer, False) if utils.remote_file_exists( h, i, p, '/home/%s/PoolParty/code/0.5/PoolData/Shares' % h) == 1: # get their shares share_names = utils.ssh_exec( 'ls /home/%s/PoolParty/code/0.5/PoolData/Shares' % h, i, h, p, False) # remote machine needs to hash its shares # distribute this peers files too # for fs in share_distro[peer]: for fs in os.listdir('PoolData/Shares'): recipient = self.distro[fs] rh, ri, rp, rk = utils.load_credentials(recipient, False) f = 'PoolData/Shares/' + fs rf = '/home/%s/PoolParty/code/0.5/PoolData/Shares/' % rh if recipient == peer and utils.remote_file_exists( h, i, p, rf + fs) == 0: print('Giving %s file: %s' % (recipient, fs)) # print(rf+fs) utils.put_file(f, rf, rh, ri, rp, True) # else: # print('%s has file %s' % (recipient, fs)) else: if utils.remote_file_exists( h, i, p, '/home/%s/PoolParty/code/0.5/PoolData' % h) == 0: utils.ssh_exec( 'mkdir /home/%s/PoolParty/code/0.5/PoolData' % h, i, h, p, False) utils.ssh_exec( 'mkdir /home/%s/PoolParty/code/0.5/PoolData/Shares' % h, i, h, p, False)
def main(): if '--setup' in sys.argv: utils.initialize_folders() utils.add_local_peers_pv2() else: nodes = test_connections(False) if '--cmd' in sys.argv and len(sys.argv) > 2: hname = utils.sys.argv[2] cmd = utils.arr2chr(sys.argv[3:]) if hname in nodes.keys(): i = nodes[hname]['ip'] h = nodes[hname]['hname'] p = nodes[hname]['pword'] result = utils.arr2str(utils.ssh_exec(cmd, i, h, p, False)) print result
def check_work_files(host, ip, password): checkCmd = 'file=/home/%s/Work/jobs.txt;[ ! -e $file ]; echo $?' % host return int(utils.ssh_exec(checkCmd, ip, host, password, False))
def check_connected(host, ip, passwd): if utils.ssh_exec('whoami', ip, host, passwd, False).pop() == host: return True else: return False