Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
james-scripts/choose_your_port.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 lines (80 sloc)
2.55 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
import hashlib | |
def memoize(func): | |
"""Decorator used to cache results of a function.""" | |
cache = dict() | |
def memoized_func(*args): | |
if args in cache: | |
return cache[args] | |
result = func(*args) | |
cache[args] = result | |
return result | |
return memoized_func | |
@memoize | |
def choose_from(input_options, entropy): | |
""" | |
Choose from a list in a random-like fashion, but be consistent | |
input_options: tuple of options | |
entropy: string used to | |
""" | |
prng = random.Random(hashlib.sha256(entropy.encode()).digest()) | |
#prng = random.Random(entropy) | |
n = prng.randrange(0,len(input_options)) % len(input_options) | |
return input_options[n] | |
def main(): | |
OPTIONS = (7001, 7002, 7003) | |
for r in ROUTER_LIST: | |
opt = choose_from(OPTIONS, r) | |
print("{:>10} {}".format(opt, r)) | |
ROUTER_LIST = [ | |
'rtsw.alba.net.internet2.edu', | |
'rtsw.ashb.net.internet2.edu', | |
'rtsw.atla.net.internet2.edu', | |
'rtsw.bato.net.internet2.edu', | |
'rtsw.bost.net.internet2.edu', | |
'rtsw.char.net.internet2.edu', | |
'rtsw.chic.net.internet2.edu', | |
'rtsw.cinc.net.internet2.edu', | |
'rtsw.clev.net.internet2.edu', | |
'rtsw.dall.net.internet2.edu', | |
'rtsw.dall3.net.internet2.edu', | |
'rtsw.denv.net.internet2.edu', | |
'rtsw.elpa.net.internet2.edu', | |
'rtsw.eqch.net.internet2.edu', | |
'rtsw.hart2.net.internet2.edu', | |
'rtsw.houh.net.internet2.edu', | |
'rtsw.hous.net.internet2.edu', | |
'rtsw.indi.net.internet2.edu', | |
'rtsw.jack.net.internet2.edu', | |
'rtsw.jcsn.net.internet2.edu', | |
'rtsw.kans.net.internet2.edu', | |
'rtsw.lasv.net.internet2.edu', | |
'rtsw.losa.net.internet2.edu', | |
'rtsw.loui.net.internet2.edu', | |
'rtsw.minn.net.internet2.edu', | |
'rtsw.miss2.net.internet2.edu', | |
'rtsw.newy2.net.internet2.edu', | |
'rtsw.newy32aoa.net.internet2.edu', | |
'rtsw.phil.net.internet2.edu', | |
'rtsw.phoe.net.internet2.edu', | |
'rtsw.pitt.net.internet2.edu', | |
'rtsw.port.net.internet2.edu', | |
'rtsw.rale.net.internet2.edu', | |
'rtsw.reno.net.internet2.edu', | |
'rtsw.salt.net.internet2.edu', | |
'rtsw.sanj.net.internet2.edu', | |
'rtsw.seat.net.internet2.edu', | |
'rtsw.star.net.internet2.edu', | |
'rtsw.sunn.net.internet2.edu', | |
'rtsw.tucs.net.internet2.edu', | |
'rtsw.tuls.net.internet2.edu', | |
'rtsw.wash.net.internet2.edu', | |
'rtsw.wash2.net.internet2.edu', | |
'rtsw.wilc.net.internet2.edu', | |
'rtsw2.ashb.net.internet2.edu', | |
'rtsw2.sanj.net.internet2.edu', | |
'rtsw3.eqch.net.internet2.edu', | |
] | |
if __name__ == "__main__": | |
main() |