Exemplo n.º 1
0
def sdn():  #initial setup and ssh into SDN controller (Ryu)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.56.10',
                port=22,
                username='******',
                password='******')
    ssh1 = ssh.invoke_shell()
    ssh1.send("sudo python topo.py\n")
    stdin, stdout, stderr = ssh.exec_command("sudo python parse.py\n")
    stdin.close()
    dpid = stdout.read()
    time.sleep(2)
    output = ssh1.recv(65535)
    output = output.decode()
    ssh.close()
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.80.10', port=22, username='******', password='******')
    ssh1 = ssh.invoke_shell()
    ssh1.send("python firewallrules1.py\n")
    ssh1.send("python firewallrules2.py\n")
    output1 = ssh1.recv(65535)
    output1 = output1.decode()
    ssh.close()
    print(output1)
    return (dpid)
Exemplo n.º 2
0
def sdnnodes():  #check nodes
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.56.10',
                port=22,
                username='******',
                password='******')
    ssh1 = ssh.invoke_shell()
    ssh1.send("sudo python topo.py\n")
    stdin, stdout, stderr = ssh.exec_command("sudo python parse.py\n")
    stdin.close()
    out = str(stdout.read())
    print(type(out))
    #time.sleep(2)
    output = ssh1.recv(65535)
    output = output.decode()
    ssh.close()
    out1 = out.split("[")[1].split("]")[0]
    dpid = []
    dpid.append((out1.split(",")[0]).strip("'"))
    dpid.append((out1.split(",")[1]).split("'")[1])
    print(dpid)
    switches = []
    for i in dpid:
        switches.append("s" + i[-1])
    return (switches)
def count_words():
    """ Dictionary to store different words
        -> key: word
        -> value: many times that the key appears
    """
    dictio = {}
    # Read file content
    for line in stdin:

        # If end of file -> stop
        if not line:
            break

        word = line[:-1].replace("-", "")
        if word not in dictio:
            dictio[word] = 1
        else:
            dictio[word] += 1

    stdin.close()

    # Store in a file
    file = open(FILE_PATH, "a")
    file.write(str(dictio) + "\n")
    file.close()
Exemplo n.º 4
0
def inetd_sockets(max_socks):
    """
    """
    sock = fromfd(stdin.fileno())
    stdin.close()
    stdout.close()
    stderr.close()
    return sock
Exemplo n.º 5
0
def main():
    line = stdin.readline()
    while (len(line) != 0):
        queries = [int(x) for x in line.split()]
        for v in queries:
            stdout.write(str(ans(v)))

        stdout.write("\n")
        line = stdin.readline()

    stdout.close()
    stdin.close()
Exemplo n.º 6
0
def main():
    global stdin, stdout
    cond = type(stdin) == str and type(stdin) == str
    if cond:  # use file instead of standard
        stdin = open(stdin, "rt")
        stdout = open(stdout, "wt")
    if METHO == 1:  # method
        func1()
    else:
        func2()
    stdin.close()
    stdout.close()
Exemplo n.º 7
0
def get_commit_errors(file_type, function):
    checkable = True
    if file_type == "js":
        checkable = config.getboolean("commit", "CHECK_JAVASCRIPT")
    elif file_type == "php":
        checkable = config.getboolean("commit", "CHECK_PHP")

    if not checkable:
        return None

    files = _get_commit_files(file_type)
    if not files:
        return None

    #Get files are both in cached zone and modified zone
    system("git diff --name-status > /tmp/git_hook")
    modified_files = _get_files(file_type, 2)
    if modified_files:
        #Ask user whether add a file in modified zone to commit
        modified_files = [
            modified_file for modified_file in modified_files
            if modified_file in files
        ]
        stdin = open('/dev/tty')
        for modified_file in modified_files:
            print(
                'File %s has been modified but not in the cached zone, add it into ? [Y|n]'
                % colored(modified_file, 'red'))
            if not stdin.readline().strip().lower().startswith('n'):
                system('git add %s' % modified_file)

        stdin.close()

    errors = []
    for file_path in files:
        if path.exists(file_path):
            file_error = function(file_path)
            if file_error:
                errors.append(file_error)

    if errors:
        errors = colored(
            "There are some errors in below %s files:\n\n" % file_type,
            "magenta") + "\n".join(errors).strip("\n")

    return errors
Exemplo n.º 8
0
def main():
    global ELEMS, ACUMS, ANS, N, KEYS
    setrecursionlimit(50000)
    line = stdin.readline()

    while len(line) != 0:

        ELEMS = [0 for i in range(20)]
        ACUMS = [0 for i in range(20)]
        ANS = list()
        KEYS = {}
        N = 0

        elems = [int(x) for x in line.split()]
        t = elems[0]
        N = elems[1]

        if N == 0:
            break

        for i in range(0, N):
            ELEMS[i] = elems[i + 2]

        for i in range(N - 1, -1, -1):
            if i + 1 == N:
                ACUMS[i] = ELEMS[i]
            else:
                ACUMS[i] = ELEMS[i] + ACUMS[i + 1]

        writer.write("Sums of " + str(t) + ":\n")
        solve(0, "", t)

        if len(ANS) > 0:
            for s in ANS:
                writer.write(s + "\n")
        else:
            writer.write("NONE\n")

        ANS = list()
        KEYS = {}

        line = stdin.readline()

    stdin.close()
    writer.close()
Exemplo n.º 9
0
def get_commit_errors(file_type, function):
  checkable = True
  if file_type == "js":
    checkable = config.getboolean("commit", "CHECK_JAVASCRIPT")
  elif file_type == "php":
    checkable = config.getboolean("commit", "CHECK_PHP")

  if not checkable:
    return None

  files = _get_commit_files(file_type)
  if not files:
    return None

  #Get files are both in cached zone and modified zone
  system("git diff --name-status > /tmp/git_hook")
  modified_files = _get_files(file_type, 2)
  if modified_files:
    #Ask user whether add a file in modified zone to commit
    modified_files = [modified_file for modified_file in modified_files if modified_file in files]
    stdin = open('/dev/tty')
    for modified_file in modified_files:
      print(
        'File %s has been modified but not in the cached zone, add it into ? [Y|n]' %
        colored(modified_file, 'red')
      )
      if not stdin.readline().strip().lower().startswith('n'):
        system('git add %s' % modified_file)

    stdin.close()

  errors = []
  for file_path in files:
    if path.exists(file_path):
      file_error = function(file_path)
      if file_error:
        errors.append(file_error)

  if errors:
    errors = colored(
      "There are some errors in below %s files:\n\n" % file_type, "magenta"
    ) + "\n".join(errors).strip("\n")

  return errors
Exemplo n.º 10
0
def main():
    line = stdin.readline()
    while (len(line) != 0):
        n, w, d, wscoins = [int(x) for x in line.split()]

        # n * (n + 1) / 2
        summ = w * ((n - 1) * (n) / 2)

        # Esto deberia ser cierto si esta la basket en el N
        # Pero curiosamente en el input no es asi, esta malo..
        if summ == wscoins:
            stdout.write(str(n) + "\n")
        else:
            lo = 1
            hi = n
            found = False
            while lo + 1 != hi:
                mid = (lo + hi) >> 1

                tmp = summ - (mid * w) + mid * (w - d)

                if tmp < wscoins:
                    hi = mid
                elif tmp > wscoins:
                    lo = mid
                else:
                    stdout.write(str(mid) + "\n")
                    found = True
                    break
            # Ojo en Busqueda binaria siempre probar con lo al final
            # Porque lo al final no se computa
            if not found:
                # Pruebe con el mismo lo, ya que que si lo + 1 == hi falta probar el mismo lo
                tmp = summ - (lo * w) + lo * (w - d)
                if tmp == wscoins:
                    stdout.write(str(lo) + "\n")
                else:
                    # Si no, es n el resultado final
                    stdout.write(str(n) + "\n")

        line = stdin.readline()

    stdout.close()
    stdin.close()
Exemplo n.º 11
0
def become_daemon():
    try:
        pid = fork()
    except OSError:
        print 'unable to fork'
        exit(1)

    if pid == 0:
        # Daemonize
        setsid()
        # We redirect only the 3 first descriptors
        file_desc = os_open(devnull, O_RDWR)
        stdin.close()
        dup2(file_desc, 0)
        stdout.flush()
        dup2(file_desc, 1)
        stderr.flush()
        dup2(file_desc, 2)
    else:
        exit()
Exemplo n.º 12
0
def become_daemon():
    try:
        pid = fork()
    except OSError:
        print 'unable to fork'
        exit(1)

    if pid == 0:
        # Daemonize
        setsid()
        # We redirect only the 3 first descriptors
        file_desc = os_open(devnull, O_RDWR)
        stdin.close()
        dup2(file_desc, 0)
        stdout.flush()
        dup2(file_desc, 1)
        stderr.flush()
        dup2(file_desc, 2)
    else:
        exit()
Exemplo n.º 13
0
def main():
    line = stdin.readline()
    while (len(line) != 0):
        R, C = [int(x) for x in line.split()]
        A = [0 for i in range((R * C))]
        offset = 0
        rowIdx = 0
        for i in range(R):
            line = stdin.readline().split()
            for e in line:
                x = int(e)
                if x == R * C:
                    rowIdx = (i + 1)
                    A[offset] = float('inf')
                else:
                    A[offset] = x

                offset += 1

        count = 0
        for i in range(len(A)):
            # Empty square
            if A[i] == float('inf'):
                continue

            for j in range(i + 1, len(A)):
                # Notice nothing is greater than inf so it wont do anything harmful
                if A[i] > A[j]:
                    count += 1

        tot = count + rowIdx
        if tot % 2 == 0:
            stdout.write("Y\n")
        else:
            stdout.write("N\n")

        line = stdin.readline()

    stdout.close()
    stdin.close()
Exemplo n.º 14
0
    Nchain = len(set(allsame_sta.keys()) - set(head_sta.keys()) - set(tail_sta.keys()))

    while head_sta:
        findnext = True
        remain = head_sta.keys()
        i = 0
        while findnext:
            if i >= len(remain):
                return 0
            h = remain[i]
            if h in tail_sta:
                i += 1
            else:
                findnext = False
                Nchain += 1
                t = cars[head_sta[h]][-1]
                del head_sta[h]
                while t in head_sta:
                    h = t
                    t = cars[head_sta[h]][-1]
                    del head_sta[h]

    for i in xrange(1,1+Nchain):
        ways = (ways*i) % 1000000007
    return ways

T = int(stdin.readline())
for t in xrange(1,T+1):
    print 'Case #{}: {}'.format(t, each_case())
stdin.close()
Exemplo n.º 15
0
stdin = open('A-large-practice.in', 'r')
#stdout = open('A-small.out', 'w')
stdout = open('A-large.out', 'w')

T = int(stdin.readline().strip())

for t in range(0, T):

    (A, N) = [int(x) for x in stdin.readline().strip().split()]

    m = [int(x) for x in stdin.readline().strip().split()]

    ans = N

    op = 0

    m.sort()

    while A > 1 and len(m) > 0:
        while A <= m[0]:
            A += A - 1
            op += 1
        A += m[0]
        m[0:1] = []
        ans = min(ans, op + len(m))

    stdout.write('Case #{0}: {1}\n'.format(t+1, ans))

stdin.close()
stdout.close()
Exemplo n.º 16
0
 def execute(input: Optional[StringIO], mem: dict) -> Tuple[Optional[StringIO], str]:
     stdin.close()
     return None, ""